Skip to content

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

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

Open
swiftcoder opened this issue Jun 5, 2015 · 3 comments
Open

Comments

@swiftcoder
Copy link

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
Copy link

nlevitt commented May 5, 2019

+1 years later 😬

@scardine
Copy link

scardine commented Aug 3, 2019

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
Copy link

scardine commented Aug 3, 2019

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

rust-argparse/src/parser.rs

Lines 423 to 432 in 3b3848a

fn check_required(&mut self) -> ParseResult {
// Check for required arguments
for var in self.parser.vars.iter() {
if var.required && !self.set_vars.contains(&var.id) {
// First try positional arguments
for opt in self.parser.arguments.iter() {
if opt.varid == var.id {
return Error(format!(
"Argument {} is required", opt.name));
}
:

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?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants