Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Reading values passed after command option as list of strings #297

Closed
lkammath opened this issue Jul 16, 2019 · 7 comments
Closed

Reading values passed after command option as list of strings #297

lkammath opened this issue Jul 16, 2019 · 7 comments

Comments

@lkammath
Copy link

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

@henryiii
Copy link
Collaborator

henryiii commented Jul 16, 2019

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.

@lkammath
Copy link
Author

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
Copy link
Author

lkammath commented Jul 16, 2019

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

@henryiii
Copy link
Collaborator

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.

@lkammath
Copy link
Author

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
Copy link
Author

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"

@phlptp
Copy link
Collaborator

phlptp commented Oct 8, 2024

I think is resolved, so I am closing it, if there is some other issue feel free to make a new one

@phlptp phlptp closed this as completed Oct 8, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants