From 93973ce0859d35019228a3dcb0ca3fba7d84c1e8 Mon Sep 17 00:00:00 2001 From: sigoden Date: Fri, 17 May 2024 09:30:04 +0800 Subject: [PATCH] refactor: omit choices/default/env info if description already contains (#328) --- src/param.rs | 28 +++++++++++-------- .../snapshots/integration__env__env_help.snap | 4 +-- .../integration__env__env_help_subcmd.snap | 4 +-- .../integration__multiline__nowrap.snap | 4 --- .../integration__multiline__wrap.snap | 4 --- .../integration__multiline__wrap2.snap | 4 --- .../integration__spec__arg_subcmd_help.snap | 6 ++-- .../integration__spec__option_help.snap | 12 ++++---- 8 files changed, 29 insertions(+), 37 deletions(-) diff --git a/src/param.rs b/src/param.rs index 6d4eb32c..dd95e989 100644 --- a/src/param.rs +++ b/src/param.rs @@ -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 = 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 = 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}]")); diff --git a/tests/snapshots/integration__env__env_help.snap b/tests/snapshots/integration__env__env_help.snap index e70e0a98..8e60b6f4 100644 --- a/tests/snapshots/integration__env__env_help.snap +++ b/tests/snapshots/integration__env__env_help.snap @@ -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 @@ -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 diff --git a/tests/snapshots/integration__env__env_help_subcmd.snap b/tests/snapshots/integration__env__env_help_subcmd.snap index 5e359f65..78270db0 100644 --- a/tests/snapshots/integration__env__env_help_subcmd.snap +++ b/tests/snapshots/integration__env__env_help_subcmd.snap @@ -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 @@ -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 diff --git a/tests/snapshots/integration__multiline__nowrap.snap b/tests/snapshots/integration__multiline__nowrap.snap index 17d300c3..ea5507f8 100644 --- a/tests/snapshots/integration__multiline__nowrap.snap +++ b/tests/snapshots/integration__multiline__nowrap.snap @@ -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 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. @@ -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 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. diff --git a/tests/snapshots/integration__multiline__wrap.snap b/tests/snapshots/integration__multiline__wrap.snap index 1a04a4e5..787a787d 100644 --- a/tests/snapshots/integration__multiline__wrap.snap +++ b/tests/snapshots/integration__multiline__wrap.snap @@ -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 Eager dogs jump quickly over the lazy brown fox, swiftly running @@ -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 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. diff --git a/tests/snapshots/integration__multiline__wrap2.snap b/tests/snapshots/integration__multiline__wrap2.snap index 9aaf27f2..7bb8650d 100644 --- a/tests/snapshots/integration__multiline__wrap2.snap +++ b/tests/snapshots/integration__multiline__wrap2.snap @@ -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 Eager dogs jump quickly over the lazy brown fox, swiftly running @@ -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 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. diff --git a/tests/snapshots/integration__spec__arg_subcmd_help.snap b/tests/snapshots/integration__spec__arg_subcmd_help.snap index d6630675..fd91dbf4 100644 --- a/tests/snapshots/integration__spec__arg_subcmd_help.snap +++ b/tests/snapshots/integration__spec__arg_subcmd_help.snap @@ -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 @@ -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 @@ -432,5 +432,3 @@ ARGS: - - diff --git a/tests/snapshots/integration__spec__option_help.snap b/tests/snapshots/integration__spec__option_help.snap index f4d0a8ea..4278857a 100644 --- a/tests/snapshots/integration__spec__option_help.snap +++ b/tests/snapshots/integration__spec__option_help.snap @@ -25,7 +25,7 @@ OPTIONS: --oda default [default: a] --odb default from fn --oca choice [possible values: a, b] - --ocb choice + default [default: a] [possible values: a, b] + --ocb choice + default [possible values: a, b] [default: a] --occ [OCC]... multi-occurs + choice [possible values: a, b] --ofa choice from fn --ofb choice from fn + no validation @@ -56,7 +56,7 @@ OPTIONS: --oda default [default: a] --odb default from fn --oca choice [possible values: a, b] - --ocb choice + default [default: a] [possible values: a, b] + --ocb choice + default [possible values: a, b] [default: a] --occ [OCC]... multi-occurs + choice [possible values: a, b] --ofa choice from fn --ofb choice from fn + no validation @@ -188,7 +188,7 @@ OPTIONS: +oda default [default: a] +odb default from fn +oca choice [possible values: a, b] - +ocb choice + default [default: a] [possible values: a, b] + +ocb choice + default [possible values: a, b] [default: a] +occ [OCC]... multi-occurs + choice [possible values: a, b] +ocd ... required + multi-occurs + choice [possible values: a, b] +ofa choice from fn @@ -219,7 +219,7 @@ OPTIONS: +oda default [default: a] +odb default from fn +oca choice [possible values: a, b] - +ocb choice + default [default: a] [possible values: a, b] + +ocb choice + default [possible values: a, b] [default: a] +occ [OCC]... multi-occurs + choice [possible values: a, b] +ocd ... required + multi-occurs + choice [possible values: a, b] +ofa choice from fn @@ -435,7 +435,7 @@ USAGE: prog test3 [OPTIONS] OPTIONS: --oe [default: val] --of - --cb [default: x] [possible values: x, y, z] + --cb [possible values: x, y, z] [default: x] -h, --help EOF @@ -447,5 +447,5 @@ USAGE: prog test3 [OPTIONS] OPTIONS: --oe [default: val] --of - --cb [default: x] [possible values: x, y, z] + --cb [possible values: x, y, z] [default: x] -h, --help