CLI11 icon indicating copy to clipboard operation
CLI11 copied to clipboard

Two-pass CLI parsing

Open maximumspatium opened this issue 5 years ago • 4 comments

Hello crews,

first of all, thank you very much for bringing us this amazing library!

We're currently working on integrating CLI11 in our Macintosh emulator: https://github.com/dingusdev/dingusppc

There are two mandatory options to specify the precise machine configuration and a BootROM file. Furthermore, we would like to provide the possibility to override default values of some HW properties directly from the command line to avoid configuration files.

In order to implement the latter, we sketched the following two-pass CLI parsing algorithm:

  1. scan the argv to parse the mandatory arguments --machine (HW config) and --rom (BootROM path)
  2. load default values for the specified HW configuration that is a collection of properties (aka "ram_size", "gfxmem", "device" etc)
  3. initialize CLI11 options from property names loaded in step 2, i.e. "gfxmem" --> --gfxmem
  4. second pass over argv to parse all other options defined in step 3

In order to implement the above algorithm, we need a possibility

  • to perform two distinct parsing passes with CLI11: 1st pass - mandatory arguments only, 2nd pass - optional arguments
  • to skip over unrecognized arguments

Is that possible?

P.S.: another (less elegant) solution would be a generic option like --property that can hold an arbitrary string that will be parsed after CLI11 is done...

Thank you in advance!

maximumspatium avatar Sep 03 '20 12:09 maximumspatium

What I have done in similar cases is create two parsing apps. The first handles the outer options and allows extra arguments.
The second then gets dynamically generated after the first is done in a second stage or even in the final_callback of the first.

The second pass sends the results of remaining_for_passthrough into the second generated app for further processing.

Getting help for the second is still a bit of hack but what sort of works is using the footer callback to print out the help for the second generated app potentially based on arguments from the first. Someday I want to figure out a cleaner way of doing that but for now that is the best I can come up with.

phlptp avatar Sep 03 '20 12:09 phlptp

Philip, thank you a lot! Creating two parsing apps sounds reasonable.

How can I get CLI11 to ignore optional arguments during the first stage? For now, I'm getting the following error:

The following argument was not expected: --gfxmem

That's because that option wasn't registered yet.

Getting help for the second is still a bit of hack

I see. I think I can work around this limitation by defining the "help" subcommand, for example:

./dingusppc help machines - lists supported HW configurations
./dingusppc help properties - lists available properties

maximumspatium avatar Sep 03 '20 13:09 maximumspatium

app.allow_extras()

tells CLI11 to just ignore any extra options it encounters and store them in a vector. Then remaining(), or remaining_for_passthrough() will retrieve them.

phlptp avatar Sep 03 '20 13:09 phlptp

app.allow_extras()

That works. Thanks!

maximumspatium avatar Sep 03 '20 13:09 maximumspatium