1 #ifndef _CMDLINE_HXX_ 2 #define _CMDLINE_HXX_ 3 4 #include "defs.hxx" 5 6 //--------------------------------- 7 /** Simple command line abstraction 8 */ 9 10 class CommandLine 11 { 12 public: 13 14 //################################ 15 // Creation 16 //################################ 17 18 19 CommandLine(size_t argc, char* argv[], const std::string& ArgPrefix = std::string("-")); 20 21 22 //################################ 23 // Query 24 //################################ 25 26 27 /** Return the argument count 28 */ 29 size_t get_arg_count() const; 30 31 /** Return an argument by index 32 This method doesn't skip argument 33 names if any, so if the second 34 argument is an argument name the 35 function nevertheless returns it. 36 37 @precond 0 <= Index < GetArgumentCount 38 39 @throws std::out_of_range exception 40 if the given index is to high 41 */ 42 std::string get_arg(size_t Index) const; 43 44 /** Returns all argument name found in the 45 command line. An argument will be identified 46 by a specified prefix. The standard prefix 47 is '-'. 48 If there are no argument names the returned 49 container is empty. 50 */ 51 StringListPtr_t get_arg_names() const; 52 53 /** Returns an argument by name. If there are 54 duplicate argument names in the command line, 55 the first one wins. 56 Argument name an the argument value must be separated 57 by spaces. If the argument value starts with an 58 argument prefix use quotes else the return value is 59 an empty string because the value will be interpreted 60 as the next argument name. 61 If an argument value contains spaces use quotes. 62 63 @precond GetArgumentNames() -> has element ArgumentName 64 65 @throws std::invalid_argument exception 66 if the specified argument could not be 67 found 68 */ 69 std::string get_arg(const std::string& ArgumentName) const; 70 71 72 //################################ 73 // Command 74 //################################ 75 76 77 /** Set the prefix used to identify arguments in 78 the command line. 79 80 @precond prefix is not empty 81 82 @throws std::invalid_argument exception if 83 the prefix is empty 84 */ 85 void set_arg_prefix(const std::string& Prefix); 86 87 private: 88 89 /** Returns whether a given argument is an argument name 90 */ 91 bool is_arg_name(const std::string& Argument) const; 92 93 private: 94 size_t m_argc; 95 char** m_argv; 96 std::string m_argprefix; 97 98 // prevent copy and assignment 99 private: 100 CommandLine(const CommandLine&); 101 CommandLine& operator=(const CommandLine&); 102 }; 103 104 #endif 105