How to display the command line operand description in the -help output file

I use Boost.program_options to parse command lines for my implementation of POSIX utilities. Take it as a simple example cmp.

Now I would like to have an extra argument --helpthat shows a description of all the arguments, which is important in this case. I have:

po::options_description options("Options");
options.add_options()("help", "Show this help output.")
                     (",l", "(Lowercase ell.) Write the byte number (decimal) and the differing bytes (octal) for each difference.")
                     (",s", "Write nothing for differing files; return exit status only.")

po::positional_options_description operands;
operands.add("file1", 1);//, "A pathname of the first file to be compared. If file1 is '-', the standard input shall be used.")
operands.add("file2", 1);//, "A pathname of the second file to be compared. If file2 is '-', the standard input shall be used.");

po::variables_map vm;
po::store(po::command_line_parser(argc, argv).options(options).positional(operands).run(), vm);
po::notify(vm);

if(vm.count("help"))
{
  std::cout << "cmp: compare two files\nUsage: cmp [ -l | -s ] file1 file2\n" << options;
  return 0;
}

which cannot show description file1and file2options. I can, of course, add them to options, but this will add at least two unwanted arguments [-]-file{1,2}that I really don't want. I just need this output for --help(without explicitly hard coding):

cmp: compare two files
Usage: cmp [ -l | -s ] file1 file2
Options:
  --help                Show this help output.
  -l                    (Lowercase ell.) Write the byte number (decimal) and the differing bytes (octal) for each difference.
  -s                    Write nothing for differing files; return exit status only.
Operands:
  file1                 A pathname of the first file to be compared. If file1 is '-', the standard input shall be used.
  file2                 A pathname of the second file to be compared. If file2 is '-', the standard input shall be used.

, ? , , .

. , , .

+5
1

, "dummy", boost:: program_options , ?

options.

- :

po::options_description dummy_options("Operands");
dummy_options.add_options()
    ("file1", po::value<std::string>(), "A pathname of the first file to be compared. If file1 is '-', the standard input shall be used.")
    ("file2", po::value<std::string>(), "A pathname of the second file to be compared. If file2 is '-', the standard input shall be used.")
    ;

std::stringstream s;
s << dummy_options;
std::string dummy_help_text = s.str();
boost::replace_all(dummy_help_text, "--", "");
boost::replace_all(dummy_help_text, "arg", "     ");

std::cout << dummy_help_text << std::endl;

:

Operands:
  file1                 A pathname of the first file to be compared. If file1
                        is '-', the standard input shall be used.
  file2                 A pathname of the second file to be compared. If file2
                        is '-', the standard input shall be used.

, , , options. - , , .

, .

( API Boost.Program_Options, "" . fooobar.com/questions/365168/... , , 3 .)

+1

All Articles