Shinya Kitaoka 810553
#pragma once
Shinya Kitaoka 810553
Toshihiro Shimizu 890ddd
#ifndef TCLI_INCLUDED
Toshihiro Shimizu 890ddd
#define TCLI_INCLUDED
Toshihiro Shimizu 890ddd
Shinya Kitaoka 262a92
#include <memory></memory>
Shinya Kitaoka 262a92
Toshihiro Shimizu 890ddd
//#include "tcommon.h"   contenuto in tconvert.h
Toshihiro Shimizu 890ddd
#include "tconvert.h"
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
#include "tfilepath.h"
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
#undef DVAPI
Toshihiro Shimizu 890ddd
#undef DVVAR
Toshihiro Shimizu 890ddd
#ifdef TAPPTOOLS_EXPORTS
Toshihiro Shimizu 890ddd
#define DVAPI DV_EXPORT_API
Toshihiro Shimizu 890ddd
#define DVVAR DV_EXPORT_VAR
Toshihiro Shimizu 890ddd
#else
Toshihiro Shimizu 890ddd
#define DVAPI DV_IMPORT_API
Toshihiro Shimizu 890ddd
#define DVVAR DV_IMPORT_VAR
Toshihiro Shimizu 890ddd
#endif
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//=========================================================
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
// forward declaration
Toshihiro Shimizu 890ddd
class TFilePath;
Toshihiro Shimizu 890ddd
//=========================================================
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
namespace TCli {
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//=========================================================
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
inline bool fromStr(int &value, std::string s) {
Shinya Kitaoka 120a6e
  if (isInt(s)) {
Shinya Kitaoka 120a6e
    value = std::stoi(s);
Shinya Kitaoka 120a6e
    return true;
Shinya Kitaoka 120a6e
  } else
Shinya Kitaoka 120a6e
    return false;
Toshihiro Shimizu 890ddd
}
Shinya Kitaoka 120a6e
inline bool fromStr(double &value, std::string s) {
Shinya Kitaoka 120a6e
  if (isDouble(s)) {
Shinya Kitaoka 120a6e
    value = std::stod(s);
Shinya Kitaoka 120a6e
    return true;
Shinya Kitaoka 120a6e
  } else
Shinya Kitaoka 120a6e
    return false;
Toshihiro Shimizu 890ddd
}
Shinya Kitaoka 120a6e
inline bool fromStr(std::string &value, std::string s) {
Shinya Kitaoka 120a6e
  value = s;
Shinya Kitaoka 120a6e
  return true;
Toshihiro Shimizu 890ddd
}
Shinya Kitaoka 120a6e
inline bool fromStr(TFilePath &value, std::string s) {
Shinya Kitaoka 120a6e
  value = TFilePath(s);
Shinya Kitaoka 120a6e
  return true;
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//=========================================================
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
class UsageError {
Shinya Kitaoka 120a6e
  std::string m_msg;
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
public:
Shinya Kitaoka 120a6e
  UsageError(std::string msg) : m_msg(msg){};
Shinya Kitaoka 120a6e
  ~UsageError(){};
Shinya Kitaoka 120a6e
  std::string getError() const { return m_msg; };
Toshihiro Shimizu 890ddd
};
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//=========================================================
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
class DVAPI UsageElement {
Toshihiro Shimizu 890ddd
protected:
Shinya Kitaoka 120a6e
  std::string m_name, m_help;
Shinya Kitaoka 120a6e
  bool m_selected;
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
public:
Shinya Kitaoka 120a6e
  UsageElement(std::string name, std::string help);
Shinya Kitaoka 120a6e
  virtual ~UsageElement(){};
Shinya Kitaoka 120a6e
  std::string getName() const { return m_name; };
Shinya Kitaoka 120a6e
  bool isSelected() const { return m_selected; };
Shinya Kitaoka 120a6e
  void select() { m_selected = true; };
Shinya Kitaoka 120a6e
Shinya Kitaoka 120a6e
  virtual bool isHidden() const { return false; };
Shinya Kitaoka 120a6e
  virtual bool isSwitcher() const { return false; };
Shinya Kitaoka 120a6e
  virtual bool isArgument() const { return false; };
Shinya Kitaoka 120a6e
  virtual bool isMultiArgument() const { return false; };
Shinya Kitaoka 120a6e
  void setHelp(std::string help) { m_help = help; };
Shinya Kitaoka 120a6e
Shinya Kitaoka 120a6e
  virtual void print(std::ostream &out) const;
Shinya Kitaoka 120a6e
  virtual void printHelpLine(std::ostream &out) const;
Shinya Kitaoka 120a6e
  virtual void dumpValue(std::ostream &out) const = 0;
Shinya Kitaoka 120a6e
  virtual void resetValue()                       = 0;
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
private:
Shinya Kitaoka 120a6e
  // not implemented
Shinya Kitaoka 120a6e
  UsageElement(const UsageElement &);
Shinya Kitaoka 120a6e
  UsageElement &operator=(const UsageElement &);
Toshihiro Shimizu 890ddd
};
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//=========================================================
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
class DVAPI Qualifier : public UsageElement {
Toshihiro Shimizu 890ddd
protected:
Shinya Kitaoka 120a6e
  bool m_switcher;
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
public:
Shinya Kitaoka 120a6e
  Qualifier(std::string name, std::string help)
Shinya Kitaoka 120a6e
      : UsageElement(name, help), m_switcher(false){};
Shinya Kitaoka 120a6e
  ~Qualifier(){};
Toshihiro Shimizu 890ddd
Shinya Kitaoka 473e70
  bool isSwitcher() const override { return m_switcher; };
Shinya Kitaoka 473e70
Shinya Kitaoka 473e70
  bool isHidden() const override { return m_help == ""; };
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
  operator bool() const { return isSelected(); };
Shinya Kitaoka 120a6e
  virtual void fetch(int index, int &argc, char *argv[]) = 0;
Shinya Kitaoka 473e70
  void print(std::ostream &out) const override;
Toshihiro Shimizu 890ddd
};
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//---------------------------------------------------------
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
class DVAPI SimpleQualifier : public Qualifier {
Toshihiro Shimizu 890ddd
public:
Shinya Kitaoka 120a6e
  SimpleQualifier(std::string name, std::string help) : Qualifier(name, help){};
Shinya Kitaoka 120a6e
  ~SimpleQualifier(){};
Shinya Kitaoka 473e70
  void fetch(int index, int &argc, char *argv[]) override;
Shinya Kitaoka 473e70
  void dumpValue(std::ostream &out) const override;
Shinya Kitaoka 473e70
  void resetValue() override;
Toshihiro Shimizu 890ddd
};
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//---------------------------------------------------------
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
class DVAPI Switcher : public SimpleQualifier {
Toshihiro Shimizu 890ddd
public:
Shinya Kitaoka 120a6e
  Switcher(std::string name, std::string help) : SimpleQualifier(name, help) {
Shinya Kitaoka 120a6e
    m_switcher = true;
Shinya Kitaoka 120a6e
  };
Shinya Kitaoka 120a6e
  ~Switcher(){};
Toshihiro Shimizu 890ddd
};
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//---------------------------------------------------------
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
template <class t=""></class>
Shinya Kitaoka 120a6e
class QualifierT : public Qualifier {
Shinya Kitaoka 120a6e
  T m_value;
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
public:
Shinya Kitaoka 120a6e
  QualifierT<t>(std::string name, std::string help)</t>
Shinya Kitaoka 120a6e
      : Qualifier(name, help), m_value(){};
Shinya Kitaoka 120a6e
  ~QualifierT<t>(){};</t>
Shinya Kitaoka 120a6e
Shinya Kitaoka 120a6e
  T getValue() const { return m_value; };
Shinya Kitaoka 120a6e
Shinya Kitaoka 473e70
  void fetch(int index, int &argc, char *argv[]) override {
Shinya Kitaoka 120a6e
    if (index + 1 >= argc) throw UsageError("missing argument");
Shinya Kitaoka 120a6e
    if (!fromStr(m_value, argv[index + 1]))
Shinya Kitaoka 120a6e
      throw UsageError(m_name + ": bad argument type /" +
Shinya Kitaoka 120a6e
                       std::string(argv[index + 1]) + "/");
Shinya Kitaoka 120a6e
    for (int i = index; i < argc - 1; i++) argv[i] = argv[i + 2];
Shinya Kitaoka 120a6e
    argc -= 2;
Shinya Kitaoka 120a6e
  };
Shinya Kitaoka 120a6e
Shinya Kitaoka 473e70
  void dumpValue(std::ostream &out) const override {
Shinya Kitaoka 120a6e
    out << m_name << " = " << (isSelected() ? "on" : "off") << " : " << m_value
Shinya Kitaoka 120a6e
        << "\n";
Shinya Kitaoka 120a6e
  };
Shinya Kitaoka 120a6e
Shinya Kitaoka 473e70
  void resetValue() override {
Shinya Kitaoka 120a6e
    m_value    = T();
Shinya Kitaoka 120a6e
    m_selected = false;
Shinya Kitaoka 120a6e
  };
Toshihiro Shimizu 890ddd
};
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//=========================================================
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
class DVAPI Argument : public UsageElement {
Toshihiro Shimizu 890ddd
public:
Shinya Kitaoka 120a6e
  Argument(std::string name, std::string help) : UsageElement(name, help){};
Shinya Kitaoka 120a6e
  ~Argument(){};
Shinya Kitaoka 120a6e
  virtual void fetch(int index, int &argc, char *argv[]);
Shinya Kitaoka 120a6e
  virtual bool assign(char *) = 0;
Shinya Kitaoka 473e70
  bool isArgument() const override { return true; };
Toshihiro Shimizu 890ddd
};
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//---------------------------------------------------------
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
template <class t=""></class>
Shinya Kitaoka 120a6e
class ArgumentT : public Argument {
Shinya Kitaoka 120a6e
  T m_value;
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
public:
Shinya Kitaoka 120a6e
  ArgumentT<t>(std::string name, std::string help) : Argument(name, help){};</t>
Shinya Kitaoka 120a6e
  ~ArgumentT<t>(){};</t>
Shinya Kitaoka 120a6e
  operator T() const { return m_value; };
Shinya Kitaoka 120a6e
  T getValue() const { return m_value; };
Shinya Kitaoka 120a6e
Shinya Kitaoka 473e70
  bool assign(char *src) override { return fromStr(m_value, src); };
Shinya Kitaoka 473e70
  void dumpValue(std::ostream &out) const override {
Shinya Kitaoka 120a6e
    out << m_name << " = " << m_value << "\n";
Shinya Kitaoka 120a6e
  };
Shinya Kitaoka 473e70
  void resetValue() override {
Shinya Kitaoka 120a6e
    m_value    = T();
Shinya Kitaoka 120a6e
    m_selected = false;
Shinya Kitaoka 120a6e
  };
Toshihiro Shimizu 890ddd
};
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//=========================================================
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
class DVAPI MultiArgument : public Argument {
Toshihiro Shimizu 890ddd
protected:
Shinya Kitaoka 120a6e
  int m_count, m_index;
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
public:
Shinya Kitaoka 120a6e
  MultiArgument(std::string name, std::string help)
Shinya Kitaoka 120a6e
      : Argument(name, help), m_count(0), m_index(0){};
Shinya Kitaoka 120a6e
  ~MultiArgument(){};
Shinya Kitaoka 120a6e
  int getCount() const { return m_count; };
Shinya Kitaoka 473e70
Shinya Kitaoka 473e70
  void fetch(int index, int &argc, char *argv[]) override;
Shinya Kitaoka 473e70
  bool isMultiArgument() const override { return true; };
Shinya Kitaoka 120a6e
  virtual void allocate(int count) = 0;
Toshihiro Shimizu 890ddd
};
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//---------------------------------------------------------
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
template <class t=""></class>
Shinya Kitaoka 120a6e
class MultiArgumentT : public MultiArgument {
Shinya Kitaoka 120a6e
  std::unique_ptr<t[]> m_values;</t[]>
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
public:
Shinya Kitaoka 120a6e
  MultiArgumentT(std::string name, std::string help)
Shinya Kitaoka 120a6e
      : MultiArgument(name, help) {}
Shinya Kitaoka 120a6e
  T operator[](int index) {
Shinya Kitaoka 120a6e
    assert(0 <= index && index < m_count);
Shinya Kitaoka 120a6e
    return m_values[index];
Shinya Kitaoka 120a6e
  };
Shinya Kitaoka 473e70
Shinya Kitaoka 473e70
  bool assign(char *src) override {
Shinya Kitaoka 120a6e
    assert(0 <= m_index && m_index < m_count);
Shinya Kitaoka 120a6e
    return fromStr(m_values[m_index], src);
Shinya Kitaoka 120a6e
  };
Shinya Kitaoka 120a6e
Shinya Kitaoka 473e70
  void dumpValue(std::ostream &out) const override {
Shinya Kitaoka 120a6e
    out << m_name << " = {";
Shinya Kitaoka 120a6e
    for (int i = 0; i < m_count; i++) out << " " << m_values[i];
Shinya Kitaoka 120a6e
    out << "}" << std::endl;
Shinya Kitaoka 120a6e
  };
Shinya Kitaoka 120a6e
Shinya Kitaoka 473e70
  void resetValue() override {
Shinya Kitaoka 120a6e
    m_values.reset();
Shinya Kitaoka 120a6e
    m_count = m_index = 0;
Shinya Kitaoka 120a6e
  };
Shinya Kitaoka 120a6e
Shinya Kitaoka 473e70
  void allocate(int count) override {
Shinya Kitaoka 120a6e
    m_values.reset((count > 0) ? new T[count] : nullptr);
Shinya Kitaoka 120a6e
    m_count = count;
Shinya Kitaoka 120a6e
    m_index = 0;
Shinya Kitaoka 120a6e
  };
Toshihiro Shimizu 890ddd
};
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//=========================================================
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
typedef UsageElement *UsageElementPtr;
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//---------------------------------------------------------
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
class DVAPI UsageLine {
Toshihiro Shimizu 890ddd
protected:
Shinya Kitaoka 120a6e
  std::unique_ptr<usageelementptr[]> m_elements;</usageelementptr[]>
Shinya Kitaoka 120a6e
  int m_count;
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
public:
Shinya Kitaoka 120a6e
  UsageLine();
Shinya Kitaoka 120a6e
  virtual ~UsageLine();
Shinya Kitaoka 120a6e
  UsageLine(const UsageLine &ul);
Shinya Kitaoka 120a6e
  UsageLine &operator=(const UsageLine &ul);
Shinya Kitaoka 120a6e
Shinya Kitaoka 120a6e
  UsageLine(int count);
Shinya Kitaoka 120a6e
  UsageLine(const UsageLine &, UsageElement &elem);
Shinya Kitaoka 120a6e
  UsageLine(UsageElement &elem);
Shinya Kitaoka 120a6e
  UsageLine(UsageElement &a, UsageElement &b);
Shinya Kitaoka 120a6e
Shinya Kitaoka 120a6e
  UsageLine operator+(UsageElement &);
Shinya Kitaoka 120a6e
Shinya Kitaoka 120a6e
  int getCount() const { return m_count; };
Shinya Kitaoka 120a6e
  UsageElementPtr &operator[](int index) { return m_elements[index]; };
Shinya Kitaoka 120a6e
  const UsageElementPtr &operator[](int index) const {
Shinya Kitaoka 120a6e
    return m_elements[index];
Shinya Kitaoka 120a6e
  };
Toshihiro Shimizu 890ddd
};
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//---------------------------------------------------------
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
DVAPI UsageLine operator+(UsageElement &a, UsageElement &b);
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//---------------------------------------------------------
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
class DVAPI Optional : public UsageLine {
Toshihiro Shimizu 890ddd
public:
Shinya Kitaoka 120a6e
  Optional(const UsageLine &ul);
Shinya Kitaoka 120a6e
  ~Optional(){};
Toshihiro Shimizu 890ddd
};
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//---------------------------------------------------------
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
DVAPI UsageLine operator+(const UsageLine &a, const Optional &b);
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//=========================================================
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
class UsageImp;
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
class DVAPI Usage {
Shinya Kitaoka 120a6e
  std::unique_ptr<usageimp> m_imp;</usageimp>
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
public:
Shinya Kitaoka 120a6e
  Usage(std::string progName);
Shinya Kitaoka 120a6e
  ~Usage();
Shinya Kitaoka 120a6e
  void add(const UsageLine &);
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
  void print(std::ostream &out) const;
Shinya Kitaoka 120a6e
  void dumpValues(std::ostream &out) const;  // per debug
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
  bool parse(int argc, char *argv[], std::ostream &err = std::cerr);
Shinya Kitaoka 120a6e
  bool parse(const char *argvString, std::ostream &err = std::cerr);
Shinya Kitaoka 120a6e
  void clear();  // per debug
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
private:
Shinya Kitaoka 120a6e
  // not implemented
Shinya Kitaoka 120a6e
  Usage(const Usage &);
Shinya Kitaoka 120a6e
  Usage &operator=(const Usage &);
Toshihiro Shimizu 890ddd
};
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//=========================================================
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
typedef QualifierT<int> IntQualifier;</int>
Toshihiro Shimizu 890ddd
typedef QualifierT<double> DoubleQualifier;</double>
Shinya Kitaoka 3bfa54
typedef QualifierT<std::string> StringQualifier;</std::string>
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
typedef ArgumentT<int> IntArgument;</int>
Toshihiro Shimizu 890ddd
typedef ArgumentT<double> DoubleArgument;</double>
Shinya Kitaoka 3bfa54
typedef ArgumentT<std::string> StringArgument;</std::string>
Toshihiro Shimizu 890ddd
typedef ArgumentT<tfilepath> FilePathArgument;</tfilepath>
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
typedef MultiArgumentT<int> IntMultiArgument;</int>
Toshihiro Shimizu 890ddd
typedef MultiArgumentT<double> DoubleMultiArgument;</double>
Shinya Kitaoka 3bfa54
typedef MultiArgumentT<std::string> StringMultiArgument;</std::string>
Toshihiro Shimizu 890ddd
typedef MultiArgumentT<tfilepath> FilePathMultiArgument;</tfilepath>
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//=========================================================
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
class DVAPI RangeQualifier : public Qualifier {
Shinya Kitaoka 120a6e
  int m_from, m_to;
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
public:
Shinya Kitaoka 120a6e
  RangeQualifier();
Shinya Kitaoka 120a6e
  ~RangeQualifier(){};
Shinya Kitaoka 120a6e
Shinya Kitaoka 120a6e
  int getFrom() const { return m_from; };
Shinya Kitaoka 120a6e
  int getTo() const { return m_to; };
Shinya Kitaoka 120a6e
  bool contains(int frame) const { return m_from <= frame && frame <= m_to; };
Shinya Kitaoka 473e70
  void fetch(int index, int &argc, char *argv[]) override;
Shinya Kitaoka 473e70
  void dumpValue(std::ostream &out) const override;
Shinya Kitaoka 473e70
  void resetValue() override;
Toshihiro Shimizu 890ddd
};
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//=========================================================
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
}  // namespace TCli
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
#endif  // TCLI_INCLUDED