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

Allow a field's type to be different than the value parser type #3912

Open
2 tasks done
epage opened this issue Jul 12, 2022 · 7 comments
Open
2 tasks done

Allow a field's type to be different than the value parser type #3912

epage opened this issue Jul 12, 2022 · 7 comments
Labels
A-derive Area: #[derive]` macro API C-enhancement Category: Raise on the bar on expectations S-waiting-on-design Status: Waiting on user-facing design to be resolved before implementing

Comments

@epage
Copy link
Member

epage commented Jul 12, 2022

Please complete the following tasks

Clap Version

3.2.8

Describe your use case

Some value parsers (Count, SetTrue, SetFalse) only support one specific data type but the user might want to support a different user type.

For example, the following parse(from_flag) test cannot be ported to value_parser

#[test]
fn non_bool_type_flag() {
    fn parse_from_flag(b: bool) -> std::sync::atomic::AtomicBool {
        std::sync::atomic::AtomicBool::new(b)
    }

    #[derive(Parser, Debug)]
    struct Opt {
        #[clap(short, long, parse(from_flag = parse_from_flag))]
        alice: std::sync::atomic::AtomicBool,
        #[clap(short, long, parse(from_flag))]
        bob: std::sync::atomic::AtomicBool,
    }

    let falsey = Opt::try_parse_from(&["test"]).unwrap();
    assert!(!falsey.alice.load(std::sync::atomic::Ordering::Relaxed));
    assert!(!falsey.bob.load(std::sync::atomic::Ordering::Relaxed));
}

(a SetConst might also work to a degree)

Others might want a different count type than u8.

Someone even brought up processing a Vec<String> into a String, see #3905 (reply in thread)

Describe the solution you'd like

The user should be able to

  • Specify the ValueParser output type to use with get_one / get_many
    • Defaults to the field's type
  • Optionally a conversion function from value parser type to field type
    • Defaults to TryInto (or Into?)

This will only be called when extracting the fields and will only be called once. We will not do a proper validation step for this.

Alternatives, if applicable

No response

Additional Context

No response

@epage epage added C-enhancement Category: Raise on the bar on expectations A-derive Area: #[derive]` macro API S-waiting-on-design Status: Waiting on user-facing design to be resolved before implementing labels Jul 12, 2022
@epage
Copy link
Member Author

epage commented Jul 12, 2022

What would we call these new attributes?

@chipsenkbeil
Copy link

chipsenkbeil commented Jul 12, 2022

If we use serde as an example, it has into, from, and try_from as attributes at the struct level for converting to a different type before serializing and deserializing. So my thought would be to use similar naming for the conversion function that you can place on an individual field. Heck, you could just have it use the corresponding trait implementations if you wanted something simpler than an arbitrary function.

In terms of output type, would just having #[clap(type = "...")] be too simple?

@itsxaos

This comment was marked as off-topic.

@epage

This comment was marked as off-topic.

@itsxaos

This comment was marked as off-topic.

@epage

This comment was marked as outdated.

@epage
Copy link
Member Author

epage commented Aug 24, 2022

Some value parsers (Count, SetTrue, SetFalse) only support one specific data type but the user might want to support a different user type.

As an update, we've loosened this restriction for SetTrue/SetFalse in the 4.0 release, see #4095 for the latest version of it

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-derive Area: #[derive]` macro API C-enhancement Category: Raise on the bar on expectations S-waiting-on-design Status: Waiting on user-facing design to be resolved before implementing
Projects
None yet
Development

No branches or pull requests

3 participants