-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathArgParser.h
More file actions
98 lines (93 loc) · 4.34 KB
/
ArgParser.h
File metadata and controls
98 lines (93 loc) · 4.34 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
/** @file ArgParser.h
*Contains the ArgParser class definition.
*An ArgParser object is a container for the command line options and operands passed to a program through argv.
*
*Copyright 2015, 2021 by Francisco Cancillo & Luis Cancillo
*<p>
*This file is part of the RXtoRINEX tools.
*<p>
*RXtoRINEX is free software: you can redistribute it and/or modify it under the terms of the GNU General Public License
*as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version.
*RXtoRINEX is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied
*warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
*See the GNU General Public License for more details.
*<p>
*A copy of the GNU General Public License can be found at <http://www.gnu.org/licenses/>.
*
*<p>Ver. |Date |Reason for change
*<p>---------------------------------
*<p>V1.0 |2/2015 |First release
*<p>V1.1 |2/2016 |Minor changes: const string used for messages
*/
#ifndef ARGPARSER_H
#define ARGPARSER_H
#include <string>
#include <forward_list>
#include <vector>
using namespace std;
//@cond DUMMY
//Constant to define error messages
const string MSG_UnknOption = " is an unknown option";
const string MSG_ValueNotSet = " is a string option. It requires a value";
const string MSG_ValueSet = " is a boolean option. A string value cannot be assigned";
const string MSG_TooOpe = " too much operators";
const string MSG_UnkId = "Unknown identification";
//@endcond
///Option class defines an abstract type for storing options in an ArgParser container.
class Option {
friend class ArgParser;
int identification; ///< the unique identifier for the option
string sName; ///< its short name (-x)
string lName; ///< its long name (--x{x})
string description; ///< a text word to identify the option value. Used in usage method output
string usage; ///< a message for helping use of this option. Used in usage method output
bool isStr; ///< true if its value is a string, false if boolean
string defStr; ///< default value if it is string
string strSet; ///< value set if it is string
bool defBool; ///< default value if it is boolean
bool boolSet; ///< value set if it is boolean
public:
Option (void);
Option(int, string, string, bool, bool);
Option(int, const char *, const char *, const char *, const char *, const char *);
Option(int, const char *, const char *, const char *, const char *, bool);
};
/**ArgParser class defines a data container for options and operators passed as arguments in the command line.
* A program using ArgParser would perform the following steps after declaring an ArgParser object:
* -# Define each option the program can accept using the addOption method. Data to be provided for each one are:
* - its short name in the form -x (like "-f")
* - its long name in the form --x{x} (like "--file")
* - a word describing the option value (like "INFILE")
* - a explanation for usage of this option (like "binary input file name")
* - default value for the option (like "data.dat" if it is a string, or true if it is boolean)
* -# Define each operand the program can accept using the addOperator method. For each one a default value has to be provided
* -# Process arguments in the command line using the method parseArgs. User should catch any error detected and provide
* information to user with the usage method
* -# Get values of options using the getBoolOpt or getStrOpt methods, and of operands using getOperator
*/
class ArgParser {
friend class Option;
forward_list<Option> optionsLst; ///< to place options data
int lastId; ///< the last identification assigned
vector<string> operatorsVector; ///< to place operator values
bool isShortOption(string, bool);
bool isLongOption(string, bool);
void checkOption(string, Option);
void setOptVal (Option);
public:
ArgParser(void);
~ArgParser(void);
int addOption (const char *, const char *, const char *, const char *, const char *);
int addOption (const char *, const char *, const char *, const char *, bool);
int addOperator(const char *);
bool getBoolOpt (int);
string getStrOpt (int);
string getOperator (int);
void usage(string, string);
void parseArgs (int, char **);
// string ArgParser::showOptValues();
// string ArgParser::showOpeValues();
string showOptValues();
string showOpeValues();
};
#endif