41 parseArgs( ignore_non_options ) ;
49 parseArgs( ignore_non_options ) ;
57 parseArgs( ignore_non_options ) ;
66 parseArgs( ignore_non_options ) ;
75 m_args =
Arg( args_in ) ;
76 parseArgs( ignore_non_options ) ;
80void G::GetOpt::parseArgs( std::size_t ignore_non_options )
83 new_args.insert( new_args.begin() , m_args.v(0U) ) ;
84 m_args =
Arg( new_args ) ;
93 if( std::find( blocklist.begin() , blocklist.end() , path.
extension() ) != blocklist.end() )
95 m_args.removeAt( n ) ;
96 addOptionsFromFile( path ) ;
107 std::string filename = m_args.v( n ) ;
108 m_args.removeAt( n ) ;
110 if( !filename.empty() )
112 if( !varkey.empty() && !varvalue.empty() && filename.find(varkey) == 0 )
114 addOptionsFromFile( filename ) ;
132 return m_spec.list() ;
152 return m_map.contains( m_spec.lookup(c) ) ;
158 return m_map.contains( name ) ;
164 return m_map.count( name ) ;
171 G_ASSERT( contains(c) ) ;
172 return value( m_spec.lookup(c) , default_ ) ;
178 return m_map.value( name , default_ ) ;
193 return !m_errors.empty() ;
198 showErrors( stream , m_args.prefix() +
": error" ) ;
201void G::GetOpt::showErrors( std::ostream & stream ,
const std::string & prefix_1 ,
const std::string & prefix_2 )
const
203 for(
const auto & error : m_errors )
205 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 the map of option-values.
std::size_t count(string_view option_name) const
Returns the option's repeat count.
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.
std::string value(string_view option_name, string_view default_={}) const
Returns the value for the option identified by its long-form name.
const std::vector< Option > & options() const
Returns 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.
G::optional< std::string > optional(string_view option_name) const
Returns an optional value 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.
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, string_view from, string_view to, std::size_t *pos_p=nullptr)
A string_view overload.
A class like c++17's std::string_view.
std::vector< std::string > StringArray
A std::vector of std::strings.