[cfe-dev] CommandLine: using cl::Positional with enum

Pedro Delgado Perez pedro.delgadoperez at mail.uca.es
Sat May 4 09:43:34 PDT 2013


 
Hi,

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<OptLevel> OptionsLevel(cl::Positional, cl::desc("Choose one of these options:"),
cl::values(
clEnumVal(option1, "..."),
clEnumVal(option2, "..."),
clEnumVal(option3, "..."),
 clEnumValEnd),
);


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::list<std::string>  Argv (cl::ConsumeAfter, cl::desc("<program arguments>..."));

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. 

Thanks,

Pedro.



-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.llvm.org/pipermail/cfe-dev/attachments/20130504/575498b3/attachment.html>


More information about the cfe-dev mailing list