bfgroup / Lyra

A simple to use, composable, command line parser for C++ 11 and beyond
https://bfgroup.github.io/Lyra/
Boost Software License 1.0
483 stars 58 forks source link

Display default value in the options help #96

Closed DarkTyger closed 1 month ago

DarkTyger commented 1 month ago

We have a number of command-line options whose default values are mandated by a formal specification. We'd like to ensure that the default value is displayed when the user displays the help. For example:

int main(int argc, const char ** argv)
{
    return lyra::main()
        (lyra::opt(lyra::val(0), "x")["-x"]) // <1>
        (lyra::opt(lyra::val(0), "y")["-y"])
        (lyra::arg(lyra::val(5), "z")) // <2>
        (argc, argv, [](lyra::main & m)
        {
            std::cout << (int(m["-x"]) + int(m["-y"]))*int(m["z"]) << "\n";
            return 0;
        });
}

When run with --help, I would anticipate seeing something along the lines of:

progname
   -x (default 0)
      description x
   -y (default 0)
      description y
   -z (default 5)
      description z

or perhaps:

progname
   -x 
      description x (default: 0).
   -y 
      description y (default: 0).
   -z 
      description z (default: 5).

Is this doable without hard-coding the default values twice? (Once in the lyra::val and once in the help text?)

grafikrobot commented 1 month ago

It's not possible currently, unfortunately. But I can see that it would be trivial to implement it at the cost of some up-front calc of a default string. It needs to be up-front, as opposed to the current way which generates the help strings on-demand, as the initial value (aka the default) can change as args are parsed. Which would change the perceived default. anway.. I'll put in on the todo list for the next thing I do. As it's a great idea!