CommandLine.hpp
Go to the documentation of this file.
1 //==============================================================================
2 //
3 // This file is part of GNSSTk, the ARL:UT GNSS Toolkit.
4 //
5 // The GNSSTk is free software; you can redistribute it and/or modify
6 // it under the terms of the GNU Lesser General Public License as published
7 // by the Free Software Foundation; either version 3.0 of the License, or
8 // any later version.
9 //
10 // The GNSSTk is distributed in the hope that it will be useful,
11 // but WITHOUT ANY WARRANTY; without even the implied warranty of
12 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 // GNU Lesser General Public License for more details.
14 //
15 // You should have received a copy of the GNU Lesser General Public
16 // License along with GNSSTk; if not, write to the Free Software Foundation,
17 // Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110, USA
18 //
19 // This software was developed by Applied Research Laboratories at the
20 // University of Texas at Austin.
21 // Copyright 2004-2022, The Board of Regents of The University of Texas System
22 //
23 //==============================================================================
24 
25 //==============================================================================
26 //
27 // This software was developed by Applied Research Laboratories at the
28 // University of Texas at Austin, under contract to an agency or agencies
29 // within the U.S. Department of Defense. The U.S. Government retains all
30 // rights to use, duplicate, distribute, disclose, or release this software.
31 //
32 // Pursuant to DoD Directive 523024
33 //
34 // DISTRIBUTION STATEMENT A: This software has been approved for public
35 // release, distribution is unlimited.
36 //
37 //==============================================================================
38 
39 // CommandLine.hpp Command line options and argument processing.
40 //
41 // NB. Repeatable means a parse error will be generated if the option appears more
42 // than once, unless that option is repeatable. Only vector types (2,5,7) can have
43 // more than one value, but this has nothing to do with being repeatable!
44 // NB. Options that go into vectors (types 2,5,7) may have arguments value,value,...
45 // and @files.lst where files.lst is a file containing values separated by spaces
46 // and/or linefeed and NOT starting with # (but not val,val,.. inside the file).
47 // NB. There is an issue for repeatable options that are not vector types, namely if
48 // there is more than one value, which to use: values[0] or values[values.size()-1]?
49 // NB. This is related to the question for bool options - does the presence of the
50 // option set the target true, or does it toggle the target? see TD in .cpp
51 
52 #ifndef COMMAND_LINE_INCLUDE
53 #define COMMAND_LINE_INCLUDE
54 
55 #include "RinexSatID.hpp"
56 #include <string>
57 #include <vector>
58 #include <map>
59 
60 namespace gnsstk
61 {
63 class CommandLine {
64 private:
66  typedef enum OptionTypeEnum {
76  typeCount // this must be last
77  } typeOpt;
78 
79  // Encapsulate information needed to use an option, including where to store values
80  class Option {
81  public:
82  char shortOpt; // character appearing in the short command : x in -x <arg>
83  std::string longOpt;// string appearing in the long command : obs in --obs <arg>
84  std::string arg; // string giving the argument : arg in the above example
85  std::string predesc;// string to put on line _before_ description on syntax page
86  std::string desc; // string giving the description on the syntax page
87  std::string syntax; // full string used in syntax page: --opt <arg> Desc (def)
88  bool repeat; // if true, option is repeatable, meaning repeat=F causes a
89  // parse error if the option appears more than once
90  bool required; // if true, option is required
91  bool expand; // if true, expand arguments a,b,c (vector types only)
92  bool toggle; // if true for typeBool, toggle instead of set true
93  typeOpt type; // type target: bool,int,doub,str,vec<str>,RinexSatID,vec<sat>
94  void *p_output; // pointer to output - default is its value on input
95  bool doc; // if false, option is undocumented
96 
97  std::vector<std::string> values; // store values from command line
98 
99  Option(void) :
100  shortOpt(0),repeat(false),required(false),expand(true),toggle(false),
101  type(typeUndefined),p_output(NULL),doc(true) { }
102 
103  Option(char s, std::string l, std::string a, std::string predes,
104  std::string des, bool rep, bool req, typeOpt t, void *ptr, bool d)
105  {
106  shortOpt = s;
107  longOpt = l;
108  arg = a;
109  predesc = predes;
110  desc = des;
111  repeat = rep;
112  required = req;
113  type = t;
114  p_output = ptr;
115  expand = true;
116  toggle = false;
117  doc = d;
118  }
119  }; // end class Option
120 
121  // -------------- member data ------------------------------------------
122  // first two for internal use only
124  int debug; // NB default is -1
125 
126  int syntaxPageBuilt; // 0 c'tor, 1 Usage: ..., 2 prgm descript, 3 complete
127  std::string syntaxPage;
128  int optionsize; // length of the string '--opt <arg> ' in syntax page
129 
131  std::vector<Option> options;
132 
134  std::vector<std::string> ignore_opts_with_arg;
135 
137  std::vector<std::string> ignore_opts_without_arg;
138 
140  std::vector<std::string> ignore_on_opts;
141 
143  std::vector<std::string> ignore_off_opts;
144 
147  std::map<std::string,std::string> deprec_opts;
148 
149 public:
150  // -------------- member functions -------------------------------------
151  // constructor
153  : help(false),debug(-1),verbose(false),requireOneArg(true),syntaxPageBuilt(0)
154  { }
155 
156  // destructor
157  ~CommandLine(void) { }
158 
159  // access
160  bool hasHelp(void) { return help; }
161  bool hasErrors(void) { return foundErrors; }
162  unsigned int count(std::string lopt) {
163  for(unsigned int i=0; i<options.size(); i++) {
164  if(options[i].longOpt == lopt)
165  return options[i].values.size();
166  }
167  return 0;
168  }
169 
170  // don't require at least one arg
171  void noArgsRequired(void) { requireOneArg=false; }
172 
173  // -------------- add to list ---------------------------------------
176  void Add_ignore(std::string opt, bool has_arg=false)
177  {
178  if(has_arg) ignore_opts_with_arg.push_back(opt);
179  else ignore_opts_without_arg.push_back(opt);
180  }
181 
184  void Add_ignore_on(std::string opt) { ignore_on_opts.push_back(opt); }
185 
188  void Add_ignore_off(std::string opt) { ignore_off_opts.push_back(opt); }
189 
192  void Add_deprecated(std::string old_opt, std::string new_opt) {
193  deprec_opts[old_opt] = new_opt;
194  }
195 
197  void Add(char s, std::string l, std::string a, bool rep, bool req,
198  bool* ptr, std::string predes, std::string des, bool doc=true)
199  {
200  Option opt(s,l,a,predes,des,rep,req,typeBool,(void *)ptr,doc);
201  options.push_back(opt);
202  }
203 
205  void Add(char s, std::string l, std::string a, bool rep, bool req,
206  int* ptr, std::string predes, std::string des, bool doc=true)
207  {
208  Option opt(s,l,a,predes,des,rep,req,typeInt,(void *)ptr,doc);
209  options.push_back(opt);
210  }
211 
213  void Add(char s, std::string l, std::string a, bool rep, bool req,
214  std::vector<int>* ptr, std::string predes, std::string des, bool doc=true)
215  {
216  Option opt(s,l,a,predes,des,rep,req,typeVectorInt,(void *)ptr,doc);
217  options.push_back(opt);
218  }
219 
221  void Add(char s, std::string l, std::string a, bool rep, bool req,
222  double* ptr, std::string predes, std::string des, bool doc=true)
223  {
224  Option opt(s,l,a,predes,des,rep,req,typeDouble,(void *)ptr,doc);
225  options.push_back(opt);
226  }
227 
229  void Add(char s, std::string l, std::string a, bool rep, bool req,
230  std::string* ptr, std::string predes, std::string des, bool doc=true)
231  {
232  Option opt(s,l,a,predes,des,rep,req,typeString,(void *)ptr,doc);
233  options.push_back(opt);
234  }
235 
237  void Add(char s, std::string l, std::string a, bool rep, bool req,
238  std::vector<std::string>* ptr, std::string predes, std::string des,
239  bool doc=true)
240  {
241  Option opt(s,l,a,predes,des,rep,req,typeVectorString,(void *)ptr,doc);
242  options.push_back(opt);
243  }
244 
246  void Add(char s, std::string l, std::string a, bool rep, bool req,
247  gnsstk::RinexSatID* ptr, std::string predes, std::string des, bool doc=true)
248  {
249  Option opt(s,l,a,predes,des,rep,req,typeSat,(void *)ptr,doc);
250  options.push_back(opt);
251  }
252 
254  void Add(char s, std::string l, std::string a, bool rep, bool req,
255  std::vector<gnsstk::RinexSatID>* ptr, std::string predes, std::string des,
256  bool doc=true)
257  {
258  Option opt(s,l,a,predes,des,rep,req,typeVectorSat,(void *)ptr,doc);
259  options.push_back(opt);
260  }
261 
262  // -------------------------------------------------------------
265  void noExpansion(std::string l)
266  {
267  for(size_t i=0; i<options.size(); i++) {
268  if(options[i].longOpt == l && (options[i].type == typeVectorString ||
269  options[i].type == typeVectorSat ||
270  options[i].type == typeVectorInt))
271  {
272  options[i].expand = false;
273  return;
274  }
275  }
276  }
277 
278  // -------------------------------------------------------------
284  void setToggle(std::string lstr, bool b=true)
285  {
286  for(size_t i=0; i<options.size(); i++) {
287  if(options[i].longOpt == lstr && options[i].type == typeBool) {
288  options[i].toggle = b;
289  return;
290  }
291  }
292  }
293 
294  // -------------------------------------------------------------
296  void DefineUsageString(std::string str) noexcept
297  {
298  syntaxPage = "Usage: " + str;
299  syntaxPageBuilt = 1;
300  }
301 
317  int ProcessCommandLine(int argc, char** argv, std::string PrgmDesc,
318  std::string& Usage, std::string& Errors, std::vector<std::string>& Unrec);
319 
324  void DumpConfiguration(std::ostream& os, std::string tag=std::string());
325 
326 private:
329  bool ValidateCommandLine(std::string& msg);
330 
333  void BuildSyntaxPage(void);
334 
339  void PreProcessArgs(const char *arg, std::vector<std::string>& Args,
340  std::string& Errors);
341 
344  void Parse(std::vector<std::string>& Args, std::string& Errors,
345  std::vector<std::string>& Unrecog);
346 
349  std::string SyntaxPage(void);
350 
353  void Postprocess(std::string& Errors, std::vector<std::string>& Unrecog);
354 
355 }; // end class CommandLine
356 }
357 
358 #endif // COMMAND_LINE_INCLUDE
gnsstk::CommandLine::BuildSyntaxPage
void BuildSyntaxPage(void)
Definition: CommandLine.cpp:381
gnsstk::CommandLine::Option::expand
bool expand
Definition: CommandLine.hpp:91
gnsstk::CommandLine::count
unsigned int count(std::string lopt)
Definition: CommandLine.hpp:162
gnsstk::CommandLine::Option::Option
Option(char s, std::string l, std::string a, std::string predes, std::string des, bool rep, bool req, typeOpt t, void *ptr, bool d)
Definition: CommandLine.hpp:103
gnsstk::CommandLine::SyntaxPage
std::string SyntaxPage(void)
Definition: CommandLine.cpp:769
gnsstk::CommandLine::help
bool help
Definition: CommandLine.hpp:123
gnsstk::CommandLine::typeVectorInt
@ typeVectorInt
Definition: CommandLine.hpp:70
gnsstk::CommandLine::verbose
bool verbose
Definition: CommandLine.hpp:123
gnsstk::CommandLine::hasHelp
bool hasHelp(void)
Definition: CommandLine.hpp:160
gnsstk::CommandLine::Add
void Add(char s, std::string l, std::string a, bool rep, bool req, std::vector< gnsstk::RinexSatID > *ptr, std::string predes, std::string des, bool doc=true)
add a vector<RinexSatID> option
Definition: CommandLine.hpp:254
gnsstk::CommandLine::setToggle
void setToggle(std::string lstr, bool b=true)
Definition: CommandLine.hpp:284
gnsstk::CommandLine::Add
void Add(char s, std::string l, std::string a, bool rep, bool req, std::string *ptr, std::string predes, std::string des, bool doc=true)
add a string option
Definition: CommandLine.hpp:229
gnsstk::CommandLine::Option::values
std::vector< std::string > values
Definition: CommandLine.hpp:97
gnsstk::CommandLine::Add_ignore
void Add_ignore(std::string opt, bool has_arg=false)
Definition: CommandLine.hpp:176
gnsstk::CommandLine::~CommandLine
~CommandLine(void)
Definition: CommandLine.hpp:157
gnsstk::CommandLine::helponly
bool helponly
Definition: CommandLine.hpp:123
gnsstk::CommandLine::Add
void Add(char s, std::string l, std::string a, bool rep, bool req, int *ptr, std::string predes, std::string des, bool doc=true)
add an integer option
Definition: CommandLine.hpp:205
gnsstk::CommandLine::ignore_opts_with_arg
std::vector< std::string > ignore_opts_with_arg
list of strings '–opt' that, with their following arg, are ignored
Definition: CommandLine.hpp:134
gnsstk::CommandLine::Add
void Add(char s, std::string l, std::string a, bool rep, bool req, std::vector< std::string > *ptr, std::string predes, std::string des, bool doc=true)
add a vector<string> option
Definition: CommandLine.hpp:237
NULL
#define NULL
Definition: getopt1.c:64
gnsstk::CommandLine::ProcessCommandLine
int ProcessCommandLine(int argc, char **argv, std::string PrgmDesc, std::string &Usage, std::string &Errors, std::vector< std::string > &Unrec)
Definition: CommandLine.cpp:80
gnsstk::CommandLine::Option::repeat
bool repeat
Definition: CommandLine.hpp:88
gnsstk::CommandLine::typeUndefined
@ typeUndefined
Definition: CommandLine.hpp:67
gnsstk::CommandLine::Add
void Add(char s, std::string l, std::string a, bool rep, bool req, gnsstk::RinexSatID *ptr, std::string predes, std::string des, bool doc=true)
add a RinexSatID option
Definition: CommandLine.hpp:246
gnsstk::CommandLine::Add
void Add(char s, std::string l, std::string a, bool rep, bool req, double *ptr, std::string predes, std::string des, bool doc=true)
add a double option
Definition: CommandLine.hpp:221
gnsstk::CommandLine::DefineUsageString
void DefineUsageString(std::string str) noexcept
Define the text after 'Usage: '; default is '<prgm> [options] ...'.
Definition: CommandLine.hpp:296
gnsstk
For Sinex::InputHistory.
Definition: BasicFramework.cpp:50
gnsstk::CommandLine::OptionTypeEnum
OptionTypeEnum
types of options supported - easily expanded
Definition: CommandLine.hpp:66
gnsstk::CommandLine::Option::arg
std::string arg
Definition: CommandLine.hpp:84
gnsstk::CommandLine::Add
void Add(char s, std::string l, std::string a, bool rep, bool req, bool *ptr, std::string predes, std::string des, bool doc=true)
add a boolean option
Definition: CommandLine.hpp:197
gnsstk::CommandLine::requireOneArg
bool requireOneArg
Definition: CommandLine.hpp:123
gnsstk::CommandLine::typeVectorSat
@ typeVectorSat
Definition: CommandLine.hpp:75
gnsstk::CommandLine::Option::Option
Option(void)
Definition: CommandLine.hpp:99
gnsstk::CommandLine::Option::doc
bool doc
Definition: CommandLine.hpp:95
gnsstk::CommandLine::Option::required
bool required
Definition: CommandLine.hpp:90
gnsstk::CommandLine::Option::longOpt
std::string longOpt
Definition: CommandLine.hpp:83
gnsstk::CommandLine::typeBool
@ typeBool
Definition: CommandLine.hpp:68
gnsstk::CommandLine::Option::predesc
std::string predesc
Definition: CommandLine.hpp:85
gnsstk::CommandLine::typeDouble
@ typeDouble
Definition: CommandLine.hpp:71
gnsstk::CommandLine::ignore_on_opts
std::vector< std::string > ignore_on_opts
list of strings '–opt' that turn "ignoring of args" ON
Definition: CommandLine.hpp:140
gnsstk::CommandLine::typeCount
@ typeCount
Definition: CommandLine.hpp:76
gnsstk::CommandLine::syntaxPageBuilt
int syntaxPageBuilt
Definition: CommandLine.hpp:126
gnsstk::CommandLine::Add_ignore_on
void Add_ignore_on(std::string opt)
Definition: CommandLine.hpp:184
gnsstk::CommandLine::hasErrors
bool hasErrors(void)
Definition: CommandLine.hpp:161
gnsstk::CommandLine::optionsize
int optionsize
Definition: CommandLine.hpp:128
gnsstk::CommandLine::CommandLine
CommandLine(void)
Definition: CommandLine.hpp:152
gnsstk::CommandLine::typeString
@ typeString
Definition: CommandLine.hpp:72
arg
double arg
Definition: IERS1996UT1mUTCData.hpp:48
gnsstk::CommandLine::typeVectorString
@ typeVectorString
Definition: CommandLine.hpp:73
gnsstk::CommandLine::noExpansion
void noExpansion(std::string l)
Definition: CommandLine.hpp:265
gnsstk::CommandLine::Option::type
typeOpt type
Definition: CommandLine.hpp:93
gnsstk::CommandLine::Postprocess
void Postprocess(std::string &Errors, std::vector< std::string > &Unrecog)
Definition: CommandLine.cpp:803
gnsstk::CommandLine::Option
Definition: CommandLine.hpp:80
gnsstk::CommandLine::Option::shortOpt
char shortOpt
Definition: CommandLine.hpp:82
gnsstk::CommandLine::noArgsRequired
void noArgsRequired(void)
Definition: CommandLine.hpp:171
gnsstk::CommandLine::DumpConfiguration
void DumpConfiguration(std::ostream &os, std::string tag=std::string())
Definition: CommandLine.cpp:180
gnsstk::RinexSatID
Definition: RinexSatID.hpp:63
gnsstk::CommandLine::typeInt
@ typeInt
Definition: CommandLine.hpp:69
gnsstk::CommandLine::Option::syntax
std::string syntax
Definition: CommandLine.hpp:87
gnsstk::CommandLine::Option::p_output
void * p_output
Definition: CommandLine.hpp:94
gnsstk::CommandLine::ignore_opts_without_arg
std::vector< std::string > ignore_opts_without_arg
list of strings '–opt' that are simply ignored
Definition: CommandLine.hpp:137
gnsstk::CommandLine::Add_deprecated
void Add_deprecated(std::string old_opt, std::string new_opt)
Definition: CommandLine.hpp:192
gnsstk::CommandLine::Option::desc
std::string desc
Definition: CommandLine.hpp:86
gnsstk::CommandLine::syntaxPage
std::string syntaxPage
Definition: CommandLine.hpp:127
gnsstk::CommandLine::ValidateCommandLine
bool ValidateCommandLine(std::string &msg)
Definition: CommandLine.cpp:294
gnsstk::CommandLine
list of Options
Definition: CommandLine.hpp:63
gnsstk::CommandLine::foundErrors
bool foundErrors
Definition: CommandLine.hpp:123
gnsstk::CommandLine::PreProcessArgs
void PreProcessArgs(const char *arg, std::vector< std::string > &Args, std::string &Errors)
Definition: CommandLine.cpp:483
RinexSatID.hpp
gnsstk::CommandLine::deprec_opts
std::map< std::string, std::string > deprec_opts
Definition: CommandLine.hpp:147
gnsstk::CommandLine::Add_ignore_off
void Add_ignore_off(std::string opt)
Definition: CommandLine.hpp:188
gnsstk::CommandLine::typeOpt
enum gnsstk::CommandLine::OptionTypeEnum typeOpt
types of options supported - easily expanded
gnsstk::CommandLine::options
std::vector< Option > options
list of Options
Definition: CommandLine.hpp:131
gnsstk::CommandLine::typeSat
@ typeSat
Definition: CommandLine.hpp:74
gnsstk::CommandLine::ignore_off_opts
std::vector< std::string > ignore_off_opts
list of strings '–opt' that turn "ignoring of args" OFF
Definition: CommandLine.hpp:143
gnsstk::CommandLine::debug
int debug
Definition: CommandLine.hpp:124
gnsstk::CommandLine::Add
void Add(char s, std::string l, std::string a, bool rep, bool req, std::vector< int > *ptr, std::string predes, std::string des, bool doc=true)
add a vector integer option
Definition: CommandLine.hpp:213
gnsstk::CommandLine::Parse
void Parse(std::vector< std::string > &Args, std::string &Errors, std::vector< std::string > &Unrecog)
Definition: CommandLine.cpp:679
gnsstk::CommandLine::Option::toggle
bool toggle
Definition: CommandLine.hpp:92


gnsstk
Author(s):
autogenerated on Wed Oct 25 2023 02:40:38