Question

I have a console application in c++ using boost program_options.

I have a parameter named --list-timezones

now I want to use it like that

either

myapp --list-timezones

which gives me all available timzones

or

myapp --list-timezones AT

which gives me onle the timezones for Austria

My options inializitation is the following

options.add_options()
    ("date-format,d", po::value<string>()->value_name("<Formatstring>")->default_value("%Y-%m-%d %H:%M:%S","\"%Y-%m-%d %H:%M:%S\""),"Format-string for input or output\ne.g. \"%Y-%m-%d %H:%M:%S\"")
    ("input-format,i", po::value<string>()->value_name("<Representation>")->default_value("HEX"),"HEX    hex value\nBIN    binary value\nDEC    decimal value")
    ("output-format,o", po::value<string>()->value_name("<Representation>")->default_value("HEX"),"HEX    hex Value\nBIN    binary value\nDEC    decimal value")
    ("to,t", po::value<string>()->value_name("<Date-Format>"),"CHROME\nMAC\nUNIX\nUNIX_ms\nWin64\nWinCookie\nWinOle\nWinFiletime\nDOS\nHFS\nHFS+")
    ("from,f", po::value<string>()->value_name("<Date-Format>")/*->default_value("UNKNOWN")*/,"CHROME\nMAC\nUNIX\nUNIX_ms\nWin64\nWinCookie\nWinOle\nWinFiletime\nDOS\nHFS\nHFS+\nUNKNOWN")
    ("timezone,z", po::value<string>()->value_name("<Time-Zone>")->default_value("UTC"),"e.g \"Europe/Vienna\"\n execute '--list-timezones ALL' to see all available timezones")
    ("list-timezones,l", po::value<string>()->value_name("<ISO-3166 Country Code>"), "List all available timezones\nyou can filter with ISO3166 country code(e.g AT for Austria)")
    ("value,v", po::value<string>()->value_name("<value>"), "Input Value")
    ("swap-bytes,s", "Swap bytes of result")
    ;

Any ideas how I can handle that?

If I use --list-timezones without an parameter I get an Exception

Thanks florian

Était-ce utile?

La solution

You want this:

("list-timezones,l", po::value<string>()->implicit_value("")->value_name(...

Then you'll be able to give an argument or not. If none is given, the string value will be empty, which seems like a reasonable sentinel value in your case.

Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top