30std::string G::Arg::m_v0 ;
31std::string G::Arg::m_cwd ;
35 G_ASSERT( argc > 0 ) ;
36 G_ASSERT( argv !=
nullptr ) ;
37 for(
int i = 0 ; i < argc ; i++ )
38 m_array.push_back( argv[i] ) ;
40 static bool first = true ;
43 m_v0 = std::string( argv[0] ) ;
61 if( proc_exe.empty() )
throw Exception(
"cannot determine the path of this executable" ) ;
63 m_array.push_back( proc_exe ) ;
64 parseImp( command_line_tail ) ;
70 G_ASSERT( !command_line.empty() ) ;
72 parseImp( command_line ) ;
78 auto p = m_array.begin() ;
79 m_array.erase( ++p , m_array.end() ) ;
80 parseImp( command_line_tail ) ;
94 while( !result.empty() && shift-- )
95 result.erase( result.begin() ) ;
99bool G::Arg::contains(
const std::string & option , std::size_t option_args ,
bool cs )
const
101 return find( cs , option , option_args ,
nullptr ) != 0U ;
107 return find(
true , option , 0U ,
nullptr ) ;
111std::size_t G::Arg::find(
bool cs ,
const std::string & option , std::size_t option_args ,
112 std::size_t * index_p )
const
114 std::size_t count = 0U ;
115 for( std::size_t i = 1U ; i < m_array.size() ; i++ )
117 if( strmatch(cs,option,m_array[i]) && (i+option_args) < m_array.size() )
120 if( index_p !=
nullptr )
130 for( std::size_t i = 1U ; i < m_array.size() ; i++ )
140bool G::Arg::strmatch(
bool cs ,
const std::string & s1 ,
const std::string & s2 )
148 const bool found = find(
true , option , option_args , &i ) != 0U ;
150 removeAt( i , option_args ) ;
156 std::size_t option_args = 1U ;
158 const bool found = find(
true , option , option_args , &i ) != 0U ;
159 return found ? removeAt( i , option_args ) : default_ ;
165 if( option_index > 0U && (option_index+option_args) < m_array.size() )
167 value = v( option_index + (option_args?1U:0U) , std::string() ) ;
168 auto p = m_array.begin() ;
169 for( std::size_t i = 0U ; i < option_index ; i++ ) ++p ;
170 p = m_array.erase( p ) ;
171 for( std::size_t i = 0U ; i < option_args && p != m_array.end() ; i++ )
172 p = m_array.erase( p ) ;
177std::size_t
G::Arg::index(
const std::string & option , std::size_t option_args ,
178 std::size_t default_ )
const
181 const bool found = find(
true , option , option_args , &i ) != 0U ;
182 return found ? i : default_ ;
187 return m_array.size() ;
192 G_ASSERT( i < m_array.size() ) ;
193 return m_array.at(i) ;
196std::string
G::Arg::v( std::size_t i ,
const std::string & default_ )
const
198 return i < m_array.size() ? m_array.at(i) : default_ ;
203 G_ASSERT( !m_array.empty() ) ;
204 Path path( m_array.at(0U) ) ;
210 const char * exe = argv[0] ;
211 const char * p1 = std::strrchr( exe ,
'/' ) ;
212 const char * p2 = std::strrchr( exe ,
'\\' ) ;
213 p1 = p1 ==
nullptr ? exe : (p1+1U) ;
214 p2 = p2 ==
nullptr ? exe : (p2+1U) ;
215 return p1 > p2 ? p1 : p2 ;
218void G::Arg::parseImp(
const std::string & command_line )
222 const char esc =
'\\' ;
223 const char qq =
'\"' ;
231 if( proc_exe.empty() && ( m_v0.empty() || ( m_cwd.empty() &&
Path(m_v0).isRelative() ) ) )
235 throw Exception(
"cannot determine the absolute path of the current executable" ,
236 G::is_windows() ?
"" :
"try mounting procfs" ) ;
240 else if( proc_exe.empty() &&
Path(m_v0).isRelative() )
244 else if( proc_exe.empty() )
257 return m_array.size() <= 1U ? cend() : std::next( m_array.cbegin() ) ;
263 return m_array.cend() ;
std::size_t c() const
Returns the number of tokens in the command line, including the program name.
std::string removeValue(const std::string &option, const std::string &default_={})
Removes the given single-valued option and its value.
static std::string exe(bool do_throw=true)
Returns Process::exe() or an absolute path constructed from v0() and possibly using the cwd.
std::size_t match(const std::string &prefix) const
Returns the index of the first argument that matches the given prefix.
std::string v(std::size_t i) const
Returns the i'th argument.
std::size_t index(const std::string &option, std::size_t option_args=0U, std::size_t default_=0U) const
Returns the index of the given option.
std::string removeAt(std::size_t option_index, std::size_t option_args=0U)
Removes the given argument and the following 'option_args' ones.
StringArray::const_iterator cend() const
Returns the end operator.
std::string prefix() const
Returns the basename of v(0) without any extension.
bool contains(const std::string &option, std::size_t option_args=0U, bool case_sensitive=true) const
Returns true if the command line contains the given option with enough command line arguments left to...
StringArray array(unsigned int shift=0U) const
Returns the arguments as a string array, with an optional shift.
bool remove(const std::string &option, std::size_t option_args=0U)
Removes the given option and its arguments.
void parse(HINSTANCE hinstance, const std::string &command_line_tail)
Parses the given command-line tail, splitting it up into an array of tokens.
static std::string v0()
Returns a copy of argv[0] from the first call to the constructor that takes argc/argv.
StringArray::const_iterator cbegin() const
Returns a begin operator, advanced to exclude argv0.
std::size_t count(const std::string &option)
Returns the number of times the given string appears in the list of arguments.
Arg()
Default constructor. Initialise with parse().
void reparse(const std::string &command_line_tail)
Reinitialises the object with the given command-line tail.
A general-purpose exception class derived from std::exception and containing an error message.
A Path object represents a file system path.
static Path join(const StringArray &parts)
Builds a path from a set of parts.
std::string basename() const
Returns the rightmost part of the path, ignoring "." parts.
Path withoutExtension() const
Returns a path without the basename extension, if any.
Path collapsed() const
Returns the path with "foo/.." and "." parts removed, so far as is possible without changing the mean...
std::string str() const
Returns the path string.
static std::string cwd(bool no_throw=false)
Returns the current working directory.
static std::string exe()
Returns the absolute path of the current executable, independent of the argv array passed to main().
static bool replace(std::string &s, string_view from, string_view to, std::size_t *pos_p=nullptr)
A string_view overload.
static void splitIntoTokens(const std::string &in, StringArray &out, string_view ws, char esc='\0')
Splits the string into 'ws'-delimited tokens.
static std::string dequote(const std::string &, char qq='\"' , char esc = '\\' , string_view ws = Str::ws() , string_view nbws = Str::ws() )
Dequotes a string by removing unescaped quotes and escaping quoted whitespace, so "qq-aaa-esc-qq-bbb-...
static std::string upper(string_view)
Returns a copy of 's' in which all seven-bit lower-case characters have been replaced by upper-case c...
static bool headMatch(const std::string &in, string_view head) noexcept
Returns true if the string has the given start (or head is empty).
A class like c++17's std::string_view.
std::vector< std::string > StringArray
A std::vector of std::strings.