Argus Camera Sample
Argus Camera Sample
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
Options.h
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2016, NVIDIA CORPORATION. All rights reserved.
3  *
4  * Redistribution and use in source and binary forms, with or without
5  * modification, are permitted provided that the following conditions
6  * are met:
7  * * Redistributions of source code must retain the above copyright
8  * notice, this list of conditions and the following disclaimer.
9  * * Redistributions in binary form must reproduce the above copyright
10  * notice, this list of conditions and the following disclaimer in the
11  * documentation and/or other materials provided with the distribution.
12  * * Neither the name of NVIDIA CORPORATION nor the names of its
13  * contributors may be used to endorse or promote products derived
14  * from this software without specific prior written permission.
15  *
16  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS ``AS IS'' AND ANY
17  * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
19  * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
20  * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
21  * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
22  * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
23  * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
24  * OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
26  * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27  */
28 
29 #ifndef OPTIONS_H
30 #define OPTIONS_H
31 
32 #include <vector>
33 #include <string>
34 
35 #include "Value.h"
36 
37 namespace ArgusSamples
38 {
39 
40 /**
41  * Handles command line options.
42  */
43 class Options
44 {
45 public:
46  explicit Options(const char *programName);
47  ~Options();
48 
49  bool initialize();
50 
51  /**
52  * Defines a single option
53  */
54  class Option
55  {
56  public:
57  /**
58  * Type
59  */
60  typedef enum
61  {
62  TYPE_ACTION, ///< triggers an action
63  TYPE_OPTION ///< sets an option
64  } Type;
65 
66  /**
67  * Argument flags
68  */
69  typedef enum
70  {
71  FLAG_NO_ARGUMENT, ///< requires no argument
72  FLAG_OPTIONAL_ARGUMENT, ///< optionally takes an argument
73  FLAG_REQUIRED_ARGUMENT ///< requires an argument
74  } Flag;
75 
76  /**
77  * Call back function.
78  *
79  * @param [in] usrPtr user pointer
80  * @param [in] optArg optional argument string, NULL when there is no argument
81  */
82  typedef bool (*CallBackFunc)(void *usrPtr, const char *optArg);
83 
84  /**
85  * Construct an option
86  *
87  * @param name [in] long option name
88  * @param shortName [in] short option name
89  * @param argument [in] argument
90  * @param type [in] option type
91  * @param flags [in] flags
92  * @param usage [in] a string describing the usage
93  * @param function [in] callback function
94  * @param userPtr [in] user pointer
95  */
96  explicit Option(std::string name, char shortName, std::string argument, Type type,
97  Flag flags, std::string usage, CallBackFunc function, void *userPtr = NULL)
98  : m_name(name)
99  , m_shortName(shortName)
100  , m_argument(argument)
101  , m_type(type)
102  , m_flags(flags)
103  , m_usage(usage)
104  , m_function(function)
105  , m_userPtr(userPtr)
106  {
107  }
108  /**
109  * Construct an option from a Value. The final usage string is build from the given usage
110  * and the default and valid values of the given value variable.
111  *
112  * @param name [in] long option name
113  * @param shortName [in] short option name
114  * @param argument [in] argument
115  * @param value [in] value to construct the option from
116  * @param usage [in] a string describing the usage
117  * @param function [in] callback function
118  * @param userPtr [in] user pointer
119  */
120  template<typename T> explicit Option(std::string name, char shortName,
121  std::string argument, Value<T> &value, std::string usage, CallBackFunc function,
122  void *userPtr = NULL)
123  : m_name(name)
124  , m_shortName(shortName)
125  , m_argument(argument)
127  , m_flags(argument.empty() ? FLAG_NO_ARGUMENT : FLAG_REQUIRED_ARGUMENT)
128  , m_function(function)
129  , m_userPtr(userPtr)
130  {
131  std::ostringstream msg;
132  msg << usage << " " << value.getValidator()->getValidValuesMessage() <<
133  " Default is '" << value.toString() << "'.";
134  m_usage = msg.str();
135  }
137  {
138  }
139 
140  std::string m_name; //!< option name
141  char m_shortName; //!< option short name
142  std::string m_argument; //!< argument name
143  Type m_type; //!< option type
144  Flag m_flags; //!< option flags
145  std::string m_usage; //!< usage message
146  CallBackFunc m_function; //!< callback function
147  void *m_userPtr; //!< user pointer passed to callback function
148  };
149 
150  /**
151  * Print the usage message.
152  */
153  bool usage();
154 
155  /**
156  * Parse the command line options
157  *
158  * @param [in] argc argument count
159  * @param [in] argv argument values
160  */
161  bool parse(const int argc, char * const *argv);
162 
163  /**
164  * Add a option
165  *
166  * @param [in] option option to add
167  * @param [in] userPtr user pointer passed to callback function
168  */
169  bool addOption(const Option &option, void *userPtr = NULL);
170 
171  /**
172  * Add multiple options
173  *
174  * @param [in] count how many options to add
175  * @param [in] options option array
176  * @param [in] userPtr user pointer passed to callback functions
177  */
178  bool addOptions(size_t count, const Option *options, void *userPtr = NULL);
179 
180  /**
181  * Add test to the description, will be printed with the usage message
182  *
183  * @param [in] description Description
184  */
185  bool addDescription(const char *description);
186 
187  /**
188  * Request exit after the current option, called from callback function.
189  */
190  bool requestExit();
191 
192  /**
193  * Has the exit been requested?
194  */
195  bool requestedExit() const;
196 
197  /**
198  * help option callback
199  */
200  static bool printHelp(void *userPtr, const char *optArg);
201 
202  /**
203  * exit option callback
204  */
205  static bool exit(void *userPtr, const char *optArg);
206 
207 private:
210  std::string m_programName;
211  std::string m_description;
212  std::vector<Option> m_options;
213 
214  /**
215  * Hide default constructor
216  */
217  Options();
218 };
219 
220 }; // namespace ArgusSamples
221 
222 #endif // OPTIONS_H