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