Tuesday, 15 July 2014

c++ - Best way of handling level 2 commands with Boost ProgramOptions -


I'm interested in making an executable that takes level 2 commands on the command line - such as git commit and git add are your separate orders within the same EXE, so I have a question: is there any way to simplify it with programmers? I know that I can define different schemes and then check the argv [1] against a particular string, but I hope the way of cleaner is the way. Thanks!

You can use:

  #include & lt; Boost /program_options.hpp> # Include & lt; Iostream & gt; # Include & lt; Algorithm & gt; #include & lt; Iterator & gt; Name space po = boost :: program_options; // Auxiliary function to simplify the body Template & lt; Class T & gt; Std :: ostream & amp; Operator & lt; & Lt; (Std :: ostream & os, const std :: vector & lt; T & gt; & amp; v) {copy (v.begin (), v.end (), std :: ostream_iterator & lt; t & Gt; (OS, ")); return OS;} int main (int ac, char * av []) {try {po :: options_description desc (" allowed option "); (" Command_opt ", po :: value & Lt;) (generate "help", "help message") ("command", po :: value  std :: vector & lt; std :: string & gt; ;), "Command option"); Po :: positional_options_description p; P.add ("command", 1); P.add ("command_opt", -1); Po :: variables_map vm; Po :: store (Po :: command_line_parser (ac, av). Options (desc) .positional (p) .run (), vm); po :: notify (vm); if (vm.Count ("help")) {std: : Cout & lt; & lt; "Usage: Options_details [options] \ n"; Std :: cout  Vm [" command "]. As & lt; std :: string & gt; () << "\ N";} If (vm.count ("command_opt")) {std :: cout  

But AFAIK you need to check the logic option, which option is compatible with other options because there is no dependency system in boost program options.

A separate option group can be helpful for the usage text generated by grouping each order options.


No comments:

Post a Comment