CLI11 icon indicating copy to clipboard operation
CLI11 copied to clipboard

Reading values passed after command option as list of strings

Open lkammath opened this issue 6 years ago • 6 comments

Hi,

I have to write an application that takes raw hex bytes as input

./app -raw 0x05 0x04 0x03 .. some thing like this

I will interpret the data got and parse it to see I have received in the proper hex format

I know that add_option() will take a vector as argument which we can access like arg[0], arg[1] which will be commands and it is number of subcommands

std::vector<std::string> args{};
app.add_option("-r, —raw", args, "Send a RAW data and print response");

Is there a way I can read all the data passed after -raw option as list of strings? and treat arg[0] as the main command instead of treating them as subcommands?

Thanks in advance for the help

Thanks, Lakshmi

lkammath avatar Jul 16 '19 17:07 lkammath

Currently, your example ./app -raw 0x05 0x04 0x03 would produce the vector: ["0x05", "0x04", "0x03"]. Is that not what you want? If you want ["-raw", "0x05", "0x04", "0x03"] instead, that could be done by making the argument a positional argument, that is, give it a name without a dash. app.add_option("raw", args, "Send a RAW data and print response");. I may not understand your question, though.

henryiii avatar Jul 16 '19 17:07 henryiii

so, the second case you explained by making argument as positional argument

arg[0] would point to -raw or raw? and arg[1] onwards i can treat as the input raw bytes and put them to list?

lkammath avatar Jul 16 '19 17:07 lkammath

I need some way to parse a normal command vs a raw command passed

For e.g i have same application accepting another command like

./app -command [some command name] [sub command1]

The same app will also have option like i told above

./app -raw/raw 0x05 0x04 0x03, etc.

I hope i am clear now

Thanks, Lakshmi

lkammath avatar Jul 16 '19 18:07 lkammath

In CLI11, long options must start with a double dash. Subcommands should have no dash. So you can do what you want to rather easily as long as you can drop the dash, that is:

./app command [some command name] [sub command1]
./app raw/raw 0x05 0x04 0x03

Here's an example: https://wandbox.org/permlink/rMGeZwjyGxYtY9Im

Change the "raw" to "command" and you'll see that it's filling in a different "remaining" vector.

henryiii avatar Jul 16 '19 18:07 henryiii

Sorry .. I messed up again

I meant , I want to have two options in my app

./app --command [main command][subcommand1] - This one i have already implemented ./app --raw 0x05 0x04 0x03 - This is the one i am asking suggestion for.. Should I change it to a positional argument like what you answered in the first comment? Then i will lose -- before raw

Thanks, Lakshmi

lkammath avatar Jul 17 '19 04:07 lkammath

std::vector<std::string> args{};
app.add_option("-c, —command", args, "request command")->required();

std::vector<uint8_t> rawCmd;
app.add_option("-r, —raw", rawCmd, "Send a RAW request and print response");

Coding like this throws "Too many positional arguments with unlimited expected args error"

lkammath avatar Jul 17 '19 16:07 lkammath