1 /************************************************************** 2 * 3 * Licensed to the Apache Software Foundation (ASF) under one 4 * or more contributor license agreements. See the NOTICE file 5 * distributed with this work for additional information 6 * regarding copyright ownership. The ASF licenses this file 7 * to you under the Apache License, Version 2.0 (the 8 * "License"); you may not use this file except in compliance 9 * with the License. You may obtain a copy of the License at 10 * 11 * http://www.apache.org/licenses/LICENSE-2.0 12 * 13 * Unless required by applicable law or agreed to in writing, 14 * software distributed under the License is distributed on an 15 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY 16 * KIND, either express or implied. See the License for the 17 * specific language governing permissions and limitations 18 * under the License. 19 * 20 *************************************************************/ 21 22 23 24 #ifndef _CMDLINE_HXX_ 25 #define _CMDLINE_HXX_ 26 27 #include "defs.hxx" 28 29 //--------------------------------- 30 /** Simple command line abstraction 31 */ 32 33 class CommandLine 34 { 35 public: 36 37 //################################ 38 // Creation 39 //################################ 40 41 42 CommandLine(size_t argc, char* argv[], const std::string& ArgPrefix = std::string("-")); 43 44 45 //################################ 46 // Query 47 //################################ 48 49 50 /** Return the argument count 51 */ 52 size_t get_arg_count() const; 53 54 /** Return an argument by index 55 This method doesn't skip argument 56 names if any, so if the second 57 argument is an argument name the 58 function nevertheless returns it. 59 60 @precond 0 <= Index < GetArgumentCount 61 62 @throws std::out_of_range exception 63 if the given index is to high 64 */ 65 std::string get_arg(size_t Index) const; 66 67 /** Returns all argument name found in the 68 command line. An argument will be identified 69 by a specified prefix. The standard prefix 70 is '-'. 71 If there are no argument names the returned 72 container is empty. 73 */ 74 StringListPtr_t get_arg_names() const; 75 76 /** Returns an argument by name. If there are 77 duplicate argument names in the command line, 78 the first one wins. 79 Argument name and the argument value must be separated 80 by spaces. If the argument value starts with an 81 argument prefix use quotes else the return value is 82 an empty string because the value will be interpreted 83 as the next argument name. 84 If an argument value contains spaces use quotes. 85 86 @precond GetArgumentNames() -> has element ArgumentName 87 88 @throws std::invalid_argument exception 89 if the specified argument could not be 90 found 91 */ 92 std::string get_arg(const std::string& ArgumentName) const; 93 94 95 //################################ 96 // Command 97 //################################ 98 99 100 /** Set the prefix used to identify arguments in 101 the command line. 102 103 @precond prefix is not empty 104 105 @throws std::invalid_argument exception if 106 the prefix is empty 107 */ 108 void set_arg_prefix(const std::string& Prefix); 109 110 private: 111 112 /** Returns whether a given argument is an argument name 113 */ 114 bool is_arg_name(const std::string& Argument) const; 115 116 private: 117 size_t m_argc; 118 char** m_argv; 119 std::string m_argprefix; 120 121 // prevent copy and assignment 122 private: 123 CommandLine(const CommandLine&); 124 CommandLine& operator=(const CommandLine&); 125 }; 126 127 #endif 128 129 /* vim: set noet sw=4 ts=4: */ 130