CommandLine: using cl::Positional with enum


I’ve been trying to code through CommandLine the options I want my tool accepts, but I find find quite impossible to achieve robustly what I need .

Look, I want the tool accepts a list of arguments in a particular order. For this goal, I know the cl::Positional flag. But, the problem is that the first argument must be one of a set of options. In my case, only the three next commands are possible:
myTool option1
myTool option2 arg1 arg2
myTool option3 arg1

and I don’t want a different order is possible, for instance, this is not permitted:
myTool arg2 option2 arg1

So, I thought about using an enum for this first argument:

enum OptLevel{
option1, option2, option3
cl::opt OptionsLevel(cl::Positional, cl::desc(“Choose one of these options:”),
clEnumVal(option1, “…”),
clEnumVal(option2, “…”),
clEnumVal(option3, “…”),

After that, the rest of arguments are also particular of the option selected as the first argument, i.e, the rest of arguments are related with the first one. So I thought I could independently parse these arguments with:

cl::liststd::string Argv (cl::ConsumeAfter, cl::desc(“…”));

But, doing this when I run:

myTool option1 file.cpp –

I got the next error:
“error - this positional option will never be matched, because it does not Require a value and a cl::ConsumeAfter option is active!”

So, I modify “OptionsLevelOptionsLevel” including the cl::Required flagThe error is now:
“option: does not allow a value! option1 specified.
option: must be specified at least once!
option: must be specified at least once!
option: must be specified at least once!”

Then, I decided to use cl::PositionalEatsArgs instead of cl::ConsumeAfter. Then, this is the result:
“option: does not allow a value! option1 specified.”

But, this time, the program continues. However, if I run “myTool option3 arg1 file.cpp --” it gives me a different problem:
“warning: …/build/arg1: ‘linker’ input unused
error: unable to handle compilation, expected exactly one compiler job in ’ '”

But the program still goes on.

Is there a way to accomplish what I have explained? I don’t want those errors and warnings.



You might try asking the llvm-dev mailing list too since the command line library is a part of LLVM.

In any case, if I understand your description it sounds like you're trying to implement a 'subcommand'-like interface (like svn, git, etc). I'm not sure how I'd do that with the command-line library but I suspect it'll require some extension to support this kind of thing.