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

implement required named arguments #74

Merged
merged 1 commit into from
Dec 28, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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