"Fossies" - the Fresh Open Source Software Archive 
Member "ansifilter-2.18-x64/src/arg_parser.h" (30 Jan 2021, 3563 Bytes) of package /windows/misc/ansifilter-2.18-x64.zip:
As a special service "Fossies" has tried to format the requested source page into HTML format using (guessed) C and C++ source code syntax highlighting (style:
standard) with prefixed line numbers and
code folding option.
Alternatively you can here
view or
download the uninterpreted source code file.
1 /* Arg_parser - A POSIX/GNU command line argument parser.
2 Copyright (C) 2006, 2007, 2008 Antonio Diaz Diaz.
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 /* Arg_parser reads the arguments in `argv' and creates a number of
19 option codes, option arguments and non-option arguments.
20
21 In case of error, `error' returns a non-empty error message.
22
23 `options' is an array of `struct Option' terminated by an element
24 containing a code which is zero. A null name means a short-only
25 option. A code value outside the unsigned char range means a
26 long-only option.
27
28 Arg_parser normally makes it appear as if all the option arguments
29 were specified before all the non-option arguments for the purposes
30 of parsing, even if the user of your program intermixed option and
31 non-option arguments. If you want the arguments in the exact order
32 the user typed them, call `Arg_parser' with `in_order' = true.
33
34 The argument `--' terminates all options; any following arguments are
35 treated as non-option arguments, even if they begin with a hyphen.
36
37 The syntax for optional option arguments is `-<short_option><argument>'
38 (without whitespace), or `--<long_option>=<argument>'.
39 */
40
41 class Arg_parser
42 {
43 public:
44 enum Has_arg { no, yes, maybe };
45
46 struct Option {
47 int code; // Short option letter or code ( code != 0 )
48 const char * name; // Long option name (maybe null)
49 Has_arg has_arg;
50 };
51
52 private:
53 struct Record {
54 int code;
55 std::string argument;
56 Record( const int c = 0 ) : code( c ) {}
57 };
58
59 std::string _error;
60 std::vector< Record > data;
61
62 bool parse_long_option( const char * const opt, const char * const arg,
63 const Option options[], int & argind ) throw();
64 bool parse_short_option( const char * const opt, const char * const arg,
65 const Option options[], int & argind ) throw();
66
67 public:
68 Arg_parser( const int argc, const char * const argv[],
69 const Option options[], const bool in_order = false ) throw();
70
71 // Restricted constructor. Parses a single token and argument (if any)
72 Arg_parser( const char * const opt, const char * const arg,
73 const Option options[] ) throw();
74
75 const std::string & error() const throw()
76 {
77 return _error;
78 }
79
80 // The number of arguments parsed (may be different from argc)
81 int arguments() const throw()
82 {
83 return data.size();
84 }
85
86 // If code( i ) is 0, argument( i ) is a non-option.
87 // Else argument( i ) is the option's argument (or empty).
88 int code( const int i ) const throw()
89 {
90 if( i >= 0 && i < arguments() ) return data[i].code;
91 else return 0;
92 }
93
94 const std::string & argument( const int i ) const throw()
95 {
96 if( i >= 0 && i < arguments() ) return data[i].argument;
97 else return _error;
98 }
99 };