32 parseSpec( spec , sep_major , sep_minor , escape ) ;
35void G::Options::parseSpec(
const std::string & spec ,
char sep_major ,
char sep_minor ,
char escape )
39 spec_items.reserve( 40U ) ;
43 for(
auto & spec_item : spec_items )
46 if( spec_item.empty() ) continue ;
48 inner_parts.reserve( 10U ) ;
51 if( inner_parts.size() < 7U )
52 throw InvalidSpecification( std::string(1U,
'[').append(
G::Str::join(
",",inner_parts)).append(1U,
']') ) ;
54 std::string short_form = inner_parts[0] ;
55 char c = short_form.empty() ?
'\0' : short_form[0U] ;
56 unsigned int level =
Str::toUInt( inner_parts[6U] ) ;
58 Option::Multiplicity multiplicity =
Option::decode( inner_parts[4U] ) ;
59 if( multiplicity == Option::Multiplicity::error )
60 throw InvalidSpecification( std::string(1U,
'[').append(
G::Str::join(
",",inner_parts)).append(1U,
']') ) ;
62 Option opt( c , inner_parts[1U] , inner_parts[2U] , inner_parts[3U] ,
63 multiplicity , inner_parts[5U] , level ) ;
65 addOption( opt , sep_minor , escape ) ;
70 const char * more , Option::Multiplicity m ,
const char * argname ,
71 unsigned int level ,
unsigned int main_tag ,
unsigned int tag_bits )
73 options.
add(
Option(c,name,
G::gettext(text),more,m,argname,level,main_tag,main_tag|tag_bits) ) ;
78 addOption( opt , sep , escape ) ;
81void G::Options::addOption(
Option opt ,
char sep ,
char escape )
91 sub_parts.reserve( 2U ) ;
93 if( sub_parts.size() > 2U || ( sub_parts.size()==2U && !opt.description_extra.empty() ) )
94 throw InvalidSpecification() ;
95 if( sub_parts.size() == 2U )
97 opt.description = sub_parts[0] ;
98 opt.description_extra = sub_parts[1] ;
102 auto range = std::equal_range( m_list.begin() , m_list.end() , opt ,
103 [](
const Option & a,
const Option & b){ return a.name < b.name ; } ) ;
105 if( range.first != range.second )
106 throw InvalidSpecification(
"duplication" ) ;
108 m_list.insert( range.first , opt ) ;
113 return defaulting( lookup(c) ) ;
118 const Option * p = find( name ) ;
119 return p ? p->defaulting() : false ;
124 return valued( lookup(c) ) ;
129 const Option * p = find( name ) ;
130 return p ? p->valued() : false ;
135 return valid(name) && !valued(name) ;
140 return multivalued( lookup(c) ) ;
145 const Option * p = find( name ) ;
146 return p ? p->multivalued() : false ;
151 const Option * p = find( name ) ;
152 return p ? p->visible({level_exact?level:1U,level}) :
false ;
157 return visible( name , 99U ,
false ) ;
162 return find(name) != nullptr ;
167 for(
auto p = m_list.begin() ; c !=
'\0' && p != m_list.end() ; ++p )
177 for(
const auto & option : m_list )
179 if( option.name == name )
A class to assemble a list of command-line options and provide access by name.
std::string lookup(char c) const
Converts from short-form option character to the corresponding long-form name.
bool unvalued(const std::string &) const
Returns true if the given option name is valid and takes no value.
static void add(Options &, char c, const char *name, const char *text, const char *more, Option::Multiplicity m, const char *argname, unsigned int level, unsigned int main_tag_bit, unsigned int tag_bits=0U)
A convenience function that constructs an Option object with the trailing arguments and then calls ad...
bool visible(const std::string &name, unsigned int level, bool level_exact=false) const
Returns true if the option is visible at the given level.
bool defaulting(const std::string &) const
Returns true if the given long-form single-valued() option can optionally have no explicit value,...
bool valid(const std::string &) const
Returns true if the long-form option name is valid.
bool multivalued(char) const
Returns true if the short-form option can have multiple values.
bool valued(char) const
Returns true if the given short-form option takes a value, Returns true if the short-form option char...
const Option * find(const std::string &) const
Returns a pointer to the option with a matching long-form name.
Options()
Default constructor for no options.
const std::vector< Option > & list() const
Returns the sorted list of option structures.
static std::string join(std::string_view sep, const StringArray &strings)
Concatenates an array of strings with separators.
static void splitIntoFields(std::string_view in, StringArray &out, char sep, char escape='\0', bool remove_escapes=true)
Splits the string into fields.
static unsigned int toUInt(std::string_view s)
Converts string 's' to an unsigned int.
const char * gettext(const char *) noexcept
Returns the message translation in the current locale's codeset, eg.
std::vector< std::string > StringArray
A std::vector of std::strings.
A structure representing a G::Options command-line option.
static Multiplicity decode(const std::string &)
Decodes a multiplicity string into its enumeration.