I am using boost add_options() like this way:
("setclient", po::value<std::string>(), "<awsipaddress>[:awsport[:mystring]] Change the settings.")
This works well if I use:
./mycli —setclient 192.68.1.1:8888:ui
I want to allow special character like
[ and ]. The usage will be like
./mycli --setclient [192.68.1.1]:8888:ui
Try out: I changed this
("setclient", po::value<std::string>(), "<awsipaddress>[:awsport[:mystring]] Change the settings.”)
to
("setclient", po::value<std::string>(), "<[awsipaddress]:>[:awsport[:mystring]] Change the settings.")
But doesn’t work for me
Mentioned in the problem statement my try out
The edited code is a descriptive text only, just intended to be printed as usage instructions. It doesn't encode a grammar or anything. So any changes to it don't matter.
The commenter has a good point that might get you half-way: the
[]characters will often be special to the shell you're using. Wrap them in quotes e.g.:or
Now, to actually support the additional characters you need to have a parser that expects the
[and]characters added. If that's not yet the case, please update with a self-contained example (that correctly handles the old syntax) and we can tell you what is missing.Live Demo
With the simplest form:
Live On Coliru
Prints
BONUS: Ipv6?
Guessing that you MIGHT require
[]to allow ipv6 addresses (as they contain:characters), have a look at What is the nicest way to parse this in C++?, and a new live demo integrating that in your program-options:Live On Coliru
Printing for various test inputs:
BONUS 2: Converting Inside Program-Options
Live On Coliru