Open lmoellendorf opened 7 years ago
@lmoellendorf How did you get the long value? I can't except by converting from string (std::stoi), even when using your syntax. See here #62
@isometriq This is a shortened snippet from my code:
int main ( int argc, char *argv[] )
{
std::map<std::string, docopt::value> args
= docopt::docopt ( USAGE,
{ argv + 1, argv + argc },
true, // show help if requested
VERSION_STRING );
struct ctx *ctx;
long option_value = 0;
for ( auto const &arg : args ) {
if ( 0 == arg.first.compare ( "--address" )
&& arg.second
/**
* "isLong() does not work as it is expected"
* https://github.com/docopt/docopt.cpp/issues/62
*/
&& arg.second.isString() ) {
try {
option_value = arg.second.asLong();
if ( ( ret = set_address ( ctx, option_value ) ) ) {
std::cerr << "Error: Setting address failed!" << std::endl;
} else {
std::cout << "Address set to: " << option_value << std::endl;
}
} catch ( std::exception &e ) {
std::cerr <<
"An exception occurred while parsing --address parameter. "
//<< e.what()
<< std::endl;
ret = -1;
}
}
}
}
It sounds like you want three states: { not included, included with no param (use default), included with param } .. which isn't something that is standard POSIX syntax as far as I know.
I think you would be better off saying what the default would be in text but not having it specified.
arg.first.compare ( "--always_set" )
will always be true andarg.second.asLong()
will be 10, even if the option--always_set
has not been passed.What I would like to achieve instead is:
mycli
->arg.first.compare ( "--always_set" )
is falsemycli --always_set=2
->arg.first.compare ( "--always_set" )
is true andarg.second.asLong()
is 2mycli --always_set
->arg.first.compare ( "--always_set" )
is true andarg.second.asLong()
is 10. Actually this gives an error, although I (tried to) set the value of--always_set
optional using the angular brackets.Is there a way to achieve what I want?