mxlib
c++ tools for analyzing astronomical data and other tasks by Jared R. Males. [git repo]
Loading...
Searching...
No Matches
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#include <unordered_map>
31#include <vector>
32#include <string>
33#include <iostream>
34
35#include "optionparser/optionparser.h"
36
37namespace mx
38{
39namespace app
40{
41
42/// Argument types
44{
45 None,
46 False,
47 True,
48 Optional,
49 Required
50};
51
52/// Command line options parser.
53/** This is a wrapper for the <a href="https://sourceforge.net/projects/optionparser/">"The Lean Mean C++ Option
54 * Parser"</a> command line parser.
55 *
56 * \ingroup mxApp
57 */
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 /**
87 * \todo test for descriptions of size 0 and with/without ending sentinel
88 */
89 void parse( int argc, ///< [in] From main(argc, argv), the number of command line arguments
90 char **argv, ///< in] From main(argc, argv), the command line arguments
91 std::vector<std::string> *nonOptions = 0 /**< [out] [optional] the elements in argv
92 which are not option or option-arguments. */
93 );
94
95 /// Get the value of the option, if present.
96 /**
97 * For a true/false type, returns true or false as appropriate.
98 * Returns an empty string if no argument for the key.
99 * Otherwise return the argument.
100 *
101 * \returns 0 on error
102 * \returns an empty string on no argument
103 * \returns a string otherwise
104 */
105 const char *operator[]( const std::string &key /**< [in] the key identifying the element */ );
106
107 /// Get the number of times the option was set on the command line.
108 /**
109 * \returns the number of times this option was found.
110 */
111 int count( const std::string &key /**< [in] the key identifying the element */ );
112
113 /// Fill a vector of strings with the arguments supplied for key, last to first.
114 void getAll( std::vector<std::string> &args, ///< [out] will be resized and populated with the arguments. Will be
115 ///< empty if no arguments specified.
116 const std::string &key ///< [in] the key identifying the element
117 );
118
119 /// Test whether this option was set.
120 /**
121 * \returns true if the options was set on the command line
122 * \returns false otherwise
123 */
124 bool optSet( const std::string &key /**< [in] the key identifying the element */ );
125
126 /// Get the number of unknown options found by the parser
127 /**
128 * \returns the count of options found that do no match an input option
129 */
130 int numUnknown();
131
132 /// Get a vector of the unknown options found by the parser
133 /**
134 * \returns 0 on success (always)
135 */
136 int unknown( std::vector<std::string> &unk /**<[out] the vector to populate with the unknown options found*/ );
137};
138
139} // namespace app
140} // namespace mx
141
142#endif // app_clOptions_hpp
argType
Argument types.
Definition clOptions.hpp:44
The mxlib c++ namespace.
Definition mxlib.hpp:37
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:99
const char * operator[](const std::string &key)
Get the value of the option, if present.
void clear()
Clear the memory held by this object.
Definition clOptions.cpp:56
int numUnknown()
Get the number of unknown options found by the parser.
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.
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.
~clOptions()
D'tor. Deletes the options and buffer pointers.
Definition clOptions.cpp:48
int unknown(std::vector< std::string > &unk)
Get a vector of the unknown options found by the parser.
bool optSet(const std::string &key)
Test whether this option was set.