I have a Visual Studio 2008 C++ application using boost v1.45.0 program-options.
I would like to be able to parse a command-line option that looks like this: foo.exe -x 1,2, 4-7
such that it produces a std::vector< int >
with the values [1, 2, 4, 5, 6, 7]. So, I've written a custom validator:
typedef std::vector< int > IDList;
void validate( boost::any& v, const std::vector< std::string >& tokens, IDList*, int )
{
// Never gets here
}
int _tmain( int argc, _TCHAR* argv[] )
{
IDList test_case_ids;
po::options_description desc( "Foo options" );
desc.add_options()
("id,x", po::value< IDList >(), "Specify a single ID or a range of IDs as shown in the following command line: foo.exe -x10,12, 15-20")
;
po::variables_map vm;
try
{
po::store( po::parse_command_line( argc, argv, desc ), vm );
po::notify( vm );
}
catch( const std::exception& e)
{
std::cerr << e.what() << std::endl;
std::cout << desc << std::endl;
return 1;
}
return 0;
}
But, I never get to my custom validator code. I always get an exception in parse_command_line
with the message: in option 'id': invalid option value
.
What do I need to do to make this work as desired?
Thanks, PaulH
The typedef
std::vector<int>
as aboost::program_options::value_semantic
does not work the way you desire because avector
has a special meaning to the program options library:which means a description like so
is coalesced into a single
std::vector<int>
given the following command linethe result would be a
std::vector
with four elements. You need to define a specific type to use the custom validator,struct IDList
is the correct approach.