Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

User-defined arguments #69

Open
daniele77 opened this issue Jun 15, 2020 · 1 comment
Open

User-defined arguments #69

daniele77 opened this issue Jun 15, 2020 · 1 comment
Assignees

Comments

@daniele77
Copy link
Owner

daniele77 commented Jun 15, 2020

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
Copy link
Owner Author

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

1 participant