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

value_of_t_or and value_of_t_or_else #3459

Closed
2 tasks done
letheed opened this issue Feb 12, 2022 · 1 comment
Closed
2 tasks done

value_of_t_or and value_of_t_or_else #3459

letheed opened this issue Feb 12, 2022 · 1 comment
Labels
A-parsing Area: Parser's logic and needs it changed somehow. C-enhancement Category: Raise on the bar on expectations S-duplicate Status: Closed as Duplicate

Comments

@letheed
Copy link

letheed commented Feb 12, 2022

Please complete the following tasks

Clap Version

3.0.14

Describe your use case

The use case is the parsing of optional arguments where a default value is provided when there is none.

Currently I’m using this code:

const MY_ARG_DEFAULT = {};

let my_val = match args.value_of_t("MY_ARG") {
        Err(error) if error.kind() == ErrorKind::ArgumentNotFound => Ok(MY_ARG_DEFAULT),
        result => result,
    }?;

Describe the solution you'd like

Ideally I would like something like:

let my_val = args.value_of_t_or("MY_ARG", MY_ARG_DEFAULT)?;

and for more expensive values:

let my_val = args.value_of_t_or_else("MY_ARG", || <expr>)?;

I’m actually a bit surprised something like that doesn’t exist because I though the derive feature would use something like that for default_value_t instead of a print/parse roundtrip.

Alternatives, if applicable

No response

Additional Context

No response

@letheed letheed added the C-enhancement Category: Raise on the bar on expectations label Feb 12, 2022
@epage
Copy link
Member

epage commented Feb 13, 2022

I’m actually a bit surprised something like that doesn’t exist because I though the derive feature would use something like that for default_value_t instead of a print/parse roundtrip.

We've been tracking this with #2505

Keep in mind that value_of_t is new in clap 3.0, so our window of feedback has been small. We just didn't have time to change the API to how it was proposed in there. Your API proposals offer some alternatives that can be added in the mean time. Naming between an erroring vs an optional version is unclear (not really seen a pattern for that emerge).

We also will have #2683 impact some of these API choices.

I'm going to close this out in favor of continuing the conversation on #2505

@epage epage closed this as completed Feb 13, 2022
@epage epage added A-parsing Area: Parser's logic and needs it changed somehow. S-duplicate Status: Closed as Duplicate labels Feb 13, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-parsing Area: Parser's logic and needs it changed somehow. C-enhancement Category: Raise on the bar on expectations S-duplicate Status: Closed as Duplicate
Projects
None yet
Development

No branches or pull requests

2 participants