clang-format for boost program options

1.7k views Asked by At

clang-format seems to make a big mess out of blocks like this:

desc.add_options()("help", "output usage")
      ("inputDirectory", po::value<boost::filesystem::path>()->required(), "The input path")
      ("outputDirectory", po::value<boost::filesystem::path>()->required(), "The output path");

I know about // clang-format off to explicitly not format a block, but is there a set of configuration rules to make it do something reasonable with this?

3

There are 3 answers

0
Alexander Chen On

Some stupid checker may also report that "You shouldn't put ( before whitespaces". So you may define temporary local variable to store options_description_easy_init.

auto op = desc.add_options();
op("help", "output usage");
op("inputDirectory", po::value<boost::filesystem::path>()->required(), "The input path");
op("outputDirectory", po::value<boost::filesystem::path>()->required(), "The output path");

And then use clang-format to reformat the code.

0
John32ma On

clang-format cannot remove comments, so adding comments at the end of lines enables nice looking code while maintaining the benefits of automatic indentation. This is especially helpful with items from libraries like boost::assign. For example when using ColumnLimit of 100 in clang-format:

    group2 = boost::assign::list_of<score_pair>("Norway", list_of(1)(0))("USA", list_of(0)(0))(
        "Andorra", list_of(1)(1));

    // Versus:

    group2 = boost::assign::list_of<score_pair> //
        ("Norway", list_of(1)(0))               //
        ("USA", list_of(0)(0))                  //
        ("Andorra", list_of(1)(1));

Using the desc.add_options example from above, adding line-break comments looks like:

    desc.add_options()("help", "output usage")                                                 //
        ("inputDirectory", po::value<boost::filesystem::path>()->required(), "The input path") //
        ("outputDirectory", po::value<boost::filesystem::path>()->required(), "The output path");
1
mtszkw On

Not sure if you can handle it by only configuring .clang-format options. However, there is still something that you can do about boost::program_options syntax. Instead of chaining operator() you could create program_options::options_description object and add options in multiple lines:

namespace po = boost::program_options;

po::options_description desc;
desc.add_options()("inputDirectory", po::value<boost::filesystem::path>()->required(), "The input path");

Now even if clang-format breaks your formatting, I believe this will look a bit better than before. If it is not good enough for you and formatting is your pain in the neck, I'd suggest defining some function or whatever to shorten these lines (in our project we've got vector of ConfigField structures that contain value_semantic, names etc. and we iterate it calling add_options - it looks shorter).

No other way I'm afraid.

BTW: Yea, it's kinda old question, but there's no answer and we had similar problem recently.