61 lines
3.0 KiB
C++
61 lines
3.0 KiB
C++
#ifndef PICLI_H
|
|
#define PICLI_H
|
|
|
|
#include "pistring.h"
|
|
|
|
class PICLI
|
|
{
|
|
public:
|
|
PICLI(int argc, char * argv[]);
|
|
|
|
void addArgument(const PIString & name, bool value = false) {_args << Argument(name, name[0], name, value); needParse = true;}
|
|
void addArgument(const PIString & name, const PIChar & shortKey, bool value = false) {_args << Argument(name, shortKey, name, value); needParse = true;}
|
|
void addArgument(const PIString & name, const char * shortKey, bool value = false) {_args << Argument(name, PIChar(shortKey), name, value); needParse = true;}
|
|
void addArgument(const PIString & name, const PIChar & shortKey, const PIString & fullKey, bool value = false) {_args << Argument(name, shortKey, fullKey, value); needParse = true;}
|
|
void addArgument(const PIString & name, const char * shortKey, const PIString & fullKey, bool value = false) {_args << Argument(name, PIChar(shortKey), fullKey, value); needParse = true;}
|
|
|
|
PIString rawArgument(int index) {return _args_raw[index];}
|
|
PIString mandatoryArgument(int index) {return _args_mand[index];}
|
|
PIString optionalArgument(int index) {return _args_opt[index];}
|
|
const PIStringList & rawArguments() const {return _args_raw;}
|
|
const PIStringList & mandatoryArguments() const {return _args_mand;}
|
|
const PIStringList & optionalArguments() const {return _args_opt;}
|
|
const PIString programCommand() const {return _args_raw.size() > 0 ? _args_raw.front() : PIString();}
|
|
bool hasArgument(const PIString & name) {parse(); piForeach (Argument & i, _args) if (i.name == name && i.found) return true; return false;}
|
|
PIString argumentValue(const PIString & name) {parse(); piForeach (Argument &i, _args) if (i.name == name && i.found) return i.value; return PIString();}
|
|
PIString argumentShortKey(const PIString & name) {piForeach (Argument &i, _args) if (i.name == name) return i.short_key; return PIString();}
|
|
PIString argumentFullKey(const PIString & name) {piForeach (Argument &i, _args) if (i.name == name) return i.full_key; return PIString();}
|
|
|
|
const PIString & shortKeyPrefix() const {return _prefix_short;}
|
|
const PIString & fullKeyPrefix() const {return _prefix_full;}
|
|
const int mandatoryArgumentsCount() const {return _count_mand;}
|
|
const int optionalArgumentsCount() const {return _count_opt;}
|
|
void setShortKeyPrefix(const PIString & prefix) {_prefix_short = prefix;}
|
|
void setFullKeyPrefix(const PIString & prefix) {_prefix_full = prefix;}
|
|
void setMandatoryArgumentsCount(const int count) {_count_mand = count;}
|
|
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
|