40 m_errors(m_errors_ignored)
47 for( ; i < args_in.size() ; i++ )
49 const std::string & arg = args_in.at(i) ;
57 if( isAnOptionSet(arg) )
59 for(
size_t n = 1U ; n < arg.length() ; n++ )
60 processOptionOn( arg.at(n) ) ;
62 else if( isOldOption(arg) )
65 if( m_spec.valued(c) && (i+1U) >= args_in.size() )
67 else if( m_spec.valued(c) )
68 processOption( c , args_in.at(++i) ) ;
70 processOptionOn( c ) ;
72 else if( isNewOption(arg) )
74 std::string name = arg.substr( 2U ) ;
75 std::string::size_type pos_eq = eqPos(name) ;
76 bool has_eq = pos_eq != std::string::npos ;
77 std::string key = has_eq ? name.substr(0U,pos_eq) : name ;
79 processOptionOn( key ) ;
81 processOptionOff( key ) ;
83 processOption( key , eqValue(name,pos_eq) ,
false ) ;
84 else if( m_spec.valued(name) && (i+1U) >= args_in.size() )
85 errorNoValue( name ) ;
86 else if( m_spec.valued(name) )
87 processOption( name , args_in.at(++i) , true ) ;
89 processOptionOn( name ) ;
99 void G::OptionParser::processOptionOn(
const std::string & name )
101 if( !m_spec.valid(name) )
102 errorUnknownOption( name ) ;
103 else if( m_spec.valued(name) )
104 errorNoValue( name ) ;
105 else if( haveSeenOff(name) )
106 errorConflict( name ) ;
111 void G::OptionParser::processOptionOff(
const std::string & name )
113 if( !m_spec.valid(name) )
114 errorUnknownOption( name ) ;
115 else if( m_spec.valued(name) )
116 errorNoValue( name ) ;
117 else if( haveSeenOn(name) )
118 errorConflict( name ) ;
123 void G::OptionParser::processOption(
const std::string & name ,
const std::string & value ,
bool fail_if_dubious_value )
125 if( !m_spec.valid(name) )
126 errorUnknownOption( name ) ;
127 else if( !value.empty() && value[0] ==
'-' && fail_if_dubious_value )
128 errorDubiousValue( name , value ) ;
129 else if( !m_spec.valued(name) )
130 errorExtraValue( name ) ;
131 else if( haveSeen(name) && !m_spec.multivalued(name) )
132 errorDuplicate( name ) ;
134 m_map.insert( OptionMap::value_type(name,OptionValue(value)) ) ;
137 void G::OptionParser::processOptionOn(
char c )
139 std::string name = m_spec.lookup( c ) ;
140 if( !m_spec.valid(name) )
141 errorUnknownOption( c ) ;
142 else if( m_spec.valued(name) )
144 else if( haveSeenOff(name) )
145 errorConflict( name ) ;
150 void G::OptionParser::processOption(
char c ,
const std::string & value )
152 std::string name = m_spec.lookup( c ) ;
153 if( !m_spec.valid(name) )
154 errorUnknownOption( c ) ;
155 else if( !m_spec.valued(name) )
156 errorExtraValue( name ) ;
157 else if( haveSeen(name) && !m_spec.multivalued(c) )
158 errorDuplicate( c ) ;
160 m_map.insert( OptionMap::value_type(name,OptionValue(value)) ) ;
163 std::string::size_type G::OptionParser::eqPos(
const std::string & s )
165 std::string::size_type p = s.find_first_not_of(
"abcdefghijklmnopqrstuvwxyz0123456789-_") ;
166 return p != std::string::npos && s.at(p) ==
'=' ? p : std::string::npos ;
169 std::string G::OptionParser::eqValue(
const std::string & s , std::string::size_type pos )
171 return (pos+1U) == s.length() ? std::string() : s.substr(pos+1U) ;
174 bool G::OptionParser::isOldOption(
const std::string & arg )
177 ( arg.length() > 1U && arg.at(0U) ==
'-' ) &&
178 ! isNewOption( arg ) ;
181 bool G::OptionParser::isNewOption(
const std::string & arg )
183 return arg.length() > 2U && arg.at(0U) ==
'-' && arg.at(1U) ==
'-' ;
186 bool G::OptionParser::isAnOptionSet(
const std::string & arg )
188 return isOldOption(arg) && arg.length() > 2U ;
191 void G::OptionParser::errorDubiousValue(
const std::string & name ,
const std::string & value )
193 m_errors.push_back( std::string(
"use of \"--")+name+
" "+value+
"\" is probably a mistake, or try \"--"+name+
"="+value+
"\" instead" ) ;
196 void G::OptionParser::errorDuplicate(
char c )
198 m_errors.push_back( std::string(
"duplicate use of \"-") + std::string(1U,c) +
"\"" ) ;
201 void G::OptionParser::errorDuplicate(
const std::string & name )
203 m_errors.push_back( std::string(
"duplicate use of \"--") + name +
"\"" ) ;
206 void G::OptionParser::errorExtraValue(
char c )
208 m_errors.push_back( std::string(
"cannot give a value with \"-") + std::string(1U,c) +
"\"" ) ;
211 void G::OptionParser::errorExtraValue(
const std::string & name )
213 m_errors.push_back( std::string(
"cannot give a value with \"--") + name +
"\"" ) ;
216 void G::OptionParser::errorNoValue(
char c )
218 m_errors.push_back( std::string(
"no value supplied for -") + std::string(1U,c) ) ;
221 void G::OptionParser::errorNoValue(
const std::string & name )
223 m_errors.push_back( std::string(
"no value supplied for \"--") + name +
"\"" ) ;
226 void G::OptionParser::errorUnknownOption(
char c )
228 m_errors.push_back( std::string(
"invalid option: \"-") + std::string(1U,c) +
"\"" ) ;
231 void G::OptionParser::errorUnknownOption(
const std::string & name )
233 m_errors.push_back( std::string(
"invalid option: \"--") + name +
"\"" ) ;
236 void G::OptionParser::errorConflict(
const std::string & name )
238 m_errors.push_back( std::string(
"conflicting values: \"--") + name +
"\"" ) ;
241 bool G::OptionParser::haveSeenOn(
const std::string & name )
const
243 OptionMap::const_iterator p = m_map.find( name ) ;
244 return p != m_map.end() && !(*p).second.is_off() ;
247 bool G::OptionParser::haveSeenOff(
const std::string & name )
const
249 OptionMap::const_iterator p = m_map.find( name ) ;
250 return p != m_map.end() && (*p).second.is_off() ;
253 bool G::OptionParser::haveSeen(
const std::string & name )
const
255 return m_map.find(name) != m_map.end() ;
static bool isPositive(const std::string &)
Returns true if the string has a positive meaning, such as "1", "true", "yes".
std::vector< std::string > StringArray
A std::vector of std::strings.
size_t parse(const StringArray &args, size_t start_position=1U)
Parses the given command-line arguments into the value map and/or error list defined by the construct...
static OptionValue on()
A factory function for an unvalued option-enabled option.
OptionParser(const Options &spec, OptionMap &values_out, StringArray &errors_out)
Constructor.
A map-like container for command-line options and their values.
static OptionValue off()
A factory function for an unvalued option-disabled option.
static bool isNegative(const std::string &)
Returns true if the string has a negative meaning, such as "0", "false", "no".
A class to represent allowed command-line options and to provide command-line usage text...