Skip to content

Default value and transformations #330

@jzakrzewski

Description

@jzakrzewski

I do this:

cmd->add_option("--existing", "What to do if deployment already exists in the destination")
      ->transform(CLI::CheckedTransformer(std::unordered_map<std::string, ExistingBehaviour>{
          {"abort"s, ExistingBehaviour::abort},
          {"overwrite"s, ExistingBehaviour ::overwrite},
          {"delete"s, ExistingBehaviour::remove},
          {"remove"s, ExistingBehaviour::remove}}))
      ->default_val("abort");

and all is well, the transformer runs but later (in a subcommand callback) I do:

cmd->get_option("--existing")->as<ExistingBehaviour>()

And it throws.
It turns out that, even though the transformer runs, the original default ("abort") is stored instead of (apparently expected) "0". If I actually provide "--existing=abort" on the command line, it works. I could just put "0" as default but then a) help is broken, b) if I change the enum the hell breaks loose.

It looks to me like this function is responsible:

Option *default_val(std::string val) {
default_str(val);
auto old_results = results_;
results_ = {val};
run_callback();
results_ = std::move(old_results);
return this;
}

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions