[cli-users] How to collect all non-option arguments?

Boris Kolpackov boris at codesynthesis.com
Mon Nov 18 09:11:29 EST 2019


Justin Huang <yohuang at nvidia.com> writes:

> Supposing we allow users to input non-option arguments in any order
> like this: -opt1 opt1_val non_opt1 -opt2 opt2_value non_opt2.
> 
> There are 2 option arguments: -opt1 opt1_val, -opt2 opt2_val, and 2
> non-option arguments: non_opt1, non_opt2.

You can make this work with a bit of effort. Essentially, you will
need to parse the first chunk of options until the first argument,
save it, then parse the next chunk of options, and so on.

We do this in the build2 toolchain, for example:

https://github.com/build2/bdep/blob/master/bdep/bdep.cxx#L161-L196

Note that this technique requires that you generate the parse()
function with the --generate-parse CLI option.



More information about the cli-users mailing list