37 parseArgs( ignore_non_options ) ;
44 parseArgs( ignore_non_options ) ;
51 parseArgs( ignore_non_options ) ;
58 parseArgs( ignore_non_options ) ;
65 m_args =
Arg( args_in ) ;
66 parseArgs( ignore_non_options ) ;
69void G::GetOpt::parseArgs( std::size_t ignore_non_options )
72 new_args.insert( new_args.begin() , m_args.v(0U) ) ;
73 m_args =
Arg( new_args ) ;
80 std::string filename = m_args.v( n ) ;
81 m_args.removeAt( n ) ;
83 if( !filename.empty() )
85 if( !varkey.empty() && !varvalue.empty() && filename.find(varkey) == 0 )
87 addOptionsFromFile( filename ) ;
92G::StringArray G::GetOpt::optionsFromFile(
const Options & spec ,
const Path & filename )
95 StringMap map = MapFile(filename,
"config").map() ;
96 for(
const auto & map_item : map )
98 const std::string & key = map_item.first ;
99 const std::string & value = map_item.second ;
100 if( spec.valued(key) )
101 result.push_back( std::string(
"--").append(key).append(1U,
'=').append(value) ) ;
103 result.push_back( std::string(
"--").append(key) ) ;
110 OptionParser::parse( optionsFromFile(m_spec,filename) , m_spec , m_map , &m_errors , 0U ) ;
130 return m_map.contains( m_spec.lookup(c) ) ;
135 return m_map.contains( name ) ;
140 return m_map.count( name ) ;
145 G_ASSERT( contains(c) ) ;
146 return value( m_spec.lookup(c) , default_ ) ;
149std::string
G::GetOpt::value(
const std::string & name ,
const std::string & default_ )
const
151 return m_map.value( name , default_ ) ;
161 return !m_errors.empty() ;
166 showErrors( stream , m_args.prefix() +
": error" ) ;
169void G::GetOpt::showErrors( std::ostream & stream ,
const std::string & prefix_1 ,
const std::string & prefix_2 )
const
171 for(
const auto & error : m_errors )
173 stream << prefix_1 << prefix_2 << error << std::endl ;
A class which holds a represention of the argc/argv command line array, and supports simple command-l...
const OptionMap & map() const
Returns a reference to the OptionMap sub-object.
void reload(const StringArray &arg, std::size_t ignore_non_options=0U)
Reinitialises the object with the given command-line arguments.
bool contains(char option_letter) const
Returns true if the command-line contains the option identified by its short-form letter.
const Options & options() const
Returns a reference to the option specification sub-object.
GetOpt(const Arg &arg, const std::string &spec, std::size_t ignore_non_options=0U)
Constructor taking a Arg object and a G::Options specification string.
void addOptionsFromFile(size_type n=1U, const std::string &varkey=std::string(), const std::string &varvalue=std::string())
Adds options from the config file named by the n'th non-option command-line argument (zero-based and ...
bool hasErrors() const
Returns true if there are errors.
std::size_t count(const std::string &option_name) const
Returns the option's repeat count.
Arg args() const
Returns the G::Arg command-line, excluding options.
void showErrors(std::ostream &stream, const std::string &prefix_1, const std::string &prefix_2=std::string(": ")) const
A convenience function which streams out each errorList() item to the given stream,...
StringArray errorList() const
Returns the list of errors.
std::string value(const std::string &option_name, const std::string &default_=std::string()) const
Returns the value for the option identified by its long-form name.
A multimap-like container for command-line options and their values.
StringArray parse(const StringArray &args_in, std::size_t start_position=1U, std::size_t ignore_non_options=0U)
Parses the given command-line arguments into the value map and/or error list defined by the construct...
A class to represent allowed command-line options and to provide command-line usage text.
A Path object represents a file system path.
static bool replace(std::string &s, const std::string &from, const std::string &to, std::size_t *pos_p=nullptr)
Replaces 'from' with 'to', starting at offset '*pos_p'.
std::vector< std::string > StringArray
A std::vector of std::strings.
std::map< std::string, std::string > StringMap
A std::map of std::strings.