Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

clang-format for boost program options

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?

like image 497
David Doria Avatar asked Dec 22 '16 17:12

David Doria


2 Answers

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.

like image 135
mtszkw Avatar answered Sep 25 '22 13:09

mtszkw


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");
like image 30
John32ma Avatar answered Sep 22 '22 13:09

John32ma