36 parseArgs( ignore_non_options ) ;
44 parseArgs( ignore_non_options ) ;
52 parseArgs( ignore_non_options ) ;
61 parseArgs( ignore_non_options ) ;
70 m_args =
Arg( args_in ) ;
71 parseArgs( ignore_non_options ) ;
75void G::GetOpt::parseArgs( std::size_t ignore_non_options )
78 new_args.insert( new_args.begin() , m_args.v(0U) ) ;
79 m_args =
Arg( new_args ) ;
88 if( std::find( blocklist.begin() , blocklist.end() , path.
extension() ) != blocklist.end() )
90 m_args.removeAt( n ) ;
91 addOptionsFromFile( path ) ;
102 std::string filename = m_args.v( n ) ;
103 m_args.removeAt( n ) ;
105 if( !filename.empty() )
107 if( !varkey.empty() && !varvalue.empty() && filename.find(varkey) == 0 )
109 addOptionsFromFile( filename ) ;
127 return m_spec.list() ;
147 return m_map.contains( m_spec.lookup(c) ) ;
153 return m_map.contains( name ) ;
159 return m_map.count( name ) ;
166 G_ASSERT( contains(c) ) ;
167 return value( m_spec.lookup(c) , default_ ) ;
173 return m_map.value( name , default_ ) ;
183 return !m_errors.empty() ;
188 showErrors( stream , m_args.prefix() +
": error" ) ;
191void G::GetOpt::showErrors( std::ostream & stream ,
const std::string & prefix_1 ,
const std::string & prefix_2 )
const
193 for(
const auto & error : m_errors )
195 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
Exposes the map of option-values.
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 std::vector< Option > & options() const
Exposes the list of option specification objects.
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.
std::string value(std::string_view option_name, std::string_view default_={}) const
Returns the value for the option identified by its long-form name.
static StringArray readOptionsFromFile(const Path &)
Reads options from file as a list of strings like "--foo=bar".
bool hasErrors() const
Returns true if there are errors.
std::size_t count(std::string_view option_name) const
Returns the option's repeat count.
void addOptionsFromFile(std::size_t n=1U, const std::string &varkey={}, const std::string &varvalue={})
Adds options from the config file named by the n'th non-option command-line argument (zero-based and ...
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.
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, std::function< std::string(const std::string &, bool)> callback_fn={})
Parses the given command-line arguments into the value map and/or error list defined by the construct...
static StringArray read(const G::Path &, std::size_t limit=1000U)
Reads options from file as a list of strings like "--foo=bar".
A class to assemble a list of command-line options and provide access by name.
A Path object represents a file system path.
std::string extension() const
Returns the path's basename extension, ie.
static bool replace(std::string &s, std::string_view from, std::string_view to, std::size_t *pos_p=nullptr)
A std::string_view overload.
std::vector< std::string > StringArray
A std::vector of std::strings.