34 #ifndef __INCLUDE_MIST_OPTIONS__
35 #define __INCLUDE_MIST_OPTIONS__
38 #ifndef __INCLUDE_MIST_CONF_H__
39 #include "../config/mist_conf.h"
63 class options :
public std::vector< std::string >
66 typedef std::vector< std::string >
base;
79 arg(
const std::string &aname =
"",
const std::string &text =
"",
const std::string &val =
"",
bool hasValue =
false )
80 : name( aname ), comment( text ), value( val ), has_value( hasValue ), found( false )
85 arg(
const arg &p ) : name( p.name ), comment( p.comment ), value( p.value ), has_value( p.has_value ), found( p.found )
90 std::string header_text;
91 std::string footer_text;
92 std::string program_name;
93 std::vector< std::string > option_list;
94 std::map< std::string, arg > args;
98 options(
const std::string header =
"",
const std::string footer =
"" ) : header_text( header ), footer_text( footer )
109 bool __isset__(
const std::string &name, std::string &val )
const
111 std::map< std::string, arg >::const_iterator ite = args.find( name );
112 if( ite != args.end( ) )
114 const arg &a = ite->second;
126 void add(
const std::string &name,
const std::string &comment )
128 option_list.push_back( name );
129 args[ name ] = arg( name, comment,
"",
false );
133 void add(
const std::string &name,
const std::string &comment,
const std::string &default_value )
135 option_list.push_back( name );
136 args[ name ] = arg( name, comment, default_value,
true );
140 void add(
const std::string &name,
const std::string &comment,
int default_value )
143 sprintf( buff,
"%d", default_value );
144 option_list.push_back( name );
145 args[ name ] = arg( name, comment, buff,
true );
149 void add(
const std::string &name,
const std::string &comment,
double default_value )
152 sprintf( buff,
"%lf", default_value );
153 option_list.push_back( name );
154 args[ name ] = arg( name, comment, buff,
true );
158 bool isset(
const std::string &name )
const
160 std::map< std::string, arg >::const_iterator ite = args.find( name );
161 if( ite != args.end( ) )
163 const arg &a = ite->second;
173 const std::string get_string(
const std::string &name )
const
176 if( __isset__( name, val ) )
187 int get_int(
const std::string &name )
const
190 if( __isset__( name, val ) )
192 return( atoi( val.c_str( ) ) );
201 double get_double(
const std::string &name )
const
204 if( __isset__( name, val ) )
206 return( atof( val.c_str( ) ) );
216 void show_help( )
const
218 std::stringstream sout;
221 sout << header_text << std::endl;
224 for(
size_t i = 0 ; i < option_list.size( ) ; i++ )
226 if( max_len < option_list[ i ].size( ) )
228 max_len = option_list[ i ].size( );
232 for(
size_t i = 0 ; i < option_list.size( ) ; i++ )
234 std::map< std::string, arg >::const_iterator ite = args.find( option_list[ i ] );
235 if( ite != args.end( ) )
237 const arg &a = ite->second;
242 for(
size_t i = a.name.size( ) ; i < max_len + 3 ; i++ )
251 sout <<
"[" << a.value <<
"]";
262 std::cerr <<
"Failed to parse options." << std::endl;
267 sout << footer_text << std::endl;
269 puts( sout.str( ).c_str( ) );
274 bool parse_args(
int argc,
char *argv[] )
276 program_name = argv[ 0 ];
280 for(
int i = 1 ; i < argc ; i++ )
282 std::string option = argv[ i ];
284 if( option[ 0 ] !=
'-' )
286 base::push_back( option );
290 option = option.substr( 1 );
292 std::map< std::string, arg >::iterator ite = args.find( option );
293 if( ite != args.end( ) )
295 arg &a = ite->second;
300 if( i + 1 < argc && argv[ i + 1 ][ 0 ] !=
'-' )
302 a.value = argv[ i + 1 ];
305 else if( i + 1 < argc && strlen( argv[ i + 1 ] ) > 1 && argv[ i + 1 ][ 0 ] ==
'-' &&
'0' <= argv[ i + 1 ][ 1 ] && argv[ i + 1 ][ 1 ] <=
'9' )
307 a.value = argv[ i + 1 ];
312 std::cerr <<
"Option \"" << option <<
"\" requires values." << std::endl;
319 std::cerr <<
"\"" << option <<
"\" is an unknown option." << std::endl;
327 std::cerr << std::endl;
340 #endif // __INCLUDE_MIST_OPTIONS__