I am using Boost.program_options to parse commandlines for my implementation of POSIX utilities. As a simple example, take cmp
.
Now I would like to have an extra argument --help
which shows a description of all 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 is unable to show the file1
and file2
options' description. I can of course add them to options
, but this would add at least two unwanted arguments [-]-file{1,2}
, which I really do not want. I just want this output for --help
(without obviously hardcoding it):
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.
Is there any way to achieve this without hacking around the library? I'd think this is pretty basic stuff, but I can't find it in any of the tutorials.
UPDATE For everyone's benefit, I submitted a feature request for this, in a hopefully backwards compatible way.