From f8f367222d9a7f35a0c8260d77acfbf122d26836 Mon Sep 17 00:00:00 2001 From: Moritz Date: Mon, 14 Oct 2024 17:09:48 +0200 Subject: [PATCH 1/3] Add option hideNegatable to ArgParser.flag() --- pkgs/args/CHANGELOG.md | 5 +++++ pkgs/args/lib/src/allow_anything_parser.dart | 1 + pkgs/args/lib/src/arg_parser.dart | 14 ++++++++++++++ pkgs/args/lib/src/option.dart | 8 ++++++++ pkgs/args/lib/src/usage.dart | 2 +- pkgs/args/pubspec.yaml | 2 +- pkgs/args/test/usage_test.dart | 10 ++++++++++ 7 files changed, 40 insertions(+), 2 deletions(-) diff --git a/pkgs/args/CHANGELOG.md b/pkgs/args/CHANGELOG.md index 15b392b1..14bc9dd8 100644 --- a/pkgs/args/CHANGELOG.md +++ b/pkgs/args/CHANGELOG.md @@ -1,3 +1,8 @@ +## 2.6.1-wip + +* Added option `hideNegatedUsage` to `ArgParser.flag()` allowing a flag to be + `negatable` without showing it in the usage text. + ## 2.6.0 * Added source argument when throwing a `ArgParserException`. diff --git a/pkgs/args/lib/src/allow_anything_parser.dart b/pkgs/args/lib/src/allow_anything_parser.dart index 46dfc940..69472b37 100644 --- a/pkgs/args/lib/src/allow_anything_parser.dart +++ b/pkgs/args/lib/src/allow_anything_parser.dart @@ -36,6 +36,7 @@ class AllowAnythingParser implements ArgParser { bool negatable = true, void Function(bool)? callback, bool hide = false, + bool hideNegatedUsage = false, List aliases = const []}) { throw UnsupportedError( "ArgParser.allowAnything().addFlag() isn't supported."); diff --git a/pkgs/args/lib/src/arg_parser.dart b/pkgs/args/lib/src/arg_parser.dart index c47c0030..7bb21c74 100644 --- a/pkgs/args/lib/src/arg_parser.dart +++ b/pkgs/args/lib/src/arg_parser.dart @@ -119,6 +119,10 @@ class ArgParser { /// /// If [hide] is `true`, this option won't be included in [usage]. /// + /// If [hideNegatedUsage] is `true`, the fact that this flag can be negated will + /// not be documented in [usage]. + /// It is an error for [hideNegatedUsage] to be `true` if [negatable] is `false`. + /// /// If [aliases] is provided, these are used as aliases for [name]. These /// aliases will not appear as keys in the [options] map. /// @@ -133,6 +137,7 @@ class ArgParser { bool negatable = true, void Function(bool)? callback, bool hide = false, + bool hideNegatedUsage = false, List aliases = const []}) { _addOption( name, @@ -146,6 +151,7 @@ class ArgParser { OptionType.flag, negatable: negatable, hide: hide, + hideNegatedUsage: hideNegatedUsage, aliases: aliases); } @@ -285,6 +291,7 @@ class ArgParser { bool? splitCommas, bool mandatory = false, bool hide = false, + bool hideNegatedUsage = false, List aliases = const []}) { var allNames = [name, ...aliases]; if (allNames.any((name) => findByNameOrAlias(name) != null)) { @@ -306,12 +313,19 @@ class ArgParser { 'The option $name cannot be mandatory and have a default value.'); } + if (!negatable && hideNegatedUsage) { + throw ArgumentError( + 'The option $name cannot have `hideNegatedUsage` without being negatable.', + ); + } + var option = newOption(name, abbr, help, valueHelp, allowed, allowedHelp, defaultsTo, callback, type, negatable: negatable, splitCommas: splitCommas, mandatory: mandatory, hide: hide, + hideNegatedUsage: hideNegatedUsage, aliases: aliases); _options[name] = option; _optionsAndSeparators.add(option); diff --git a/pkgs/args/lib/src/option.dart b/pkgs/args/lib/src/option.dart index 50a8628a..463e5e2d 100644 --- a/pkgs/args/lib/src/option.dart +++ b/pkgs/args/lib/src/option.dart @@ -20,6 +20,7 @@ Option newOption( bool? splitCommas, bool mandatory = false, bool hide = false, + bool hideNegatedUsage = false, List aliases = const []}) { return Option._(name, abbr, help, valueHelp, allowed, allowedHelp, defaultsTo, callback, type, @@ -27,6 +28,7 @@ Option newOption( splitCommas: splitCommas, mandatory: mandatory, hide: hide, + hideNegatedUsage: hideNegatedUsage, aliases: aliases); } @@ -66,6 +68,11 @@ class Option { /// This is `null` unless [type] is [OptionType.flag]. final bool? negatable; + /// Whether to document that this flag is [negatable]. + /// + /// This is `null` unless [type] is [OptionType.flag]. + final bool? hideNegatedUsage; + /// The callback to invoke with the option's value when the option is parsed. final Function? callback; @@ -108,6 +115,7 @@ class Option { bool? splitCommas, this.mandatory = false, this.hide = false, + this.hideNegatedUsage, this.aliases = const []}) : allowed = allowed == null ? null : List.unmodifiable(allowed), allowedHelp = diff --git a/pkgs/args/lib/src/usage.dart b/pkgs/args/lib/src/usage.dart index 1ef96273..bd39f112 100644 --- a/pkgs/args/lib/src/usage.dart +++ b/pkgs/args/lib/src/usage.dart @@ -121,7 +121,7 @@ class _Usage { String _longOption(Option option) { String result; - if (option.negatable!) { + if (option.negatable! && !option.hideNegatedUsage!) { result = '--[no-]${option.name}'; } else { result = '--${option.name}'; diff --git a/pkgs/args/pubspec.yaml b/pkgs/args/pubspec.yaml index 859e1860..a1ae3c93 100644 --- a/pkgs/args/pubspec.yaml +++ b/pkgs/args/pubspec.yaml @@ -1,5 +1,5 @@ name: args -version: 2.6.0 +version: 2.6.1-wip description: >- Library for defining parsers for parsing raw command-line arguments into a set of options and values using GNU and POSIX style options. diff --git a/pkgs/args/test/usage_test.dart b/pkgs/args/test/usage_test.dart index 11671860..6f5315b5 100644 --- a/pkgs/args/test/usage_test.dart +++ b/pkgs/args/test/usage_test.dart @@ -16,6 +16,16 @@ void main() { '''); }); + test('negatable flags with hideNegatedUsage don\'t show "no-" in title', + () { + var parser = ArgParser(); + parser.addFlag('mode', help: 'The mode', hideNegatedUsage: true); + + validateUsage(parser, ''' + --mode The mode + '''); + }); + test('non-negatable flags don\'t show "no-" in title', () { var parser = ArgParser(); parser.addFlag('mode', negatable: false, help: 'The mode'); From db3c8fe4025435ab2dd7d6776e6b749de57fa342 Mon Sep 17 00:00:00 2001 From: Moritz Date: Mon, 14 Oct 2024 17:12:11 +0200 Subject: [PATCH 2/3] Fix lints --- pkgs/args/lib/src/arg_parser.dart | 10 ++++++---- 1 file changed, 6 insertions(+), 4 deletions(-) diff --git a/pkgs/args/lib/src/arg_parser.dart b/pkgs/args/lib/src/arg_parser.dart index 7bb21c74..50c3991d 100644 --- a/pkgs/args/lib/src/arg_parser.dart +++ b/pkgs/args/lib/src/arg_parser.dart @@ -119,9 +119,10 @@ class ArgParser { /// /// If [hide] is `true`, this option won't be included in [usage]. /// - /// If [hideNegatedUsage] is `true`, the fact that this flag can be negated will - /// not be documented in [usage]. - /// It is an error for [hideNegatedUsage] to be `true` if [negatable] is `false`. + /// If [hideNegatedUsage] is `true`, the fact that this flag can be negated + /// will not be documented in [usage]. + /// It is an error for [hideNegatedUsage] to be `true` if [negatable] is + /// `false`. /// /// If [aliases] is provided, these are used as aliases for [name]. These /// aliases will not appear as keys in the [options] map. @@ -315,7 +316,8 @@ class ArgParser { if (!negatable && hideNegatedUsage) { throw ArgumentError( - 'The option $name cannot have `hideNegatedUsage` without being negatable.', + 'The option $name cannot have `hideNegatedUsage` ' + 'without being negatable.', ); } From 25efc0f67b376c4288dd71bf41ec4e8fd9dc681d Mon Sep 17 00:00:00 2001 From: Sigurd Meldgaard Date: Mon, 21 Oct 2024 12:18:11 +0200 Subject: [PATCH 3/3] Update pkgs/args/CHANGELOG.md --- pkgs/args/CHANGELOG.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/pkgs/args/CHANGELOG.md b/pkgs/args/CHANGELOG.md index 7484eee2..b97daf57 100644 --- a/pkgs/args/CHANGELOG.md +++ b/pkgs/args/CHANGELOG.md @@ -1,6 +1,6 @@ ## 2.6.1-wip -* Fix the reporitory URL in `pubspec.yaml`. +* Fix the repository URL in `pubspec.yaml`. * Added option `hideNegatedUsage` to `ArgParser.flag()` allowing a flag to be `negatable` without showing it in the usage text.