Skip to content

Allow iterating over ArgMatches and check whether a match uses default value #2759

@romange

Description

@romange

Please complete the following tasks

  • I have searched the discussions
  • I have searched the existing issues

Clap Version

3.0.0-beta

Describe your use case

Use case: I would like to merge ArgMatches into Config settings using the following rules:

  1. ArgMatches have higher precedence over Config if they are explicitly set.
  2. If config setting is present but ArgMatches has an argument with default value then config value is taken.
  3. If config does not set the setting, then ArgMatches default value it taken.

In order to do this, I would need to iterate over ArgMatches.args and query MatchedArg.ty field.

Describe the solution you'd like

Some sort of interface that allows me to do this.

Alternatives, if applicable

Currently, I have to declare all fields in clap structure with Option<> and without default values in order to expose similar semantics. For example,

struct MyOpts {
    #[clap(short, long, default_value="80")]
    port: Option<u16>,
}

instead of

struct MyOpts {
    #[clap(short, long, default_value="8080")]
    port: u16,
}

Additional Context

No response

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions