Skip to content

Commit

Permalink
refactor: omit choices/default/env info if description already contai…
Browse files Browse the repository at this point in the history
…ns (#328)
  • Loading branch information
sigoden authored May 17, 2024
1 parent 36d6d9d commit 93973ce
Show file tree
Hide file tree
Showing 8 changed files with 29 additions and 37 deletions.
28 changes: 17 additions & 11 deletions src/param.rs
Original file line number Diff line number Diff line change
Expand Up @@ -855,23 +855,29 @@ impl ParamData {

pub(crate) fn render_describe(&self, describe: &str, id: &str) -> String {
let mut output = describe.to_string();
let multiline = output.contains('\n');
let multiline = describe.contains('\n');
let mut documented = false;
let sep = if multiline { '\n' } else { ' ' };
if let Some(DefaultValue::Value(value)) = &self.default {
if !output.is_empty() {
output.push(sep)
if let Some(ChoiceValue::Values(values)) = &self.choice {
documented = values.iter().all(|v| describe.contains(v));
if !documented {
if !output.is_empty() {
output.push(sep)
}
let values: Vec<String> = values.iter().map(|v| escape_shell_words(v)).collect();
output.push_str(&format!("[possible values: {}]", values.join(", ")));
}
output.push_str(&format!("[default: {}]", escape_shell_words(value)));
}
if let Some(ChoiceValue::Values(values)) = &self.choice {
if !output.is_empty() {
output.push(sep)
if !documented {
if let Some(DefaultValue::Value(value)) = &self.default {
if !output.is_empty() {
output.push(sep)
}
output.push_str(&format!("[default: {}]", escape_shell_words(value)));
}
let values: Vec<String> = values.iter().map(|v| escape_shell_words(v)).collect();
output.push_str(&format!("[possible values: {}]", values.join(", ")));
}
if let Some(env) = self.normalize_bind_env(id) {
if !output.is_empty() {
if !describe.contains(&env) && !output.is_empty() {
output.push(sep)
}
output.push_str(&format!("[env: {env}]"));
Expand Down
4 changes: 2 additions & 2 deletions tests/snapshots/integration__env__env_help.snap
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ ENVIRONMENTS:
TEST_EDA default [default: a]
TEST_EDB default from fn
TEST_ECA choice [possible values: a, b]
TEST_ECB choice + default [default: a] [possible values: a, b]
TEST_ECB choice + default [possible values: a, b] [default: a]
TEST_EFA choice from fn


Expand All @@ -35,5 +35,5 @@ ENVIRONMENTS:
TEST_EDA default [default: a]
TEST_EDB default from fn
TEST_ECA choice [possible values: a, b]
TEST_ECB choice + default [default: a] [possible values: a, b]
TEST_ECB choice + default [possible values: a, b] [default: a]
TEST_EFA choice from fn
4 changes: 2 additions & 2 deletions tests/snapshots/integration__env__env_help_subcmd.snap
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ ENVIRONMENTS:
TEST_EDA default [default: a]
TEST_EDB default from fn
TEST_ECA choice [possible values: a, b]
TEST_ECB choice + default [default: a] [possible values: a, b]
TEST_ECB choice + default [possible values: a, b] [default: a]
TEST_EFA choice from fn
TEST_EA override
TEST_NEW append
Expand All @@ -25,7 +25,7 @@ ENVIRONMENTS:
TEST_EDA default [default: a]
TEST_EDB default from fn
TEST_ECA choice [possible values: a, b]
TEST_ECB choice + default [default: a] [possible values: a, b]
TEST_ECB choice + default [possible values: a, b] [default: a]
TEST_EFA choice from fn
TEST_EA override
TEST_NEW append
4 changes: 0 additions & 4 deletions tests/snapshots/integration__multiline__nowrap.snap
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,6 @@ OPTIONS:
* default: enables recommended style components.
* full: enables all available components.
* auto: same as 'default', unless the output is piped.
[default: default]
[possible values: default, full, auto]

--bar <BAR>
Eager dogs jump quickly over the lazy brown fox, swiftly running past green fields, but only until the night turns dark. Bright stars sparkle clearly above us now.
Expand Down Expand Up @@ -66,8 +64,6 @@ OPTIONS:
* default: enables recommended style components.
* full: enables all available components.
* auto: same as 'default', unless the output is piped.
[default: default]
[possible values: default, full, auto]

--bar <BAR>
Eager dogs jump quickly over the lazy brown fox, swiftly running past green fields, but only until the night turns dark. Bright stars sparkle clearly above us now.
Expand Down
4 changes: 0 additions & 4 deletions tests/snapshots/integration__multiline__wrap.snap
Original file line number Diff line number Diff line change
Expand Up @@ -33,8 +33,6 @@ OPTIONS:
* default: enables recommended style components.
* full: enables all available components.
* auto: same as 'default', unless the output is piped.
[default: default]
[possible values: default, full, auto]

--bar <BAR>
Eager dogs jump quickly over the lazy brown fox, swiftly running
Expand Down Expand Up @@ -76,8 +74,6 @@ OPTIONS:
* default: enables recommended style components.
* full: enables all available components.
* auto: same as 'default', unless the output is piped.
[default: default]
[possible values: default, full, auto]

--bar <BAR>
Eager dogs jump quickly over the lazy brown fox, swiftly running past green fields, but only until the night turns dark. Bright stars sparkle clearly above us now.
Expand Down
4 changes: 0 additions & 4 deletions tests/snapshots/integration__multiline__wrap2.snap
Original file line number Diff line number Diff line change
Expand Up @@ -33,8 +33,6 @@ OPTIONS:
* default: enables recommended style components.
* full: enables all available components.
* auto: same as 'default', unless the output is piped.
[default: default]
[possible values: default, full, auto]

--bar <BAR>
Eager dogs jump quickly over the lazy brown fox, swiftly running
Expand Down Expand Up @@ -76,8 +74,6 @@ OPTIONS:
* default: enables recommended style components.
* full: enables all available components.
* auto: same as 'default', unless the output is piped.
[default: default]
[possible values: default, full, auto]

--bar <BAR>
Eager dogs jump quickly over the lazy brown fox, swiftly running past green fields, but only until the night turns dark. Bright stars sparkle clearly above us now.
Expand Down
6 changes: 2 additions & 4 deletions tests/snapshots/integration__spec__arg_subcmd_help.snap
Original file line number Diff line number Diff line change
Expand Up @@ -169,7 +169,7 @@ command cat >&2 <<-'EOF'
USAGE: prog cmd_arg_with_choices_and_default [VAL]

ARGS:
[VAL] [default: x] [possible values: x, y, z]
[VAL] [possible values: x, y, z] [default: x]

EOF
exit 0
Expand All @@ -178,7 +178,7 @@ exit 0
USAGE: prog cmd_arg_with_choices_and_default [VAL]

ARGS:
[VAL] [default: x] [possible values: x, y, z]
[VAL] [possible values: x, y, z] [default: x]

************ RUN ************
prog cmd_multi_arg_with_choices -h
Expand Down Expand Up @@ -432,5 +432,3 @@ ARGS:
<VAL1>
<VAL2>
<VAL3>


12 changes: 6 additions & 6 deletions tests/snapshots/integration__spec__option_help.snap
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ OPTIONS:
--oda <ODA> default [default: a]
--odb <ODB> default from fn
--oca <OCA> choice [possible values: a, b]
--ocb <OCB> choice + default [default: a] [possible values: a, b]
--ocb <OCB> choice + default [possible values: a, b] [default: a]
--occ [OCC]... multi-occurs + choice [possible values: a, b]
--ofa <OFA> choice from fn
--ofb <OFB> choice from fn + no validation
Expand Down Expand Up @@ -56,7 +56,7 @@ OPTIONS:
--oda <ODA> default [default: a]
--odb <ODB> default from fn
--oca <OCA> choice [possible values: a, b]
--ocb <OCB> choice + default [default: a] [possible values: a, b]
--ocb <OCB> choice + default [possible values: a, b] [default: a]
--occ [OCC]... multi-occurs + choice [possible values: a, b]
--ofa <OFA> choice from fn
--ofb <OFB> choice from fn + no validation
Expand Down Expand Up @@ -188,7 +188,7 @@ OPTIONS:
+oda <ODA> default [default: a]
+odb <ODB> default from fn
+oca <OCA> choice [possible values: a, b]
+ocb <OCB> choice + default [default: a] [possible values: a, b]
+ocb <OCB> choice + default [possible values: a, b] [default: a]
+occ [OCC]... multi-occurs + choice [possible values: a, b]
+ocd <OCD>... required + multi-occurs + choice [possible values: a, b]
+ofa <OFA> choice from fn
Expand Down Expand Up @@ -219,7 +219,7 @@ OPTIONS:
+oda <ODA> default [default: a]
+odb <ODB> default from fn
+oca <OCA> choice [possible values: a, b]
+ocb <OCB> choice + default [default: a] [possible values: a, b]
+ocb <OCB> choice + default [possible values: a, b] [default: a]
+occ [OCC]... multi-occurs + choice [possible values: a, b]
+ocd <OCD>... required + multi-occurs + choice [possible values: a, b]
+ofa <OFA> choice from fn
Expand Down Expand Up @@ -435,7 +435,7 @@ USAGE: prog test3 [OPTIONS]
OPTIONS:
--oe <OE> [default: val]
--of <OF>
--cb <CB> [default: x] [possible values: x, y, z]
--cb <CB> [possible values: x, y, z] [default: x]
-h, --help

EOF
Expand All @@ -447,5 +447,5 @@ USAGE: prog test3 [OPTIONS]
OPTIONS:
--oe <OE> [default: val]
--of <OF>
--cb <CB> [default: x] [possible values: x, y, z]
--cb <CB> [possible values: x, y, z] [default: x]
-h, --help

0 comments on commit 93973ce

Please sign in to comment.