2016-05-17 03:04:11 +12:00
|
|
|
#pragma once
|
|
|
|
|
2016-03-19 06:57:51 +13:00
|
|
|
#ifndef TCLI_INCLUDED
|
|
|
|
#define TCLI_INCLUDED
|
|
|
|
|
2016-04-14 22:15:09 +12:00
|
|
|
#include <memory>
|
|
|
|
|
2016-03-19 06:57:51 +13:00
|
|
|
//#include "tcommon.h" contenuto in tconvert.h
|
|
|
|
#include "tconvert.h"
|
|
|
|
|
|
|
|
#include "tfilepath.h"
|
|
|
|
|
|
|
|
#undef DVAPI
|
|
|
|
#undef DVVAR
|
|
|
|
#ifdef TAPPTOOLS_EXPORTS
|
|
|
|
#define DVAPI DV_EXPORT_API
|
|
|
|
#define DVVAR DV_EXPORT_VAR
|
|
|
|
#else
|
|
|
|
#define DVAPI DV_IMPORT_API
|
|
|
|
#define DVVAR DV_IMPORT_VAR
|
|
|
|
#endif
|
|
|
|
|
|
|
|
//=========================================================
|
|
|
|
|
2016-06-15 18:43:10 +12:00
|
|
|
// forward declaration
|
2016-03-19 06:57:51 +13:00
|
|
|
class TFilePath;
|
|
|
|
//=========================================================
|
|
|
|
|
2016-06-15 18:43:10 +12:00
|
|
|
namespace TCli {
|
2016-03-19 06:57:51 +13:00
|
|
|
|
|
|
|
//=========================================================
|
|
|
|
|
2016-06-15 18:43:10 +12:00
|
|
|
inline bool fromStr(int &value, std::string s) {
|
|
|
|
if (isInt(s)) {
|
|
|
|
value = std::stoi(s);
|
|
|
|
return true;
|
|
|
|
} else
|
|
|
|
return false;
|
2016-03-19 06:57:51 +13:00
|
|
|
}
|
2016-06-15 18:43:10 +12:00
|
|
|
inline bool fromStr(double &value, std::string s) {
|
|
|
|
if (isDouble(s)) {
|
|
|
|
value = std::stod(s);
|
|
|
|
return true;
|
|
|
|
} else
|
|
|
|
return false;
|
2016-03-19 06:57:51 +13:00
|
|
|
}
|
2016-06-15 18:43:10 +12:00
|
|
|
inline bool fromStr(std::string &value, std::string s) {
|
2018-07-10 17:42:57 +12:00
|
|
|
value = QString::fromLocal8Bit(s.c_str()).toStdString();
|
2016-06-15 18:43:10 +12:00
|
|
|
return true;
|
2016-03-19 06:57:51 +13:00
|
|
|
}
|
2016-06-15 18:43:10 +12:00
|
|
|
inline bool fromStr(TFilePath &value, std::string s) {
|
2018-07-10 17:42:57 +12:00
|
|
|
value = TFilePath(QString::fromLocal8Bit(s.c_str()));
|
2016-06-15 18:43:10 +12:00
|
|
|
return true;
|
2016-03-19 06:57:51 +13:00
|
|
|
}
|
|
|
|
|
|
|
|
//=========================================================
|
|
|
|
|
2016-06-15 18:43:10 +12:00
|
|
|
class UsageError {
|
|
|
|
std::string m_msg;
|
2016-03-19 06:57:51 +13:00
|
|
|
|
|
|
|
public:
|
2016-06-15 18:43:10 +12:00
|
|
|
UsageError(std::string msg) : m_msg(msg){};
|
|
|
|
~UsageError(){};
|
|
|
|
std::string getError() const { return m_msg; };
|
2016-03-19 06:57:51 +13:00
|
|
|
};
|
|
|
|
|
|
|
|
//=========================================================
|
|
|
|
|
2016-06-15 18:43:10 +12:00
|
|
|
class DVAPI UsageElement {
|
2016-03-19 06:57:51 +13:00
|
|
|
protected:
|
2016-06-15 18:43:10 +12:00
|
|
|
std::string m_name, m_help;
|
|
|
|
bool m_selected;
|
2016-03-19 06:57:51 +13:00
|
|
|
|
|
|
|
public:
|
2016-06-15 18:43:10 +12:00
|
|
|
UsageElement(std::string name, std::string help);
|
|
|
|
virtual ~UsageElement(){};
|
|
|
|
std::string getName() const { return m_name; };
|
|
|
|
bool isSelected() const { return m_selected; };
|
|
|
|
void select() { m_selected = true; };
|
|
|
|
|
|
|
|
virtual bool isHidden() const { return false; };
|
|
|
|
virtual bool isSwitcher() const { return false; };
|
|
|
|
virtual bool isArgument() const { return false; };
|
|
|
|
virtual bool isMultiArgument() const { return false; };
|
|
|
|
void setHelp(std::string help) { m_help = help; };
|
|
|
|
|
|
|
|
virtual void print(std::ostream &out) const;
|
|
|
|
virtual void printHelpLine(std::ostream &out) const;
|
|
|
|
virtual void dumpValue(std::ostream &out) const = 0;
|
|
|
|
virtual void resetValue() = 0;
|
2016-03-19 06:57:51 +13:00
|
|
|
|
|
|
|
private:
|
2016-06-15 18:43:10 +12:00
|
|
|
// not implemented
|
|
|
|
UsageElement(const UsageElement &);
|
|
|
|
UsageElement &operator=(const UsageElement &);
|
2016-03-19 06:57:51 +13:00
|
|
|
};
|
|
|
|
|
|
|
|
//=========================================================
|
|
|
|
|
2016-06-15 18:43:10 +12:00
|
|
|
class DVAPI Qualifier : public UsageElement {
|
2016-03-19 06:57:51 +13:00
|
|
|
protected:
|
2016-06-15 18:43:10 +12:00
|
|
|
bool m_switcher;
|
2016-03-19 06:57:51 +13:00
|
|
|
|
|
|
|
public:
|
2016-06-15 18:43:10 +12:00
|
|
|
Qualifier(std::string name, std::string help)
|
|
|
|
: UsageElement(name, help), m_switcher(false){};
|
|
|
|
~Qualifier(){};
|
2016-03-19 06:57:51 +13:00
|
|
|
|
2016-06-19 20:06:29 +12:00
|
|
|
bool isSwitcher() const override { return m_switcher; };
|
|
|
|
|
|
|
|
bool isHidden() const override { return m_help == ""; };
|
2016-03-19 06:57:51 +13:00
|
|
|
|
2016-06-15 18:43:10 +12:00
|
|
|
operator bool() const { return isSelected(); };
|
|
|
|
virtual void fetch(int index, int &argc, char *argv[]) = 0;
|
2016-06-19 20:06:29 +12:00
|
|
|
void print(std::ostream &out) const override;
|
2016-03-19 06:57:51 +13:00
|
|
|
};
|
|
|
|
|
|
|
|
//---------------------------------------------------------
|
|
|
|
|
2016-06-15 18:43:10 +12:00
|
|
|
class DVAPI SimpleQualifier : public Qualifier {
|
2016-03-19 06:57:51 +13:00
|
|
|
public:
|
2016-06-15 18:43:10 +12:00
|
|
|
SimpleQualifier(std::string name, std::string help) : Qualifier(name, help){};
|
|
|
|
~SimpleQualifier(){};
|
2016-06-19 20:06:29 +12:00
|
|
|
void fetch(int index, int &argc, char *argv[]) override;
|
|
|
|
void dumpValue(std::ostream &out) const override;
|
|
|
|
void resetValue() override;
|
2016-03-19 06:57:51 +13:00
|
|
|
};
|
|
|
|
|
|
|
|
//---------------------------------------------------------
|
|
|
|
|
2016-06-29 18:17:12 +12:00
|
|
|
class DVAPI Switcher final : public SimpleQualifier {
|
2016-03-19 06:57:51 +13:00
|
|
|
public:
|
2016-06-15 18:43:10 +12:00
|
|
|
Switcher(std::string name, std::string help) : SimpleQualifier(name, help) {
|
|
|
|
m_switcher = true;
|
|
|
|
};
|
|
|
|
~Switcher(){};
|
2016-03-19 06:57:51 +13:00
|
|
|
};
|
|
|
|
|
|
|
|
//---------------------------------------------------------
|
|
|
|
|
|
|
|
template <class T>
|
2016-06-29 18:17:12 +12:00
|
|
|
class QualifierT final : public Qualifier {
|
2016-06-15 18:43:10 +12:00
|
|
|
T m_value;
|
2016-03-19 06:57:51 +13:00
|
|
|
|
|
|
|
public:
|
2016-06-15 18:43:10 +12:00
|
|
|
QualifierT<T>(std::string name, std::string help)
|
|
|
|
: Qualifier(name, help), m_value(){};
|
|
|
|
~QualifierT<T>(){};
|
|
|
|
|
|
|
|
T getValue() const { return m_value; };
|
|
|
|
|
2016-06-19 20:06:29 +12:00
|
|
|
void fetch(int index, int &argc, char *argv[]) override {
|
2016-06-15 18:43:10 +12:00
|
|
|
if (index + 1 >= argc) throw UsageError("missing argument");
|
|
|
|
if (!fromStr(m_value, argv[index + 1]))
|
|
|
|
throw UsageError(m_name + ": bad argument type /" +
|
|
|
|
std::string(argv[index + 1]) + "/");
|
|
|
|
for (int i = index; i < argc - 1; i++) argv[i] = argv[i + 2];
|
|
|
|
argc -= 2;
|
|
|
|
};
|
|
|
|
|
2016-06-19 20:06:29 +12:00
|
|
|
void dumpValue(std::ostream &out) const override {
|
2016-06-15 18:43:10 +12:00
|
|
|
out << m_name << " = " << (isSelected() ? "on" : "off") << " : " << m_value
|
|
|
|
<< "\n";
|
|
|
|
};
|
|
|
|
|
2016-06-19 20:06:29 +12:00
|
|
|
void resetValue() override {
|
2016-06-15 18:43:10 +12:00
|
|
|
m_value = T();
|
|
|
|
m_selected = false;
|
|
|
|
};
|
2016-03-19 06:57:51 +13:00
|
|
|
};
|
|
|
|
|
|
|
|
//=========================================================
|
|
|
|
|
2016-06-15 18:43:10 +12:00
|
|
|
class DVAPI Argument : public UsageElement {
|
2016-03-19 06:57:51 +13:00
|
|
|
public:
|
2016-06-15 18:43:10 +12:00
|
|
|
Argument(std::string name, std::string help) : UsageElement(name, help){};
|
|
|
|
~Argument(){};
|
|
|
|
virtual void fetch(int index, int &argc, char *argv[]);
|
|
|
|
virtual bool assign(char *) = 0;
|
2016-06-19 20:06:29 +12:00
|
|
|
bool isArgument() const override { return true; };
|
2016-03-19 06:57:51 +13:00
|
|
|
};
|
|
|
|
|
|
|
|
//---------------------------------------------------------
|
|
|
|
|
|
|
|
template <class T>
|
2016-06-29 18:17:12 +12:00
|
|
|
class ArgumentT final : public Argument {
|
2016-06-15 18:43:10 +12:00
|
|
|
T m_value;
|
2016-03-19 06:57:51 +13:00
|
|
|
|
|
|
|
public:
|
2016-06-15 18:43:10 +12:00
|
|
|
ArgumentT<T>(std::string name, std::string help) : Argument(name, help){};
|
|
|
|
~ArgumentT<T>(){};
|
|
|
|
operator T() const { return m_value; };
|
|
|
|
T getValue() const { return m_value; };
|
|
|
|
|
2016-06-19 20:06:29 +12:00
|
|
|
bool assign(char *src) override { return fromStr(m_value, src); };
|
|
|
|
void dumpValue(std::ostream &out) const override {
|
2016-06-15 18:43:10 +12:00
|
|
|
out << m_name << " = " << m_value << "\n";
|
|
|
|
};
|
2016-06-19 20:06:29 +12:00
|
|
|
void resetValue() override {
|
2016-06-15 18:43:10 +12:00
|
|
|
m_value = T();
|
|
|
|
m_selected = false;
|
|
|
|
};
|
2016-03-19 06:57:51 +13:00
|
|
|
};
|
|
|
|
|
|
|
|
//=========================================================
|
|
|
|
|
2016-06-15 18:43:10 +12:00
|
|
|
class DVAPI MultiArgument : public Argument {
|
2016-03-19 06:57:51 +13:00
|
|
|
protected:
|
2016-06-15 18:43:10 +12:00
|
|
|
int m_count, m_index;
|
2016-03-19 06:57:51 +13:00
|
|
|
|
|
|
|
public:
|
2016-06-15 18:43:10 +12:00
|
|
|
MultiArgument(std::string name, std::string help)
|
|
|
|
: Argument(name, help), m_count(0), m_index(0){};
|
|
|
|
~MultiArgument(){};
|
|
|
|
int getCount() const { return m_count; };
|
2016-06-19 20:06:29 +12:00
|
|
|
|
|
|
|
void fetch(int index, int &argc, char *argv[]) override;
|
|
|
|
bool isMultiArgument() const override { return true; };
|
2016-06-15 18:43:10 +12:00
|
|
|
virtual void allocate(int count) = 0;
|
2016-03-19 06:57:51 +13:00
|
|
|
};
|
|
|
|
|
|
|
|
//---------------------------------------------------------
|
|
|
|
|
|
|
|
template <class T>
|
2016-06-29 18:17:12 +12:00
|
|
|
class MultiArgumentT final : public MultiArgument {
|
2016-06-15 18:43:10 +12:00
|
|
|
std::unique_ptr<T[]> m_values;
|
2016-03-19 06:57:51 +13:00
|
|
|
|
|
|
|
public:
|
2016-06-15 18:43:10 +12:00
|
|
|
MultiArgumentT(std::string name, std::string help)
|
|
|
|
: MultiArgument(name, help) {}
|
|
|
|
T operator[](int index) {
|
|
|
|
assert(0 <= index && index < m_count);
|
|
|
|
return m_values[index];
|
|
|
|
};
|
2016-06-19 20:06:29 +12:00
|
|
|
|
|
|
|
bool assign(char *src) override {
|
2016-06-15 18:43:10 +12:00
|
|
|
assert(0 <= m_index && m_index < m_count);
|
|
|
|
return fromStr(m_values[m_index], src);
|
|
|
|
};
|
|
|
|
|
2016-06-19 20:06:29 +12:00
|
|
|
void dumpValue(std::ostream &out) const override {
|
2016-06-15 18:43:10 +12:00
|
|
|
out << m_name << " = {";
|
|
|
|
for (int i = 0; i < m_count; i++) out << " " << m_values[i];
|
|
|
|
out << "}" << std::endl;
|
|
|
|
};
|
|
|
|
|
2016-06-19 20:06:29 +12:00
|
|
|
void resetValue() override {
|
2016-06-15 18:43:10 +12:00
|
|
|
m_values.reset();
|
|
|
|
m_count = m_index = 0;
|
|
|
|
};
|
|
|
|
|
2016-06-19 20:06:29 +12:00
|
|
|
void allocate(int count) override {
|
2016-06-15 18:43:10 +12:00
|
|
|
m_values.reset((count > 0) ? new T[count] : nullptr);
|
|
|
|
m_count = count;
|
|
|
|
m_index = 0;
|
|
|
|
};
|
2016-03-19 06:57:51 +13:00
|
|
|
};
|
|
|
|
|
|
|
|
//=========================================================
|
|
|
|
|
|
|
|
typedef UsageElement *UsageElementPtr;
|
|
|
|
|
|
|
|
//---------------------------------------------------------
|
|
|
|
|
2016-06-15 18:43:10 +12:00
|
|
|
class DVAPI UsageLine {
|
2016-03-19 06:57:51 +13:00
|
|
|
protected:
|
2016-06-15 18:43:10 +12:00
|
|
|
std::unique_ptr<UsageElementPtr[]> m_elements;
|
|
|
|
int m_count;
|
2016-03-19 06:57:51 +13:00
|
|
|
|
|
|
|
public:
|
2016-06-15 18:43:10 +12:00
|
|
|
UsageLine();
|
|
|
|
virtual ~UsageLine();
|
|
|
|
UsageLine(const UsageLine &ul);
|
|
|
|
UsageLine &operator=(const UsageLine &ul);
|
|
|
|
|
|
|
|
UsageLine(int count);
|
|
|
|
UsageLine(const UsageLine &, UsageElement &elem);
|
|
|
|
UsageLine(UsageElement &elem);
|
|
|
|
UsageLine(UsageElement &a, UsageElement &b);
|
|
|
|
|
|
|
|
UsageLine operator+(UsageElement &);
|
|
|
|
|
|
|
|
int getCount() const { return m_count; };
|
|
|
|
UsageElementPtr &operator[](int index) { return m_elements[index]; };
|
|
|
|
const UsageElementPtr &operator[](int index) const {
|
|
|
|
return m_elements[index];
|
|
|
|
};
|
2016-03-19 06:57:51 +13:00
|
|
|
};
|
|
|
|
|
|
|
|
//---------------------------------------------------------
|
|
|
|
|
|
|
|
DVAPI UsageLine operator+(UsageElement &a, UsageElement &b);
|
|
|
|
|
|
|
|
//---------------------------------------------------------
|
|
|
|
|
2016-06-29 18:17:12 +12:00
|
|
|
class DVAPI Optional final : public UsageLine {
|
2016-03-19 06:57:51 +13:00
|
|
|
public:
|
2016-06-15 18:43:10 +12:00
|
|
|
Optional(const UsageLine &ul);
|
|
|
|
~Optional(){};
|
2016-03-19 06:57:51 +13:00
|
|
|
};
|
|
|
|
|
|
|
|
//---------------------------------------------------------
|
|
|
|
|
|
|
|
DVAPI UsageLine operator+(const UsageLine &a, const Optional &b);
|
|
|
|
|
|
|
|
//=========================================================
|
|
|
|
|
|
|
|
class UsageImp;
|
|
|
|
|
2016-06-15 18:43:10 +12:00
|
|
|
class DVAPI Usage {
|
|
|
|
std::unique_ptr<UsageImp> m_imp;
|
2016-03-19 06:57:51 +13:00
|
|
|
|
|
|
|
public:
|
2016-06-15 18:43:10 +12:00
|
|
|
Usage(std::string progName);
|
|
|
|
~Usage();
|
|
|
|
void add(const UsageLine &);
|
2016-03-19 06:57:51 +13:00
|
|
|
|
2016-06-15 18:43:10 +12:00
|
|
|
void print(std::ostream &out) const;
|
|
|
|
void dumpValues(std::ostream &out) const; // per debug
|
2016-03-19 06:57:51 +13:00
|
|
|
|
2016-06-15 18:43:10 +12:00
|
|
|
bool parse(int argc, char *argv[], std::ostream &err = std::cerr);
|
|
|
|
bool parse(const char *argvString, std::ostream &err = std::cerr);
|
|
|
|
void clear(); // per debug
|
2016-03-19 06:57:51 +13:00
|
|
|
|
|
|
|
private:
|
2016-06-15 18:43:10 +12:00
|
|
|
// not implemented
|
|
|
|
Usage(const Usage &);
|
|
|
|
Usage &operator=(const Usage &);
|
2016-03-19 06:57:51 +13:00
|
|
|
};
|
|
|
|
|
|
|
|
//=========================================================
|
|
|
|
|
|
|
|
typedef QualifierT<int> IntQualifier;
|
|
|
|
typedef QualifierT<double> DoubleQualifier;
|
2016-04-19 19:32:17 +12:00
|
|
|
typedef QualifierT<std::string> StringQualifier;
|
2016-03-19 06:57:51 +13:00
|
|
|
|
|
|
|
typedef ArgumentT<int> IntArgument;
|
|
|
|
typedef ArgumentT<double> DoubleArgument;
|
2016-04-19 19:32:17 +12:00
|
|
|
typedef ArgumentT<std::string> StringArgument;
|
2016-03-19 06:57:51 +13:00
|
|
|
typedef ArgumentT<TFilePath> FilePathArgument;
|
|
|
|
|
|
|
|
typedef MultiArgumentT<int> IntMultiArgument;
|
|
|
|
typedef MultiArgumentT<double> DoubleMultiArgument;
|
2016-04-19 19:32:17 +12:00
|
|
|
typedef MultiArgumentT<std::string> StringMultiArgument;
|
2016-03-19 06:57:51 +13:00
|
|
|
typedef MultiArgumentT<TFilePath> FilePathMultiArgument;
|
|
|
|
|
|
|
|
//=========================================================
|
|
|
|
|
2016-06-29 18:17:12 +12:00
|
|
|
class DVAPI RangeQualifier final : public Qualifier {
|
2016-06-15 18:43:10 +12:00
|
|
|
int m_from, m_to;
|
2016-03-19 06:57:51 +13:00
|
|
|
|
|
|
|
public:
|
2016-06-15 18:43:10 +12:00
|
|
|
RangeQualifier();
|
|
|
|
~RangeQualifier(){};
|
|
|
|
|
|
|
|
int getFrom() const { return m_from; };
|
|
|
|
int getTo() const { return m_to; };
|
|
|
|
bool contains(int frame) const { return m_from <= frame && frame <= m_to; };
|
2016-06-19 20:06:29 +12:00
|
|
|
void fetch(int index, int &argc, char *argv[]) override;
|
|
|
|
void dumpValue(std::ostream &out) const override;
|
|
|
|
void resetValue() override;
|
2016-03-19 06:57:51 +13:00
|
|
|
};
|
|
|
|
|
|
|
|
//=========================================================
|
|
|
|
|
2016-06-15 18:43:10 +12:00
|
|
|
} // namespace TCli
|
2016-03-19 06:57:51 +13:00
|
|
|
|
2016-06-15 18:43:10 +12:00
|
|
|
#endif // TCLI_INCLUDED
|