|
Boost : |
Subject: [boost] command line parameters parser
From: Denis Shevchenko (shev.denis_at_[hidden])
Date: 2010-03-04 02:18:49
Hi! Sorry for my English, I am from Russia.
Is there any interest in a library which parse command line parameters
and call correspond functions/functors?
Features:
- check type of parameter,
- check "value semantic" of parameter,
- set necessity of parameter,
- maybe set default value for parameter,
- some other settings.
Example:
void help() {
std::cout << "This is my help info!" << std::endl;
::exit( 0 ); // Exit after printing help info.
}
struct config_file {
void operator()( const std::string& file ) {
// Some work with obtaining file path.
}
};
void limit( const std::string& some_limit_value ) {
// int limit = boost::lexical_cast< int >( some_limit_value );
// ...
}
int main( int argc, char* argv[] ) {
typedef command_line_parameter_parser clp_parser;
clp_parser parser;
parser.add_parameter( "-h", "--help", help ); // Short and full
names, in Unix tradition.
// In this case we define parameter with one name and value (it is
must be valid path);
// Without check_value() obtaining path will not be checked.
parser.add_parameter( "--config", config_file,
clp_parser::with_value ).check_value( clp_parser::path );
parser.add_parameter( "-l", "--limit", limit ).check_type(
clp_parser::integer ); // Is integer?
parser.set_value_separator( ':' );
// "--config:/home/file.conf" instead default "--config=/home/file.conf"
try {
parser.parse( argc, argv );
} catch ( const std::exception& exc ) {
std::cerr << exc.what() << std::endl;
}
...
return 0;
}
Denis
Boost list run by bdawes at acm.org, gregod at cs.rpi.edu, cpdaniel at pacbell.net, john at johnmaddock.co.uk