tailhook / rust-argparse

The command-line argument parser library for rust
MIT License
240 stars 39 forks source link

.required() on Collect argument does not enforce at least one value #23

Open swiftcoder opened 9 years ago

swiftcoder commented 9 years ago

I set .required() on an argument configured to Collect, with the expectation that an error would occur if the user did not pass in at least one value. This does not appear to be the case, as can be demonstrated by running the example program without any arguments:


extern crate argparse;

use argparse::{ArgumentParser, Collect};

fn main() {
    let mut input_files : Vec<String> = Vec::new();

    {
        let mut ap = ArgumentParser::new();
        ap.set_description("compile a program.");
        ap.refer(&mut input_files)
            .add_argument("input-files", Collect, "one or more input files")
            .required();
        ap.parse_args_or_exit();
    }

    println!("success");
}
nlevitt commented 5 years ago

+1 years later 😬

scardine commented 5 years ago

As a Pythonista I'm at home with this library, thanks for writing it.

I'm trying to learn Rust and would be glad to fix this but to be honest I'm a bit lost. Anyone kind enough to nudge me in the right direction?

scardine commented 5 years ago

I'm looking at check_required in parser.rs:423:

https://github.com/tailhook/rust-argparse/blob/3b3848a9a1f46abbe5353cdef9eddd0fc671d7fd/src/parser.rs#L423-L432:

When we check for "input-files", self.parser.arguments is empty, hence the for loop body is never executed. My first thought was adding information if var is positional or not, then if required and self.parser.arguments is empty we could panic.

The debugger I'm using with Visual Studio Code is pretty much useless for hashmaps, is there a better option?