36 G_ASSERT( argc > 0 ) ;
37 G_ASSERT( argv !=
nullptr ) ;
39 for(
int i = 0 ; i < argc ; i++ )
40 m_array.emplace_back( argv[i] ) ;
42 if( m_v0.
empty() && !m_array.empty() )
44 m_v0 =
Path( m_array[0] ) ;
58 throw Exception(
"invalid path for this executable" ) ;
59 parseImp( command_line_tail ) ;
60 m_array.insert( m_array.begin() , argv0.
str() ) ;
66 G_ASSERT( !command_line.empty() ) ;
67 parseImp( command_line ) ;
72 Arg arg( (Windows()) ) ;
73 if( m_v0.empty() && !arg.m_array.empty() )
75 m_v0 =
Path( arg.m_array[0] ) ;
91 while( !result.empty() && shift-- )
92 result.erase( result.begin() ) ;
96bool G::Arg::contains( std::string_view option , std::size_t option_args ,
bool cs )
const
98 return find( cs , option , option_args ,
nullptr ) != 0U ;
104 return find(
true , option , 0U ,
nullptr ) ;
108std::size_t G::Arg::find(
bool cs , std::string_view option , std::size_t option_args ,
109 std::size_t * index_p )
const
111 std::size_t count = 0U ;
112 for( std::size_t i = 1U ; i < m_array.size() ; i++ )
114 if( strmatch(cs,option,m_array[i]) && (i+option_args) < m_array.size() )
117 if( index_p !=
nullptr )
127 for( std::size_t i = 1U ; i < m_array.size() ; i++ )
137bool G::Arg::strmatch(
bool cs , std::string_view s1 , std::string_view s2 )
145 const bool found = find(
true , option , option_args , &i ) != 0U ;
147 removeAt( i , option_args ) ;
153 std::size_t option_args = 1U ;
155 const bool found = find(
true , option , option_args , &i ) != 0U ;
156 return found ? removeAt( i , option_args ) : default_ ;
162 if( option_index > 0U && (option_index+option_args) < m_array.size() )
164 value = v( option_index + (option_args?1U:0U) , std::string() ) ;
165 auto p = m_array.begin() ;
166 for( std::size_t i = 0U ; i < option_index ; i++ ) ++p ;
167 p = m_array.erase( p ) ;
168 for( std::size_t i = 0U ; i < option_args && p != m_array.end() ; i++ )
169 p = m_array.erase( p ) ;
174std::size_t
G::Arg::index( std::string_view option , std::size_t option_args ,
175 std::size_t default_ )
const
178 const bool found = find(
true , option , option_args , &i ) != 0U ;
179 return found ? i : default_ ;
184 return m_array.size() ;
189 G_ASSERT( i < m_array.size() ) ;
190 return m_array.at(i) ;
193std::string
G::Arg::v( std::size_t i ,
const std::string & default_ )
const
195 return i < m_array.size() ? m_array.at(i) : default_ ;
200 G_ASSERT( !m_array.empty() ) ;
201 Path path( m_array.at(0U) ) ;
207 if( argv ==
nullptr )
return "" ;
208 const char * exe = argv[0] ;
209 if( exe ==
nullptr || exe[0] ==
'\0' )
return "" ;
210 const char * p1 = std::strrchr( exe ,
'/' ) ;
211 const char * p2 = std::strrchr( exe ,
'\\' ) ;
212 p1 = p1 ==
nullptr ? exe : (p1+1U) ;
213 p2 = p2 ==
nullptr ? exe : (p2+1U) ;
214 return p1 > p2 ? p1 : p2 ;
217void G::Arg::parseImp(
const std::string & command_line )
219 constexpr std::string_view ws(
" \t" , 2U ) ;
220 constexpr std::string_view nbws(
"\0\0" , 2U ) ;
221 static_assert( ws.size() == nbws.size() ,
"" ) ;
222 const char esc =
'\\' ;
223 const char qq =
'\"' ;
230 return exeImp(
true ) ;
236 return exeImp(
false ) ;
240G::Path G::Arg::exeImp(
bool do_throw )
243 if( !process_exe.
empty() )
247 else if( m_v0.isAbsolute() )
251 else if( !m_cwd.empty() )
257 throw Exception(
"cannot determine the absolute path of the current executable" ,
258 G::is_windows() ?
"" :
"try mounting procfs" ) ;
269 return m_array.size() <= 1U ? cend() : std::next( m_array.cbegin() ) ;
275 return m_array.cend() ;
A class which holds a represention of the argc/argv command line array, and supports simple command-l...
std::size_t c() const
Returns the number of tokens in the command line, including the program name.
static Arg windows()
Factory function for Windows using GetCommandLineW().
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(std::string_view option, std::size_t option_args=0U, std::size_t default_=0U) const
Returns the index of the given option.
Arg(int argc, char **argv)
Constructor taking argc/argv directly from main().
std::size_t count(std::string_view option) const
Returns the number of times the given string appears in the list of arguments.
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 iterator.
std::string prefix() const
Returns the basename of v(0) without any extension.
std::string removeValue(std::string_view option, const std::string &default_={})
Removes the given single-valued option and its value.
StringArray array(unsigned int shift=0U) const
Returns the arguments as a string array, with an optional shift.
bool remove(std::string_view option, std::size_t option_args=0U)
Removes the given option and its arguments.
static Path exe()
Returns Process::exe() or in exceptional circumstances an absolute path constructed from v0() and the...
bool contains(std::string_view 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::const_iterator cbegin() const
Returns a begin iterator, advanced to exclude argv0.
static Path v0()
Returns a copy of argv[0] from the first call to the argc/argv constructor overload or windows().
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.
bool empty() const noexcept
Returns true if the path is empty.
static Path cwd()
Returns the current working directory. Throws on error.
static Path 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, std::string_view ws, char esc='\0')
Splits the string into 'ws'-delimited tokens.
static bool imatch(char, char) noexcept
Returns true if the two characters are the same, ignoring seven-bit case.
static bool headMatch(std::string_view in, std::string_view head) noexcept
Returns true if the string has the given start (or head is empty).
static std::string dequote(const std::string &, char qq='\"' , char esc = '\\' , std::string_view ws = Str::ws() , std::string_view nbws = Str::ws() )
Dequotes a string by removing unescaped quotes and escaping quoted whitespace, so "qq-aaa-esc-qq-bbb-...
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.