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

Example code of README.md using builder pattern results in panic #1965

Closed
2 tasks done
asteding opened this issue Jun 8, 2020 · 6 comments
Closed
2 tasks done

Example code of README.md using builder pattern results in panic #1965

asteding opened this issue Jun 8, 2020 · 6 comments
Labels
C-bug Category: Updating dependencies E-easy Call for participation: Experience needed to fix: Easy / not much
Milestone

Comments

@asteding
Copy link

asteding commented Jun 8, 2020

Make sure you completed the following tasks

Code

// (Full example with detailed comments in examples/01a_quick_example.rs)
//
// This example demonstrates clap's "builder pattern" method of creating arguments
// which the most flexible, but also most verbose.
use clap::{Arg, App};

fn main() {
    let matches = App::new("My Super Program")
        .version("1.0")
        .author("Kevin K. <kbknapp@gmail.com>")
        .about("Does awesome things")
        .arg(Arg::new("config")
            .short('c')
            .long("config")
            .value_name("FILE")
            .about("Sets a custom config file")
            .takes_value(true))
        .arg(Arg::new("INPUT")
            .about("Sets the input file to use")
            .required(true)
            .index(1))
        .arg(Arg::new("v")
            .short('v')
            .multiple(true)
            .about("Sets the level of verbosity"))
        .subcommand(App::new("test")
            .about("controls testing features")
            .version("1.3")
            .author("Someone E. <someone_else@other.com>")
            .arg(Arg::new("debug")
                .short('d')
                .about("print debug information verbosely")))
        .get_matches();

    // Same as above examples...
}

Steps to reproduce the issue

  1. Run cargo run

Version

  • Rust: rustc 1.45.0-nightly (47c3158c3 2020-06-04)
  • Clap: 3.0.0-beta.1

Actual Behavior Summary

If i use the above example code from the clap-rs README.md i get a
thread 'main' panicked at 'Argument names must be unique, but '' is in use by more than one argument or group'

Expected Behavior Summary

I think this should happen instead.
error: The following required arguments were not provided:

USAGE:
clap_bug [FLAGS] [OPTIONS] [SUBCOMMAND]

Additional context

Debug output

Compile clap with debug feature:

[dependencies]
clap = { version = "*", features = ["debug"] }

The output may be very long, so feel free to link to a gist or attach a text file

Debug Output

Paste Debug Output Here
[ clap::build::app] App::_do_parse
[ clap::build::app] App::_build
[ clap::build::app] App::_derive_display_order:My Super Program
[ clap::build::app] App::_derive_display_order:test
[ clap::build::app] App::_create_help_and_version
[ clap::build::app] App::_create_help_and_version: Building --help
[ clap::build::app] App::_create_help_and_version: Building --version
[ clap::build::app] App::_create_help_and_version: Building help
[ clap::build::app] App::_debug_asserts
[ clap::build::arg] Arg::_debug_asserts:
thread 'main' panicked at 'Argument names must be unique, but '' is in use by more than one argument or group', /home/andi/.cargo/registry/src/github.com-1ecc6299db9ec823/clap-3.0.0-beta.1/src/build/app/mod.rs:1613:13
note: run with RUST_BACKTRACE=1 environment variable to display a backtrace

@asteding asteding added the C-bug Category: Updating dependencies label Jun 8, 2020
@pksunkara pksunkara added this to the 3.0 milestone Jun 8, 2020
@pksunkara pksunkara added D: easy E-easy Call for participation: Experience needed to fix: Easy / not much labels Jun 8, 2020
@asteding
Copy link
Author

asteding commented Jun 8, 2020

After some research it looks like the field arg.name is always ""

So

assert!(
    two_args_of(|x| x.name == arg.name).is_none(),
    "Argument names must be unique, but '{}' is in use by more than one argument or group",
    arg.name,
    );

returns always a false.

Due to my still little knowledge of Rust i'd be happy to help and learn.
Would someone provide some information where i should look? Probably I need to investigate

impl<'help> Arg<'help> {
    /// @TODO @p2 @docs @v3-beta1: Write Docs
    pub fn new<T: Key + ToString>(t: T) -> Self {
        Arg {
            id: t.into(),
            disp_ord: 999,
            unified_ord: 999,
            ..Default::default()
        }
    }
}

which is lacking a name field too.

@CreepySkeleton
Copy link
Contributor

In the first snippet, the condition must be x.id == arg.id.

Where did you get the second snippet from? I can't find it in the codebase.

@pksunkara
Copy link
Member

pksunkara commented Jun 8, 2020 via email

@asteding
Copy link
Author

asteding commented Jun 8, 2020

Hello @CreepySkeleton ,
it's from here
It also looks like it's already fixed on the master branch?

@CreepySkeleton
Copy link
Contributor

Ah, yes. There used to be Arg::new method different from with_name. We eventually decided to ditch the old new and just rename with_name to new. it happened after the beta release.

This is fixed in master. The condition must be fixed though - it's not wrong, but it would be "more correct" to check id. We would appreciate such PR!

@asteding
Copy link
Author

asteding commented Jun 8, 2020

Alright.
Thanks for the feedback.
I'll set up a PR if nobody minds? :-)

@asteding asteding closed this as completed Jun 9, 2020
kiobu added a commit to kiobu/kaste that referenced this issue Aug 30, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
C-bug Category: Updating dependencies E-easy Call for participation: Experience needed to fix: Easy / not much
Projects
None yet
Development

No branches or pull requests

3 participants