E-MailRelay

garg.h
Go to the documentation of this file.
1//
2// Copyright (C) 2001-2021 Graeme Walker <graeme_walker@users.sourceforge.net>
3//
4// This program is free software: you can redistribute it and/or modify
5// it under the terms of the GNU General Public License as published by
6// the Free Software Foundation, either version 3 of the License, or
7// (at your option) any later version.
8//
9// This program is distributed in the hope that it will be useful,
10// but WITHOUT ANY WARRANTY; without even the implied warranty of
11// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12// GNU General Public License for more details.
13//
14// You should have received a copy of the GNU General Public License
15// along with this program. If not, see <http://www.gnu.org/licenses/>.
16// ===
17///
18/// \file garg.h
19///
20
21#ifndef G_ARG_H
22#define G_ARG_H
23
24#include "gdef.h"
25#include "gstrings.h"
26#include <vector>
27#include <string>
28
29 namespace G
30{
31 class Arg ;
32}
33
34//| \class G::Arg
35/// A class which holds a represention of the argc/argv command line array,
36/// and supports simple command-line parsing.
37///
38/// A copy of argv[0] is squirrelled away and made accessible via a static
39/// method.
40///
41/// \see G::GetOpt
42///
43 class G::Arg
44{
45 public:
46 Arg( int argc , char ** argv ) ;
47 ///< Constructor taking argc/argv. Should not be used in a shared
48 ///< object or dll.
49
50 explicit Arg( const G::StringArray & ) ;
51 ///< Constructor taking an array of command-line arguments. The
52 ///< program name in the first position is expected but may be
53 ///< ignored.
54
55 Arg() ;
56 ///< Default constructor. Initialise with parse().
57
58 void parse( HINSTANCE hinstance , const std::string & command_line_tail ) ;
59 ///< Parses the given command-line tail, splitting it up into
60 ///< an array of tokens. The v(0) value comes from the supplied
61 ///< instance handle.
62
63 void parse( const std::string & command_line ) ;
64 ///< Parses the given command line, splitting it up into an array
65 ///< of tokens. The command-line should start with the v(0) value.
66
67 void reparse( const std::string & command_line_tail ) ;
68 ///< Reinitialises the object with the given command-line tail. The
69 ///< command-line should not contain the program name: the v(0)
70 ///< value and prefix() are unchanged.
71
72 static std::string v0() ;
73 ///< Returns a copy of argv[0] from the first call to the constructor
74 ///< that takes argc/argv. Returns the empty string if that constructor
75 ///< overload has never been used. See also exe().
76
77 static std::string exe( bool do_throw = true ) ;
78 ///< Returns Process::exe() or an absolute path constructed from v0()
79 ///< and possibly using the cwd. Throws on error by default, or
80 ///< optionally returns the empty string.
81 ///< See also v0().
82
83 std::size_t c() const ;
84 ///< Returns the number of tokens in the command line, including the
85 ///< program name.
86
87 std::string v( std::size_t i ) const ;
88 ///< Returns the i'th argument.
89 ///< Precondition: i < c()
90
91 std::string v( std::size_t i , const std::string & default_ ) const ;
92 ///< Returns the i'th argument or the default if out of range.
93
94 std::string prefix() const ;
95 ///< Returns the basename of v(0) without any extension. Typically used
96 ///< as a prefix in error messages.
97
98 static const char * prefix( char ** argv ) noexcept ;
99 ///< An exception-free version of prefix() which can be used in
100 ///< main() outside of the outermost try block.
101
102 bool contains( const std::string & option , std::size_t option_args = 0U , bool case_sensitive = true ) const ;
103 ///< Returns true if the command line contains the given option
104 ///< with enough command line arguments left to satisfy the required
105 ///< number of option arguments. (By convention an option starts
106 ///< with a dash, but that is not required here; it's just a string
107 ///< that is matched against command-line arguments.)
108
109 std::size_t count( const std::string & option ) ;
110 ///< Returns the number of times the given string appears in the
111 ///< list of arguments.
112
113 std::size_t index( const std::string & option , std::size_t option_args = 0U ,
114 std::size_t default_ = 0U ) const ;
115 ///< Returns the index of the given option. Returns zero
116 ///< (or the given default) if not present.
117
118 std::size_t match( const std::string & prefix ) const ;
119 ///< Returns the index of the first argument that matches the
120 ///< given prefix. Returns zero if none.
121
122 bool remove( const std::string & option , std::size_t option_args = 0U ) ;
123 ///< Removes the given option and its arguments. Returns false if
124 ///< the option does not exist.
125
126 std::string removeAt( std::size_t option_index , std::size_t option_args = 0U ) ;
127 ///< Removes the given argument and the following 'option_args' ones.
128 ///< Returns v(option_index+(option_args?1:0),"").
129
130 StringArray array( unsigned int shift = 0U ) const ;
131 ///< Returns the arguments as a string array, with an optional shift.
132 ///< A shift of one will remove the program name.
133
134 private:
135 std::size_t find( bool , const std::string & , std::size_t , std::size_t * ) const ;
136 static bool strmatch( bool , const std::string & , const std::string & ) ;
137 void parseImp( const std::string & ) ;
138
139 private:
140 StringArray m_array ;
141 static bool m_first ;
142 static std::string m_v0 ;
143 static std::string m_cwd ;
144} ;
145
146#endif
G::Arg
A class which holds a represention of the argc/argv command line array, and supports simple command-l...
Definition: garg.h:44
G::Arg::c
std::size_t c() const
Returns the number of tokens in the command line, including the program name.
Definition: garg.cpp:169
G::Arg::exe
static std::string exe(bool do_throw=true)
Returns Process::exe() or an absolute path constructed from v0() and possibly using the cwd.
Definition: garg.cpp:212
G::Arg::match
std::size_t match(const std::string &prefix) const
Returns the index of the first argument that matches the given prefix.
Definition: garg.cpp:120
G::Arg::v
std::string v(std::size_t i) const
Returns the i'th argument.
Definition: garg.cpp:174
G::Arg::index
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.
Definition: garg.cpp:161
G::Arg::removeAt
std::string removeAt(std::size_t option_index, std::size_t option_args=0U)
Removes the given argument and the following 'option_args' ones.
Definition: garg.cpp:146
G::Arg::prefix
std::string prefix() const
Returns the basename of v(0) without any extension.
Definition: garg.cpp:185
G::Arg::contains
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...
Definition: garg.cpp:93
G::Arg::array
StringArray array(unsigned int shift=0U) const
Returns the arguments as a string array, with an optional shift.
Definition: garg.cpp:85
G::Arg::remove
bool remove(const std::string &option, std::size_t option_args=0U)
Removes the given option and its arguments.
Definition: garg.cpp:137
G::Arg::parse
void parse(HINSTANCE hinstance, const std::string &command_line_tail)
Parses the given command-line tail, splitting it up into an array of tokens.
Definition: garg.cpp:57
G::Arg::v0
static std::string v0()
Returns a copy of argv[0] from the first call to the constructor that takes argc/argv.
Definition: garg.cpp:80
G::Arg::count
std::size_t count(const std::string &option)
Returns the number of times the given string appears in the list of arguments.
Definition: garg.cpp:98
G::Arg::Arg
Arg()
Default constructor. Initialise with parse().
G::Arg::reparse
void reparse(const std::string &command_line_tail)
Reinitialises the object with the given command-line tail.
Definition: garg.cpp:73
gstrings.h
G
Low-level classes.
Definition: galign.h:28
G::StringArray
std::vector< std::string > StringArray
A std::vector of std::strings.
Definition: gstrings.h:31