I use clang-format to automatically format the code and in most cases it does it with a bang, but there is a code that it does not format as we would like.

A piece of code from the library example ( file on github ):

 int main(int argc, char* argv[]) { options.add_options() ("a,apple", "an apple", cxxopts::value<bool>(apple)) ("b,bob", "Bob") ("f,file", "File", cxxopts::value<std::vector<std::string>>(), "FILE") ("i,input", "Input", cxxopts::value<std::string>()) ("o,output", "Output file", cxxopts::value<std::string>() ->default_value("a.out")->implicit_value("b.def"), "BIN") ("positional", "Positional arguments: these are the arguments that are entered " "without an option", cxxopts::value<std::vector<std::string>>()) ("long-description", "thisisareallylongwordthattakesupthewholelineandcannotbebrokenataspace") ("help", "Print help") ("int", "An integer", cxxopts::value<int>(), "N") ("option_that_is_too_long_for_the_help", "A very long option") ; } 

turns into something unreadable:

 int main(int argc, char *argv[]) { options.add_options()("a,apple", "an apple", cxxopts::value<bool>(apple))("b,bob", "Bob")( "f,file", "File", cxxopts::value<std::vector<std::string>>(), "FILE")("i,input", "Input", cxxopts::value<std::string>())( "o,output", "Output file", cxxopts::value<std::string>()->default_value("a.out")->implicit_value( "b.def"), "BIN")("positional", "Positional arguments: these are the arguments that are entered " "without an option", cxxopts::value<std::vector<std::string>>())( "long-description", "thisisareallylongwordthattakesupthewholelineandcannotbebrokenataspace")( "help", "Print help")("int", "An integer", cxxopts::value<int>(), "N")( "option_that_is_too_long_for_the_help", "A very long option"); } 

.clang-format :

 BasedOnStyle: LLVM IndentWidth: 2 --- Language: Cpp Standard: Cpp11 

Is there any way to fix this?

  • 2
    Are you sure that the first code sample is something readable? Personally, I did not understand what this code means. - Vlad from Moscow
  • @VladfromMoscow, if you know what happens in the code, then for me the first piece of code is more readable than the first. And yes, this code is not working. - pank

1 answer 1

It turned out all quite simple:

 int main(int argc, char* argv[]) { // clang-format off options.add_options() ("a,apple", "an apple", cxxopts::value<bool>(apple)) ("b,bob", "Bob") ("f,file", "File", cxxopts::value<std::vector<std::string>>(), "FILE") ("i,input", "Input", cxxopts::value<std::string>()) ("o,output", "Output file", cxxopts::value<std::string>() ->default_value("a.out")->implicit_value("b.def"), "BIN") ("positional", "Positional arguments: these are the arguments that are entered " "without an option", cxxopts::value<std::vector<std::string>>()) ("long-description", "thisisareallylongwordthattakesupthewholelineandcannotbebrokenataspace") ("help", "Print help") ("int", "An integer", cxxopts::value<int>(), "N") ("option_that_is_too_long_for_the_help", "A very long option") ; // clang-format on } 

With the help of comments

 // clang-format off ... // clang-format on 

You can disable the formatting for the desired piece of code.