/
commandlineoptions.h
163 lines (141 loc) · 5.58 KB
/
commandlineoptions.h
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
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
/*
The MIT License
Copyright 2006 Sony Computer Entertainment Inc.
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
*/
#ifndef _COMMAND_LINE_OPTIONS_H_
#define _COMMAND_LINE_OPTIONS_H_
//---------------------------------------------------------------------------
// CommandLineOptions
//
// Author: John Bates
// Date: 04/22/02
//
// Description: Creates a structure of command line parameters that can
// be accessed by their string name. Prints out a formatted "usage" screen
// based on the registered command line parameters if the input is not
// acceptable.
//
//---------------------------------------------------------------------------
#include <map>
#include <set>
#include <vector>
#include <string>
class CommandLineOptions
{
class PrintOut{
public:
virtual ~PrintOut(){}
virtual void print(const CommandLineOptions* const clo) const=0;
};
class Heading : public PrintOut {
public:
virtual ~Heading();
Heading();
Heading(const Heading& rhs);
Heading& operator=(const Heading& rhs);
std::string name;
virtual void print(const CommandLineOptions* const clo) const;
};
class Setting : public PrintOut {
public:
virtual ~Setting();
Setting();
Setting(const Setting& rhs);
Setting& operator=(const Setting& rhs);
std::string name, input, description;
virtual void print(const CommandLineOptions* const clo) const;
};
class Option : public PrintOut {
public:
virtual ~Option();
Option();
Option(const Option& rhs);
Option& operator=(const Option& rhs);
std::string name, description;
virtual void print(const CommandLineOptions* const clo) const;
};
class Global : public PrintOut {
public:
virtual ~Global();
Global();
Global(const Global& rhs);
Global& operator=(const Global& rhs);
std::string input, description;
virtual void print(const CommandLineOptions* const clo) const;
};
class Example : public PrintOut {
public:
virtual ~Example();
Example();
Example(const Example& rhs);
Example& operator=(const Example& rhs);
std::string input, description;
virtual void print(const CommandLineOptions* const clo) const;
};
private:
std::set<std::string> names;
std::map<std::string, std::string> aliases;
std::vector<Setting*> settings;
std::vector<Option*> options;
std::vector<Global*> globals;
std::vector<Example> examples;
std::vector<PrintOut*> printouts;
std::string binary_name;
std::string title;
std::map<std::string, std::string> isettings;
std::set<std::string> ioptions;
std::vector<std::string> iglobals;
std::vector<std::string>::iterator global_it;
// These private checks check for *support* of a setting or option
bool _CheckForSetting(std::string& name) const;
bool _CheckForOption(std::string& name) const;
const std::string& _BaseName(const std::string& name) const;
void _NewName(const std::string& name);
void _AddAlias(const std::string& name, const std::string& alias);
void _ExitUsage() const;
public:
CommandLineOptions();
CommandLineOptions(const CommandLineOptions *const clo); // For partial copies only
~CommandLineOptions();
void Load(int argc, char *argv[]);
void Unload();
void PartiallyUnload();
void SetTitle(const std::string& name);
void AddHeading(const std::string& name);
void AddExample(const std::string& example, const std::string& description);
void AddSetting(const std::string& name, const std::string& input, const std::string& description);
void AddSetting(const std::string& name, const std::string& input, const std::string& description, const std::string& alias);
void AddOption(const std::string& name, const std::string& description);
void AddOption(const std::string& name, const std::string& description, const std::string& alias);
void AddGlobal(const std::string& input, const std::string& description);
size_t NumSettings() const { return isettings.size(); }
size_t NumOptions() const { return ioptions.size(); }
size_t NumGlobals() const { return iglobals.size(); }
// These public checks check for presence of the setting or option on the command line
bool CheckForSetting(const std::string& name) const;
bool CheckForOption(const std::string& name) const;
bool CheckForGlobal() const { return !iglobals.empty(); }
// Get methods for settings. Make sure you call CheckForSetting() first.
std::string GetString(const std::string& name) const;
long GetInt(const std::string& name) const;
double GetFloat(const std::string& name) const;
// Returns the next global, or false if there is none
bool GetNextGlobal(std::string& s);
void PrintUsage() const;
};
#endif