cli
cli copied to clipboard
User-defined arguments
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:
- restrict to a subset of values of a type (e.g., an
intvariable that can only assume the values:{-1000, 0, 1000}or astd::stringthat can only assume the values:{"red", "green", "blue"}). - use a custom type (e.g., a struct or a class).
See also #157
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.