23 #ifndef __CLRX_CLIPARSER_H__ 24 #define __CLRX_CLIPARSER_H__ 40 #define CLRX_CLI_AUTOHELP \ 41 { "help", '?', CLIArgType::NONE, false, false, "print help", nullptr }, \ 42 { "usage", 0, CLIArgType::NONE, false, false, "print usage", nullptr }, \ 43 { "version", 0, CLIArgType::NONE, false, false, "print version", nullptr }, 53 BOOL, UINT, INT,
UINT64,
INT64, SIZE, FLOAT, DOUBLE, STRING,
57 UINT_ARRAY, INT_ARRAY, UINT64_ARRAY, INT64_ARRAY, SIZE_ARRAY, FLOAT_ARRAY,
58 DOUBLE_ARRAY, STRING_ARRAY,
60 ARRAY_MAX = TRIMMED_STRING_ARRAY
87 CLIException(
const std::string& message,
char shortName);
89 CLIException(
const std::string& message,
const std::string& longName);
92 bool chooseShortName);
125 struct OptTypeTrait {
static const CLIArgType type; };
168 operator bool()
const {
return b; }
170 operator cxint()
const {
return i; }
182 operator float()
const {
return f; }
184 operator double()
const {
return d; }
186 operator const char*()
const {
return s; }
189 operator const bool*()
const {
return bArr; }
191 operator const cxint*()
const {
return iArr; }
193 operator const cxuint*()
const {
return uArr; }
195 operator const cxlong*()
const {
return liArr; }
197 operator const cxulong*()
const {
return luArr; }
199 operator const cxllong*()
const {
return lliArr; }
201 operator const cxullong*()
const {
return lluArr; }
203 operator const float*()
const {
return fArr; }
205 operator const double*()
const {
return dArr; }
207 operator const char**()
const {
return sArr; }
211 OptionEntry() : isSet(
false), isArg(
false), arrSize(0)
212 { ::memset(&v, 0,
sizeof(v)); }
216 const char* programName;
217 const char* packageName;
220 std::vector<const char*> leftOverArgs;
223 LongNameMap longNameMap;
224 std::unique_ptr<cxuint[]> shortNameMap;
226 void handleExceptionsForGetOptArg(
cxuint optionId,
CLIArgType argType)
const;
227 void parseOptionArg(
cxuint optionId,
const char* optArg,
bool chooseShortName);
238 cxuint argc,
const char** argv);
243 { packageName = pkgName; }
249 bool handleHelpOrUsage(std::ostream& os = std::cout)
const;
252 cxuint findOption(
char shortName)
const;
255 cxuint findOption(
const char* longName)
const;
261 handleExceptionsForGetOptArg(optionId, OptTypeTrait<T>::type);
262 return static_cast<T
>(optionEntries[optionId].v);
268 {
return getOptArg<T>(findOption(shortName)); }
273 {
return getOptArg<T>(findOption(longName)); }
284 handleExceptionsForGetOptArg(optionId, OptTypeTrait<T*>::type);
285 const OptionEntry& optEntry = optionEntries[optionId];
286 length = optEntry.arrSize;
287 return static_cast<const T*
>(optEntry.v);
298 {
return getOptArgArray<T>(findOption(shortName), length); }
308 {
return getOptArgArray<T>(findOption(longName), length); }
313 if (optionId >= optionEntries.
size())
315 return optionEntries[optionId].isArg;
320 {
return hasOptArg(findOption(shortName)); }
324 {
return hasOptArg(findOption(longName)); }
329 if (optionId >= optionEntries.
size())
331 return optionEntries[optionId].isSet;
336 {
return hasOption(findOption(shortName)); }
340 {
return hasOption(findOption(longName)); }
344 {
return leftOverArgs.size()-1; }
347 {
return leftOverArgs.data(); }
350 void printHelp(std::ostream& os = std::cout)
const;
352 void printUsage(std::ostream& os = std::cout)
const;
354 void printVersion(std::ostream& os = std::cout)
const;
359 struct CLIParser::OptTypeTrait<bool> {
371 struct CLIParser::OptTypeTrait<
cxint> {
401 struct CLIParser::OptTypeTrait<float> {
407 struct CLIParser::OptTypeTrait<double> {
413 struct CLIParser::OptTypeTrait<const char*> {
419 struct CLIParser::OptTypeTrait<bool*> {
425 struct CLIParser::OptTypeTrait<
cxuint*> {
431 struct CLIParser::OptTypeTrait<
cxint*> {
440 (
sizeof(
cxulong)==8)?CLIArgType::UINT64_ARRAY:CLIArgType::UINT_ARRAY; };
444 struct CLIParser::OptTypeTrait<
cxlong*> {
447 (
sizeof(
cxlong)==8)?CLIArgType::INT64_ARRAY:CLIArgType::INT_ARRAY; };
463 struct CLIParser::OptTypeTrait<float*> {
469 struct CLIParser::OptTypeTrait<double*> {
475 struct CLIParser::OptTypeTrait<const char**> {
T getOptArg(cxuint optionId) const
get option argument if it provided
Definition: CLIParser.h:259
cxllong * lliArr
array of cxllongs
Definition: CLIParser.h:156
const T * getOptArgArray(cxuint optionId, size_t &length) const
get option argument array if it provided
Definition: CLIParser.h:282
non copyable and non movable base structure (class)
Definition: Utilities.h:46
T getShortOptArg(char shortName) const
get option argument if it provided
Definition: CLIParser.h:267
trimmed string (without spaces at begin and end)
bool b
boolean
Definition: CLIParser.h:136
bool * bArr
array of booleans
Definition: CLIParser.h:151
bool hasShortOptArg(char shortName) const
returns true when argument provided for specified option
Definition: CLIParser.h:319
uint64_t * u64Arr
array of 64-bit uints
Definition: CLIParser.h:161
uint32_t * u32Arr
array of 32-bit uints
Definition: CLIParser.h:159
cxint i
cxint
Definition: CLIParser.h:137
CLIArgType argType
type of argument of option (or none)
Definition: CLIParser.h:68
bool arrayFromOccurrences
construct argument array from any occurrence of option argument
Definition: CLIParser.h:72
cxint * iArr
array of cxints
Definition: CLIParser.h:152
cxullong * lluArr
array of cxullongs
Definition: CLIParser.h:157
signed long cxlong
signed long
Definition: Config.h:239
bool argIsOptional
if true then option argument is optional
Definition: CLIParser.h:69
trimmed string (without spaces at begin and end)
float * fArr
array of floats
Definition: CLIParser.h:163
an array class
Definition: Containers.h:41
float f
float
Definition: CLIParser.h:148
cxulong lu
cxulong
Definition: CLIParser.h:140
cxuint u
cxuint
Definition: CLIParser.h:138
const char *const * getArgs() const
get left over arguments (null-terminated)
Definition: CLIParser.h:346
Command line option description.
Definition: CLIParser.h:64
const char ** sArr
array of C-style strings
Definition: CLIParser.h:165
double * dArr
array of doubles
Definition: CLIParser.h:164
unsigned char cxuchar
unsigned character (unsigned byte)
Definition: Config.h:227
int32_t * i32Arr
array of 32-bit ints
Definition: CLIParser.h:158
cxlong li
cxlong
Definition: CLIParser.h:139
CLIArgType
type of argument of the option
Definition: CLIParser.h:50
int64_t * i64Arr
array of 64-bit ints
Definition: CLIParser.h:160
void setPackageName(const char *pkgName)
set package name
Definition: CLIParser.h:242
cxuint * uArr
array of cxuints
Definition: CLIParser.h:153
bool hasLongOption(const char *longName) const
returns true if option included in command line
Definition: CLIParser.h:339
char shortName
short name of option (single character)
Definition: CLIParser.h:67
unsigned long long cxullong
unsigned long long
Definition: Config.h:245
uint64_t u64
64-bit uint
Definition: CLIParser.h:146
const char * argName
name of argument of option
Definition: CLIParser.h:75
cxuint getArgsNum() const
get left over arguments number
Definition: CLIParser.h:343
const char * longName
long name of option
Definition: CLIParser.h:66
bool hasOptArg(cxuint optionId) const
returns true when argument provided for specified option
Definition: CLIParser.h:311
main namespace
Definition: AsmDefs.h:38
const char * description
description of option
Definition: CLIParser.h:74
unsigned int cxuint
unsigned int
Definition: Config.h:237
value holder
Definition: CLIParser.h:135
int64_t i64
64-bit int
Definition: CLIParser.h:145
const char * s
C-style string.
Definition: CLIParser.h:150
cxullong llu
cxullong
Definition: CLIParser.h:142
unsigned long cxulong
unsigned long
Definition: Config.h:241
signed long long cxllong
signed long long
Definition: Config.h:243
cxllong lli
cxllong
Definition: CLIParser.h:141
double d
double
Definition: CLIParser.h:149
size_t size
size
Definition: CLIParser.h:147
utilities for other libraries and programs
bool hasOption(cxuint optionId) const
returns true if option included in command line
Definition: CLIParser.h:327
const T * getLongOptArgArray(const char *longName, size_t &length) const
get option argument array if it provided
Definition: CLIParser.h:307
cxlong * liArr
array of cxlongs
Definition: CLIParser.h:154
exception class
Definition: Utilities.h:61
size_t * sizeArr
array of sizes
Definition: CLIParser.h:162
T getLongOptArg(const char *longName) const
get option argument if it provided
Definition: CLIParser.h:272
size_t size() const
returns number of elements
Definition: Containers.h:172
signed int cxint
signed int
Definition: Config.h:235
cxulong * luArr
array cxulongs
Definition: CLIParser.h:155
The Command Line Parser (parses options and their arguments)
Definition: CLIParser.h:121
uint32_t u32
32-bit uint
Definition: CLIParser.h:144
CLI exception class.
Definition: CLIParser.h:79
bool hasLongOptArg(const char *longName) const
returns true when argument provided for specified option
Definition: CLIParser.h:323
int32_t i32
32-bit int
Definition: CLIParser.h:143
const T * getShortOptArgArray(char shortName, size_t &length) const
get option argument array if it provided
Definition: CLIParser.h:297
containers and other utils for other libraries and programs
bool hasShortOption(char shortName) const
returns true if option included in command line
Definition: CLIParser.h:335