11 #ifndef __CommandLine_h
12 #define __CommandLine_h
14 #include "Reference.h"
28 struct arg_traits<const T&> { typedef T unref; };
30 namespace CommandLine {
32 typedef std::pair<std::string,std::string> Help;
42 virtual bool matches ( int code) const = 0;
45 virtual void handle ( const std::string& arg) = 0;
94 void set_short_name ( char c) {
98 void set_long_name ( const std::string& s) { long_name = s; }
99 void set_type ( const std::string& s) { type = s; }
100 void set_help ( const std::string& s) { help = s; }
101 void set_long_help ( const std::string& s) { long_help = s; }
102 void set_has_arg ( int h) { has_arg = h; }
105 void set_notification ( bool& f) { handled = &f; };
127 { set_argument (a); short_name = short_alias;}
130 { set_argument (a); long_name = long_alias; }
133 void handle ( const std::string& arg) { argument->handle (arg); }
153 void handle ( const std::string& arg) { value = fromstring<T>(arg); }
169 : values (_values) { has_arg = required_argument; }
174 values.push_back( fromstring<T>(arg) );
198 template< class C, typename M, typename P>
217 { has_arg = required_argument; }
257 template< class C, typename M>
280 template< class C, typename M>
288 class Heading : public Item
294 Heading ( const std::string& _text) { text = _text; }
297 bool matches ( int) const { return false; }
300 void handle ( const std::string&) { }
303 Help get_help () const { return Help(text, ""); }
327 { return help_header; }
346 virtual void parse ( int argc, char* const *argv);
361 return add_value (value, name, &Argument::set_short_name, type);
366 Argument* add (T& value, const std::string& name, const char* type = 0)
368 return add_value (value, name, &Argument::set_long_name, type);
373 char name, const char* type = 0)
375 return add_parser (action, name, &Argument::set_short_name, type);
380 const std::string& name, const char* type = 0)
382 return add_parser (action, name, &Argument::set_long_name, type);
386 template< class C, class B, typename T>
388 const char* type = 0)
390 return add_attribute (ptr, method,
391 &fromstring< typename arg_traits<T>::unref >,
392 name, &Argument::set_short_name, type);
396 template< class C, class B, typename T>
397 Argument* add (C* ptr, void (B::*method)(T), const std::string& name,
398 const char* type = 0)
400 return add_attribute (ptr, method,
401 &fromstring< typename arg_traits<T>::unref >,
402 name, &Argument::set_long_name, type);
406 template< class C, typename M, typename P>
409 return add_attribute (ptr, method, parse,
410 name, &Argument::set_short_name, type);
414 template< class C, typename M, typename P>
418 return add_attribute (ptr, method, parse,
419 name, &Argument::set_long_name, type);
423 template< class C, class B>
426 return add_action (ptr, method, name, &Argument::set_short_name);
430 template< class C, class B>
431 Argument* add (C* ptr, void (B::*method)(), const std::string& name)
433 return add_action (ptr, method, name, &Argument::set_long_name);
437 template< class C, class B, class T>
440 return add_action (ptr, method, name, &Argument::set_short_name, arg);
444 template< class C, class B, class T>
445 Argument* add (C* ptr, void (B::*method)(T), const std::string& name, T arg)
447 return add_action (ptr, method, name, &Argument::set_long_name, arg);
454 void add ( const std::string&);
457 void help ( const std::string&);
464 std::vector< Reference::To<Item> > item;
466 std::string version_info;
469 std::string help_header;
470 std::string help_footer;
474 template< typename T, typename N, typename S>
478 (argument->*set) (name);
480 argument->set_type (type);
481 item.push_back (argument);
485 template< class C, typename M, typename P, typename N, typename S>
486 Argument* add_attribute (C* ptr, M method, P parse, N name, S set,
490 (argument->*set) (name);
492 argument->set_type (type);
493 item.push_back (argument);
497 template< typename N, typename S>
498 Argument* add_parser ( Functor< void( const std::string&) >& action,
499 N name, S set, const char* type)
503 Argument* argument = new Parser (action);
504 (argument->*set) (name);
506 argument->set_type (type);
507 item.push_back (argument);
511 template< class C, typename M, typename N, typename S>
512 Argument* add_action (C* ptr, M method, N name, S set)
514 Argument* argument = new Action (ptr, method);
516 (argument->*set) (name);
517 item.push_back (argument);
521 template< class C, typename M, typename N, typename S, typename T>
522 Argument* add_action (C* ptr, M method, N name, S set, T arg)
524 Argument* argument = new UnaryAction<T> (ptr, method, arg);
526 (argument->*set) (name);
527 item.push_back (argument);
bool matches(int c) const Return true if key matches. Definition: CommandLine.h:108
Functor< void() > action The action to be taken. Definition: CommandLine.h:252
A command line Action. Definition: CommandLine.h:247
bool & value Reference to the value to be set. Definition: CommandLine.h:186
Value(std::vector< T > &_values) Default constructor. Definition: CommandLine.h:168
bool * handled bool handled notification Definition: CommandLine.h:85
UnaryAction(C *instance, M method, T arg) Default constructor. Definition: CommandLine.h:281
Action(C *instance, M method) Default constructor. Definition: CommandLine.h:258
std::string short_name Single-character name of value. Definition: CommandLine.h:64
Argument * add(Functor< void(const std::string &) > action, const std::string &name, const char *type=0) Add a parse action with only a long string name. Definition: CommandLine.h:379
T argument The argument to be passed. Definition: CommandLine.h:275
std::string long_name Long name of value. Definition: CommandLine.h:67
virtual void set_help_footer(const std::string &s) Set the help footer. Definition: CommandLine.h:330
virtual bool matches(int code) const =0 Return true if code matches.
std::string type Type of value. Definition: CommandLine.h:70
Argument * find(const std::string &name) Find the named Argument. Definition: CommandLine.C:209
M method Pointer to the method (unary member function) Definition: CommandLine.h:207
void handle(const std::string &arg) Handle the argument. Definition: CommandLine.h:172
A convenient exception handling class. Definition: Error.h:54
T & value Reference to the value to be set. Definition: CommandLine.h:144
A single item in a command line menu. Definition: CommandLine.h:35
Attribute(C *_instance, M _method, P _parse) Default constructor. Definition: CommandLine.h:215
virtual void add_version() Add the –version command line option. Definition: CommandLine.C:70
std::vector< T > & values Reference to the value to be set. Definition: CommandLine.h:163
virtual Help get_help() const =0 Return two columns of help text.
A function that can parse the string. Definition: CommandLine.h:225
void help(const std::string &) Print help and exit. Definition: CommandLine.C:221
virtual void set_help_header(const std::string &s) Set the help header (activates -h,–help) Definition: CommandLine.h:322
Argument * add(C *ptr, void(B::*method)(T), const std::string &name, T arg) Add an UnaryAction with only a long string name. Definition: CommandLine.h:445
void version() Print version and exit. Definition: CommandLine.C:304
virtual void handle(const std::string &arg)=0 Handle the argument.
Template class manages Reference::Able objects. Definition: Reference.h:74
Functor< void(const std::string &) > action The action to be taken. Definition: CommandLine.h:230
A command line corresponding to a class attribute. Definition: CommandLine.h:199
std::string help Brief description of value. Definition: CommandLine.h:73
void handle(const std::string &arg) Handle the argument. Definition: CommandLine.h:153
C * instance Pointer to the instance. Definition: CommandLine.h:204
Value(T &_value) Default constructor. Definition: CommandLine.h:149
void set_handled(bool f) Set the handled flag. Definition: CommandLine.h:104
Alias to a single command line argument. Definition: CommandLine.h:116
Menu() Construct with two default options: –help and –version. Definition: CommandLine.C:54
std::string get_help_header() const Get the help header. Definition: CommandLine.h:326
A command line Action with a single argument. Definition: CommandLine.h:267
Argument * add(C *ptr, void(B::*method)(), char name) Add an Action with only a single letter name. Definition: CommandLine.h:424
virtual void add_help() Add the –help command line option. Definition: CommandLine.C:59
void handle(const std::string &arg) Handle the argument. Definition: CommandLine.h:133
Functor< void(T) > action The action to be taken. Definition: CommandLine.h:272
Argument * add(C *ptr, void(B::*method)(T), const std::string &name, const char *type=0) Add an Attribute with only a long string name. Definition: CommandLine.h:397
Enable a single template that works for both func(T) and func(const T&) Definition: CommandLine.h:25
std::string long_help Detailed description of value. Definition: CommandLine.h:76
P parse Pointer to the parser (unary) Definition: CommandLine.h:210
Manages Reference::To references to the instance. Definition: ReferenceAble.h:40
void set_handled(bool f) Set the handled flag. Definition: CommandLine.h:132
void handle(const std::string &arg) Handle the argument. Definition: CommandLine.h:239
void handle(const std::string &) Handle the argument. Definition: CommandLine.h:194
Stores keyword-value pairs. Definition: Alias.h:21
Argument * add(Functor< void(const std::string &) > action, char name, const char *type=0) Add an parse action with only a single letter name. Definition: CommandLine.h:372
virtual void add(Item *) Add an item to the menu. Definition: CommandLine.C:88
virtual void set_version(const std::string &s) Set the version information string (activates -i,–version) Definition: CommandLine.h:339
std::string get_help_footer() const Get the help footer. Definition: CommandLine.h:333
Help get_help() const Return two columns of help text. Definition: CommandLine.C:38
virtual void set_handled(bool) Set the handled flag. Definition: CommandLine.h:48
Argument * add(C *ptr, M method, P parse, char name, const char *type) Add an Attribute with only a single letter name. Definition: CommandLine.h:407
Help get_help() const Return two columns of help text. Definition: CommandLine.C:18
Implements an adaptable function object in compliance with the STL. Definition: Functor.h:39
Argument * add_value(T &value, N name, S set, const char *type) Add a Value with only a long string name. Definition: CommandLine.h:475
Argument * add(C *ptr, void(B::*method)(), const std::string &name) Add an Action with only a long string name. Definition: CommandLine.h:431
A command line value. Definition: CommandLine.h:139
Argument * add(T &value, const std::string &name, const char *type=0) Add a Value with only a long string name. Definition: CommandLine.h:366
Argument The complex phase of the basis. Definition: Conventions.h:30
Argument * add(T &value, char name, const char *type=0) Add a Value with only a single letter name. Definition: CommandLine.h:359
virtual void remove(Item *) Remove an item from the menu. Definition: CommandLine.C:103
void handle(const std::string &) Handle the argument. Definition: CommandLine.h:285
virtual int process_error(int code, char *const *argv) Process any option parsing error and return a new code. Definition: CommandLine.C:196
int val Code assigned to this Argument by Menu class. Definition: CommandLine.h:82
A command line menu. Definition: CommandLine.h:312
Argument * add(C *ptr, void(B::*method)(T), char name, const char *type=0) Add an Attribute with only a single letter name. Definition: CommandLine.h:387
int has_arg The has_arg attribute used by getopt_long. Definition: CommandLine.h:79
A single command line argument. Definition: CommandLine.h:58
Parser(Functor< void(const std::string &) > &_action) Default constructor. Definition: CommandLine.h:235
void handle(const std::string &) Handle the argument. Definition: CommandLine.h:262
Argument * add(C *ptr, void(B::*method)(T), char name, T arg) Add an UnaryAction with only a single letter name. Definition: CommandLine.h:438
Argument * add(C *ptr, M method, P parse, const std::string &name, const char *type) Add an Attribute with only a long string name. Definition: CommandLine.h:415
virtual void parse(int argc, char *const *argv) Parse the command line. Definition: CommandLine.C:120
Value(bool &_value) Default constructor. Definition: CommandLine.h:191
void handle(const std::string &arg) Handle the argument. Definition: CommandLine.h:220
Generated using doxygen 1.8.17
|