30bool G::Arg::m_first = true ;
31std::string G::Arg::m_v0 ;
32std::string G::Arg::m_cwd ;
36 G_ASSERT( argc > 0 ) ;
37 G_ASSERT( argv !=
nullptr ) ;
38 for(
int i = 0 ; i < argc ; i++ )
39 m_array.push_back( argv[i] ) ;
43 m_v0 = std::string( argv[0] ) ;
60 if( proc_exe.empty() )
throw Exception(
"cannot determine the path of this executable" ) ;
62 m_array.push_back( proc_exe ) ;
63 parseImp( command_line_tail ) ;
68 G_ASSERT( !command_line.empty() ) ;
70 parseImp( command_line ) ;
75 auto p = m_array.begin() ;
76 m_array.erase( ++p , m_array.end() ) ;
77 parseImp( command_line_tail ) ;
88 while( !result.empty() && shift-- )
89 result.erase( result.begin() ) ;
93bool G::Arg::contains(
const std::string & option , std::size_t option_args ,
bool cs )
const
95 return find( cs , option , option_args ,
nullptr ) != 0U ;
100 return find(
true , option , 0U ,
nullptr ) ;
103std::size_t G::Arg::find(
bool cs ,
const std::string & option , std::size_t option_args ,
104 std::size_t * index_p )
const
106 std::size_t count = 0U ;
107 for( std::size_t i = 1U ; i < m_array.size() ; i++ )
109 if( strmatch(cs,option,m_array[i]) && (i+option_args) < m_array.size() )
112 if( index_p !=
nullptr )
122 for( std::size_t i = 1U ; i < m_array.size() ; i++ )
132bool G::Arg::strmatch(
bool cs ,
const std::string & s1 ,
const std::string & s2 )
140 const bool found = find(
true , option , option_args , &i ) ;
142 removeAt( i , option_args ) ;
149 if( option_index > 0U && (option_index+option_args) < m_array.size() )
151 value = v( option_index + (option_args?1U:0U) , std::string() ) ;
152 auto p = m_array.begin() ;
153 for( std::size_t i = 0U ; i < option_index ; i++ ) ++p ;
154 p = m_array.erase( p ) ;
155 for( std::size_t i = 0U ; i < option_args && p != m_array.end() ; i++ )
156 p = m_array.erase( p ) ;
161std::size_t
G::Arg::index(
const std::string & option , std::size_t option_args ,
162 std::size_t default_ )
const
165 const bool found = find(
true , option , option_args , &i ) ;
166 return found ? i : default_ ;
171 return m_array.size() ;
176 G_ASSERT( i < m_array.size() ) ;
177 return m_array.at(i) ;
180std::string
G::Arg::v( std::size_t i ,
const std::string & default_ )
const
182 return i < m_array.size() ? m_array.at(i) : default_ ;
187 G_ASSERT( !m_array.empty() ) ;
188 Path path( m_array.at(0U) ) ;
194 const char * exe = argv[0] ;
195 const char * p1 = std::strrchr( exe ,
'/' ) ;
196 const char * p2 = std::strrchr( exe ,
'\\' ) ;
197 p1 = p1 ? (p1+1U) : exe ;
198 p2 = p2 ? (p2+1U) : exe ;
199 return p1 > p2 ? p1 : p2 ;
202void G::Arg::parseImp(
const std::string & command_line )
206 const char esc =
'\\' ;
207 const char qq =
'\"' ;
215 if( proc_exe.empty() && ( m_v0.empty() || ( m_cwd.empty() &&
Path(m_v0).isRelative() ) ) )
219 throw Exception(
"cannot determine the absolute path of the current executable" ,
220 G::is_windows() ?
"" :
"try mounting procfs" ) ;
222 return std::string() ;
224 else if( proc_exe.empty() &&
Path(m_v0).isRelative() )
228 else if( proc_exe.empty() )
std::size_t c() const
Returns the number of tokens in the command line, including the program name.
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.
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.
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 void splitIntoTokens(const std::string &in, StringArray &out, string_view ws, char esc='\0')
Splits the string into 'ws'-delimited tokens.
static std::string upper(const std::string &s)
Returns a copy of 's' in which all Latin-1 lower-case characters have been replaced by upper-case cha...
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 bool headMatch(const std::string &in, const std::string &head)
Returns true if the string has the given start (or head is empty).
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'.
A class template like c++17's std::basic_string_view.
std::vector< std::string > StringArray
A std::vector of std::strings.