29.07.2011 - fundamental new
This commit is contained in:
60
picli.h
Normal file
60
picli.h
Normal file
@@ -0,0 +1,60 @@
|
||||
#ifndef PICLI_H
|
||||
#define PICLI_H
|
||||
|
||||
#include "pistring.h"
|
||||
|
||||
class PICLI
|
||||
{
|
||||
public:
|
||||
PICLI(int argc, char * argv[]);
|
||||
|
||||
inline void addArgument(const PIString & name, bool value = false) {_args << Argument(name, name[0], name, value); needParse = true;}
|
||||
inline void addArgument(const PIString & name, const PIChar & shortKey, bool value = false) {_args << Argument(name, shortKey, name, value); needParse = true;}
|
||||
inline void addArgument(const PIString & name, const char * shortKey, bool value = false) {_args << Argument(name, PIChar(shortKey), name, value); needParse = true;}
|
||||
inline void addArgument(const PIString & name, const PIChar & shortKey, const PIString & fullKey, bool value = false) {_args << Argument(name, shortKey, fullKey, value); needParse = true;}
|
||||
inline void addArgument(const PIString & name, const char * shortKey, const PIString & fullKey, bool value = false) {_args << Argument(name, PIChar(shortKey), fullKey, value); needParse = true;}
|
||||
|
||||
inline PIString rawArgument(int index) {return _args_raw[index];}
|
||||
inline PIString mandatoryArgument(int index) {return _args_mand[index];}
|
||||
inline PIString optionalArgument(int index) {return _args_opt[index];}
|
||||
inline const PIStringList & rawArguments() const {return _args_raw;}
|
||||
inline const PIStringList & mandatoryArguments() const {return _args_mand;}
|
||||
inline const PIStringList & optionalArguments() const {return _args_opt;}
|
||||
inline const PIString programCommand() const {return _args_raw.size() > 0 ? _args_raw.front() : PIString();}
|
||||
inline bool hasArgument(const PIString & name) {parse(); piForeachA (i, _args) if (i.name == name && i.found) return true; return false;}
|
||||
inline PIString argumentValue(const PIString & name) {parse(); piForeachA (i, _args) if (i.name == name && i.found) return i.value; return PIString();}
|
||||
inline PIString argumentShortKey(const PIString & name) {piForeachA (i, _args) if (i.name == name) return i.short_key; return PIString();}
|
||||
inline PIString argumentFullKey(const PIString & name) {piForeachA (i, _args) if (i.name == name) return i.full_key; return PIString();}
|
||||
|
||||
inline const PIString & shortKeyPrefix() const {return _prefix_short;}
|
||||
inline const PIString & fullKeyPrefix() const {return _prefix_full;}
|
||||
inline const int mandatoryArgumentsCount() const {return _count_mand;}
|
||||
inline const int optionalArgumentsCount() const {return _count_opt;}
|
||||
inline void setShortKeyPrefix(const PIString & prefix) {_prefix_short = prefix;}
|
||||
inline void setFullKeyPrefix(const PIString & prefix) {_prefix_full = prefix;}
|
||||
inline void setMandatoryArgumentsCount(const int count) {_count_mand = count;}
|
||||
inline void setOptionalArgumentsCount(const int count) {_count_opt = count;}
|
||||
|
||||
private:
|
||||
struct Argument {
|
||||
Argument() {has_value = found = false;}
|
||||
Argument(const PIString & n, const PIChar & s, const PIString & f, bool v) {name = n; short_key = s; full_key = f; has_value = v; found = false;}
|
||||
PIString name;
|
||||
PIChar short_key;
|
||||
PIString full_key;
|
||||
PIString value;
|
||||
bool has_value, found;
|
||||
};
|
||||
|
||||
void parse();
|
||||
|
||||
PIString _prefix_short, _prefix_full;
|
||||
PIStringList _args_raw, _args_mand, _args_opt;
|
||||
PISet<PIString> keys_full, keys_short;
|
||||
PIVector<Argument> _args;
|
||||
int _count_mand, _count_opt;
|
||||
bool needParse;
|
||||
|
||||
};
|
||||
|
||||
#endif // PICLI_H
|
||||
Reference in New Issue
Block a user