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

When specifying style multiple times the last occurence wins #371

Merged
merged 4 commits into from
Oct 21, 2018
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
11 changes: 9 additions & 2 deletions src/app.rs
Original file line number Diff line number Diff line change
Expand Up @@ -266,8 +266,15 @@ impl App {
.collect::<Result<Vec<OutputComponent>>>()
}))?;

values_t!(matches.values_of("style"), OutputComponent)
.ok()
matches
.value_of("style")
.map(|styles| {
styles
.split(",")
.map(|style| style.parse::<OutputComponent>())
.filter_map(|style| style.ok())
.collect::<Vec<_>>()
})
.or(env_style_components)
.unwrap_or(vec![OutputComponent::Full])
.into_iter()
Expand Down
29 changes: 22 additions & 7 deletions src/clap_app.rs
Original file line number Diff line number Diff line change
Expand Up @@ -99,11 +99,28 @@ pub fn build_app(interactive_output: bool) -> ClapApp<'static, 'static> {
Arg::with_name("style")
.long("style")
.value_name("style-components")
.use_delimiter(true)
// Need to turn this off for overrides_with to work as we want. See the bottom most
// example at https://docs.rs/clap/2.32.0/clap/struct.Arg.html#method.overrides_with
.use_delimiter(false)
.takes_value(true)
.possible_values(&[
"auto", "full", "plain", "changes", "header", "grid", "numbers",
])
.overrides_with("style")
.overrides_with("plain")
.overrides_with("number")
// Cannot use clap's built in validation because we have to turn off clap's delimiters
.validator(|val| {
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nice!

let mut invalid_vals = val.split(",").filter(|style| {
!&[
"auto", "full", "plain", "changes", "header", "grid", "numbers",
]
.contains(style)
});

if let Some(invalid) = invalid_vals.next() {
Err(format!("Unknown style, '{}'", invalid))
} else {
Ok(())
}
})
.help(
"Comma-separated list of style elements to display \
(*auto*, full, plain, changes, header, grid, numbers).",
Expand All @@ -121,10 +138,9 @@ pub fn build_app(interactive_output: bool) -> ClapApp<'static, 'static> {
.arg(
Arg::with_name("plain")
.overrides_with("plain")
.overrides_with("number")
.short("p")
.long("plain")
.conflicts_with("style")
.conflicts_with("number")
.help("Show plain style (alias for '--style=plain').")
.long_help(
"Only show plain style, no decorations. This is an alias for \
Expand All @@ -136,7 +152,6 @@ pub fn build_app(interactive_output: bool) -> ClapApp<'static, 'static> {
.long("number")
.overrides_with("number")
.short("n")
.conflicts_with("style")
.help("Show line numbers (alias for '--style=numbers').")
.long_help(
"Only show line numbers, no other decorations. This is an alias for \
Expand Down