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

Can I display the default value and help message for each arguments? #30

Open
DapengFeng opened this issue Oct 23, 2024 · 2 comments
Open

Comments

@DapengFeng
Copy link

I find a similar library at https://github.com/morrisfranken/argparse?tab=readme-ov-file

Screenshot from 2024-10-23 20-30-13

It provides a feasible way to define default values and help messages.

Screenshot from 2024-10-23 20-32-34

@DapengFeng
Copy link
Author

Similar issues: #16 #17

@DapengFeng
Copy link
Author

DapengFeng commented Oct 25, 2024

Inspired by morrisfranken/argparse, I develop the similar function using template struct,

template <typename T>
struct arg
{
  T value_;
  std::string msg_;

  arg(const T& value = T(), const std::string& msg = "") : value_(value), msg_(msg) {}

  operator T&() { value_.msg_ = msg_; return value_; }

  friend std::istringstream& operator >>(std::istringstream& ss, arg& obj) { ss >> obj.value_; return ss;}
  
};

struct Options {
  struct Init : public structopt::sub_command
  {
    Init() {}

    friend std::istringstream& operator >> (std::istringstream& ss, Init& obj) {
      ss >> obj.path.value();
      return ss;
    }

    std::optional<arg<std::filesystem::path>> path = arg<std::filesystem::path>("init", "Path to the scene file");
    std::optional<arg<float>> scale = arg<float>(1.0f, "Scale of the scene");
    arg<float> density = arg<float>(float(), "Density of the scene");
  };

  Init init = arg(Init(), "Initialize a new scene");
};
STRUCTOPT(Options, init);
STRUCTOPT(Options::Init, path, scale, density);

the examples are shown as follow:

./main -h
USAGE: main [FLAGS] [SUBCOMMANDS] 

FLAGS:
    -h, --help Prints help information

    -v, --version Prints version information


SUBCOMMANDS:
    init : Initialize a new scene

and the subcommand mode

./main init -h
USAGE: init [FLAGS] [OPTIONS] density 

FLAGS:
    -h, --help Prints help information

    -v, --version Prints version information


OPTIONS:
    -p, --path Path to the scene file (default: "init")

    -s, --scale Scale of the scene (default: 1)


ARGS:
    density : Density of the scene

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

No branches or pull requests

1 participant