mxlib
c++ tools for analyzing astronomical data and other tasks by Jared R. Males. [git repo]
clOptions.hpp
Go to the documentation of this file.
1 /** \file clOptions.hpp
2  * \author Jared R. Males
3  * \brief A command line parser
4  *
5  * \ingroup mxApp_files
6  */
7 
8 //***********************************************************************//
9 // Copyright 2015, 2016, 2017, 2018 Jared R. Males (jaredmales@gmail.com)
10 //
11 // This file is part of mxlib.
12 //
13 // mxlib is free software: you can redistribute it and/or modify
14 // it under the terms of the GNU General Public License as published by
15 // the Free Software Foundation, either version 3 of the License, or
16 // (at your option) any later version.
17 //
18 // mxlib is distributed in the hope that it will be useful,
19 // but WITHOUT ANY WARRANTY; without even the implied warranty of
20 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21 // GNU General Public License for more details.
22 //
23 // You should have received a copy of the GNU General Public License
24 // along with mxlib. If not, see <http://www.gnu.org/licenses/>.
25 //***********************************************************************//
26 
27 #ifndef app_clOptions_hpp
28 #define app_clOptions_hpp
29 
30 
31 #include <unordered_map>
32 #include <vector>
33 #include <string>
34 #include <iostream>
35 
36 #include "optionparser/optionparser.h"
37 
38 namespace mx
39 {
40 namespace app
41 {
42 
43 /// Argument types
44 enum argType
45 {
46  None,
47  False,
48  True,
49  Optional,
50  Required
51 };
52 
53 /// Command line options parser.
54 /** This is a wrapper for the <a href="https://sourceforge.net/projects/optionparser/">"The Lean Mean C++ Option Parser"</a> command line parser.
55  *
56  * \ingroup mxApp
57  */
58 struct clOptions
59 {
60  std::unordered_map<std::string, unsigned int> map;
61  std::unordered_map<std::string, unsigned int> typeMap;
62 
63  typedef std::unordered_map<std::string, unsigned int>::iterator mapIterator;
64 
65  std::vector<option::Descriptor> descriptions;
66 
67  option::Option * options {nullptr};
68  option::Option * buffer {nullptr};
69 
70  unsigned int nOpts {0}; ///< The number of options added.
71 
72  /// D'tor. Deletes the options and buffer pointers.
73  ~clOptions();
74 
75  /// Clear the memory held by this object.
76  void clear();
77 
78  /// Add a command line option target.
79  void add( const std::string & optName, ///< [in] The name of the option, used as its key
80  const char * const shortOpt, ///< [in] The short option character
81  const char * const longOpt, ///< [in] The long option keyword
82  int argT ///< [in] The option type
83  );
84 
85  ///Parse the command line
86  void parse( int argc, ///< [in] From main(argc, argv), the number of command line arguments
87  char **argv, ///< in] From main(argc, argv), the command line arguments
88  std::vector<std::string> * nonOptions = 0 ///< [out] [optional] the elements in argv which are not option or option-arguments.
89  );
90 
91  ///Get the value of the option, if present.
92  /**
93  * For a true/false type, returns true or false as appropriate.
94  * Returns an empty string if no argument for the key.
95  * Otherwise return the argument.
96  *
97  * \returns 0 on error
98  * \returns an empty string on no argument
99  * \returns a string otherwise
100  */
101  const char * operator[](const std::string & key /**< [in] the key identifying the element */);
102 
103  ///Get the number of times the option was set on the command line.
104  /**
105  * \returns the number of times this option was found.
106  */
107  int count( const std::string & key /**< [in] the key identifying the element */ );
108 
109  ///Fill a vector of strings with the arguments supplied for key, last to first.
110  void getAll( std::vector<std::string> & args, ///< [out] will be resized and populated with the arguments. Will be empty if no arguments specified.
111  const std::string & key ///< [in] the key identifying the element
112  );
113 
114  ///Test whether this option was set.
115  /**
116  * \returns true if the options was set on the command line
117  * \returns false otherwise
118  */
119  bool optSet( const std::string & key /**< [in] the key identifying the element */ );
120 
121  /// Get the number of unknown options found by the parser
122  /**
123  * \returns the count of options found that do no match an input option
124  */
125  int numUnknown();
126 
127  /// Get a vector of the unknown options found by the parser
128  /**
129  * \returns 0 on success (always)
130  */
131  int unknown( std::vector<std::string> & unk /**<[out] the vector to populate with the unknown options found*/);
132 };
133 
134 } //namespace app
135 } //namespace mx
136 
137 
138 #endif //app_clOptions_hpp
139 
argType
Argument types.
Definition: clOptions.hpp:45
The mxlib c++ namespace.
Definition: mxError.hpp:107
Command line options parser.
Definition: clOptions.hpp:59
void parse(int argc, char **argv, std::vector< std::string > *nonOptions=0)
Parse the command line.
Definition: clOptions.cpp:104
const char * operator[](const std::string &key)
Get the value of the option, if present.
Definition: clOptions.cpp:136
void clear()
Clear the memory held by this object.
Definition: clOptions.cpp:58
int numUnknown()
Get the number of unknown options found by the parser.
Definition: clOptions.cpp:252
void add(const std::string &optName, const char *const shortOpt, const char *const longOpt, int argT)
Add a command line option target.
Definition: clOptions.cpp:71
int count(const std::string &key)
Get the number of times the option was set on the command line.
Definition: clOptions.cpp:167
unsigned int nOpts
The number of options added.
Definition: clOptions.hpp:70
void getAll(std::vector< std::string > &args, const std::string &key)
Fill a vector of strings with the arguments supplied for key, last to first.
Definition: clOptions.cpp:176
~clOptions()
D'tor. Deletes the options and buffer pointers.
Definition: clOptions.cpp:52
int unknown(std::vector< std::string > &unk)
Get a vector of the unknown options found by the parser.
Definition: clOptions.cpp:258
bool optSet(const std::string &key)
Test whether this option was set.
Definition: clOptions.cpp:242