Skip to content

Commit

Permalink
implement required named arguments
Browse files Browse the repository at this point in the history
  • Loading branch information
cherusk committed Oct 12, 2020
1 parent d2b0fdd commit 9961304
Show file tree
Hide file tree
Showing 3 changed files with 10 additions and 3 deletions.
3 changes: 3 additions & 0 deletions src/command.rs
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ impl Command {
long: "verbose".to_string(),
multiple: false,
takes_value: false,
required: false,
validate_as_number: false,
val: "".to_string(),
});
Expand Down Expand Up @@ -85,6 +86,7 @@ pub struct OptionFlag {
pub multiple: bool, // Can it have multiple values? (-vvv OR -i one -i two)
pub takes_value: bool, // Does it take a value? (-i value)
pub validate_as_number: bool, // Should we validate it as a number?
pub required: bool,
pub val: String,
}

Expand All @@ -97,6 +99,7 @@ impl OptionFlag {
long: "".to_string(),
multiple: false,
takes_value: false,
required: false,
validate_as_number: false,
val: "".to_string(),
}
Expand Down
7 changes: 4 additions & 3 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -101,20 +101,21 @@ fn build_subcommands<'a, 'b>(
}
}

// Add all required arguments
// Add all positional arguments
for a in &c.required_args {
let arg = Arg::with_name(&a.name).required(true);
subcmd = subcmd.arg(arg);
}

// Add all optional flags
// Add all named flags
for f in &c.option_flags {
let arg = Arg::with_name(&f.name)
.help(&f.desc)
.short(&f.short)
.long(&f.long)
.takes_value(f.takes_value)
.multiple(f.multiple);
.multiple(f.multiple)
.required(f.required);
subcmd = subcmd.arg(arg);
}
cli_app = cli_app.subcommand(subcmd);
Expand Down
3 changes: 3 additions & 0 deletions src/parser.rs
Original file line number Diff line number Diff line change
Expand Up @@ -129,6 +129,9 @@ pub fn build_command_structure(maskfile_contents: String) -> Command {
}
}
}
"required" => {
current_option_flag.required = true;
}
_ => (),
};
}
Expand Down

0 comments on commit 9961304

Please sign in to comment.