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 CSV_COMMANDLINE_HXX 25 #define CSV_COMMANDLINE_HXX 26 27 #include <cosv/string.hxx> 28 29 30 31 32 namespace csv 33 { 34 35 /** Does the standards in handling command line parameters. 36 37 This class provides a default behaviour this way: 38 39 * Each option can have several forms 40 41 like: "-f" and "--file" 42 43 which are identified by having the same id. 44 The option id is used when calling ->do_HandleOption(). 45 46 * For each recognized option together with its parameters 47 ->do_HandleOption() is called. 48 49 * For the first unrecognized argument ->do_HandleFreeArgument() is 50 called. 51 After the first unrecognized argument, ->do_HandleFreeArgument() 52 is called for all remaining arguments. 53 54 @howtoderive 55 - Overwrite ->do_HandleOption() to act on all known options. 56 Overwrite ->do_HandleFreeArgument() to act on additional 57 arguments not connected to an option. 58 */ 59 class CommandLine 60 { 61 public: 62 // LIFECYCLE 63 virtual ~CommandLine() {} 64 65 // OPERATIONS 66 bool Interpret( 67 int argc, 68 char * argv[] ); 69 // INQUIRY 70 const StringVector & 71 Arguments() const; 72 bool IsOk() const; 73 74 protected: 75 CommandLine(); 76 void Add_Option( 77 intt i_id, 78 String i_text ); 79 void Set_Error(); 80 81 private: 82 // public for use by struct commandline.cxx-anonymous::FindOptionByText; 83 struct OptionDescription 84 { 85 intt nId; 86 String sText; 87 88 OptionDescription( 89 intt i_id, 90 String i_text ); 91 }; private: 92 93 struct FindOptionByText; 94 95 typedef std::vector<OptionDescription> OptionList; 96 typedef std::vector<StringVector::const_iterator> StringCIteratorList; 97 typedef std::vector<intt> OptionIdList; 98 99 // Locals 100 void Get_Arguments( 101 int argc, 102 char * argv[] ); 103 intt Find_Option( 104 const String & i_text ) const; 105 bool Store_Argument( 106 const String & i_arg ); 107 void Find_OptionPoints(); 108 void Handle_FreeArguments( 109 StringVector::const_iterator 110 i_begin, 111 StringVector::const_iterator 112 i_end ); 113 114 // Helpers for options included via file 115 bool Try2Include_Options( 116 const String & i_optionsFile ); 117 bool Include_Options( 118 const String & i_optionsFile ); 119 bool Load_Options( 120 StreamStr & o_text, 121 const String & i_optionsFile ); 122 123 /** Handles an option found in the command line. 124 Needs to be overwritten. 125 126 @return 127 The first argument within the range 128 i_next_argument .. i_commandLine_end that does not belong as a 129 parameter to the handled option. 130 */ 131 virtual StringVector::const_iterator 132 do_HandleOption( 133 intt i_id, 134 StringVector::const_iterator 135 i_paramsBegin, 136 StringVector::const_iterator 137 i_paramsEnd ) = 0; 138 /** Handles arguments on the command line that do not belong to 139 an option. 140 */ 141 virtual void do_HandleFreeArgument( 142 const String & i_argument ) = 0; 143 // DATA 144 OptionList aOptions; 145 146 /// Used during and after ->GetArguments() 147 StringVector aCommandLine; 148 StringCIteratorList aOptionPoints; 149 OptionIdList aOptionIds; 150 bool bIsOk; 151 }; 152 153 154 inline const StringVector & 155 CommandLine::Arguments() const 156 { 157 return aCommandLine; 158 } 159 160 inline bool 161 CommandLine::IsOk() const 162 { 163 return bIsOk; 164 } 165 166 inline void 167 CommandLine::Set_Error() 168 { 169 bIsOk = false; 170 } 171 172 173 174 175 } // namespace csv 176 #endif 177