cli icon indicating copy to clipboard operation
cli copied to clipboard

User-defined arguments

Open daniele77 opened this issue 5 years ago • 1 comments

The library currently supports only arguments of type:

  • char
  • unsigned char
  • short
  • unsigned short
  • int
  • unsigned int
  • long
  • unsigned long
  • float
  • double
  • long double
  • bool
  • std::string
  • std::vector<std::string>

However, I see two big improvements:

  1. restrict to a subset of values of a type (e.g., an int variable that can only assume the values: {-1000, 0, 1000} or a std::string that can only assume the values: {"red", "green", "blue"}).
  2. use a custom type (e.g., a struct or a class).

See also #157

daniele77 avatar Jun 15 '20 13:06 daniele77

Actually, point 2) already works. For example, to support a custom struct foo, one can use:

struct foo
{
    friend istream & operator >> (istream &in, foo& p);
    int value;
};

istream & operator >> (istream& in, foo& f)
{
    in >> f.value;
    return in;
}

// needed only for generic help, you can omit this
namespace cli {  template <> struct TypeDesc<foo> { static const char* Name() { return "<foo>"; } }; }

You just need to provide << operator.

So, e.g., std::complex type is already supported by the library:

    rootMenu->Insert(
            "complex",
            [](std::ostream& out, complex<double> x){ out << "you entered: " << x << "\n"; },
            "Print a complex number" );

It remains to work on 1) to close this issue.

daniele77 avatar Sep 07 '22 10:09 daniele77