From 307bda3d86ed86f7ac09a84ca980de6adc2270f5 Mon Sep 17 00:00:00 2001 From: Sigurd Meldgaard Date: Mon, 7 Oct 2024 12:24:14 +0000 Subject: [PATCH 1/3] Add option `hideNegatable` to `ArgParser.flag()` --- lib/src/allow_anything_parser.dart | 1 + lib/src/arg_parser.dart | 14 ++++++++++++++ lib/src/option.dart | 8 ++++++++ lib/src/usage.dart | 2 +- test/usage_test.dart | 9 +++++++++ 5 files changed, 33 insertions(+), 1 deletion(-) diff --git a/lib/src/allow_anything_parser.dart b/lib/src/allow_anything_parser.dart index 46dfc94..39f8151 100644 --- a/lib/src/allow_anything_parser.dart +++ b/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 hideNegatable = false, List aliases = const []}) { throw UnsupportedError( "ArgParser.allowAnything().addFlag() isn't supported."); diff --git a/lib/src/arg_parser.dart b/lib/src/arg_parser.dart index c47c003..a3cb989 100644 --- a/lib/src/arg_parser.dart +++ b/lib/src/arg_parser.dart @@ -119,6 +119,10 @@ class ArgParser { /// /// If [hide] is `true`, this option won't be included in [usage]. /// + /// If [hideNegatable] is `true`, the fact that this flag can be negated will + /// not be documented in [usage]. + /// It is an error for [hideNegatable] 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 hideNegatable = false, List aliases = const []}) { _addOption( name, @@ -146,6 +151,7 @@ class ArgParser { OptionType.flag, negatable: negatable, hide: hide, + hideNegatable: hideNegatable, aliases: aliases); } @@ -285,6 +291,7 @@ class ArgParser { bool? splitCommas, bool mandatory = false, bool hide = false, + bool hideNegatable = 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 && hideNegatable) { + throw ArgumentError( + 'The option $name cannot have `hideNegatable` without being negatable.', + ); + } + var option = newOption(name, abbr, help, valueHelp, allowed, allowedHelp, defaultsTo, callback, type, negatable: negatable, splitCommas: splitCommas, mandatory: mandatory, hide: hide, + hideNegatable: hideNegatable, aliases: aliases); _options[name] = option; _optionsAndSeparators.add(option); diff --git a/lib/src/option.dart b/lib/src/option.dart index 50a8628..58717ee 100644 --- a/lib/src/option.dart +++ b/lib/src/option.dart @@ -20,6 +20,7 @@ Option newOption( bool? splitCommas, bool mandatory = false, bool hide = false, + bool hideNegatable = 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, + hideNegatable: hideNegatable, 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? hideNegatable; + /// 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.hideNegatable, this.aliases = const []}) : allowed = allowed == null ? null : List.unmodifiable(allowed), allowedHelp = diff --git a/lib/src/usage.dart b/lib/src/usage.dart index 1ef9627..c19d784 100644 --- a/lib/src/usage.dart +++ b/lib/src/usage.dart @@ -121,7 +121,7 @@ class _Usage { String _longOption(Option option) { String result; - if (option.negatable!) { + if (option.negatable! && !option.hideNegatable!) { result = '--[no-]${option.name}'; } else { result = '--${option.name}'; diff --git a/test/usage_test.dart b/test/usage_test.dart index 1167186..3edc5dc 100644 --- a/test/usage_test.dart +++ b/test/usage_test.dart @@ -16,6 +16,15 @@ void main() { '''); }); + test('negatable flags with hideNegatable don\'t show "no-" in title', () { + var parser = ArgParser(); + parser.addFlag('mode', help: 'The mode', hideNegatable: 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 9e8664f8c3f356e7cf86bbc9a614dd9bbee8dd55 Mon Sep 17 00:00:00 2001 From: Sigurd Meldgaard Date: Fri, 11 Oct 2024 08:39:30 +0000 Subject: [PATCH 2/3] Rename option to `hideNegatedUsage` --- lib/src/allow_anything_parser.dart | 2 +- lib/src/arg_parser.dart | 16 ++++++++-------- lib/src/option.dart | 8 ++++---- lib/src/usage.dart | 2 +- test/usage_test.dart | 5 +++-- 5 files changed, 17 insertions(+), 16 deletions(-) diff --git a/lib/src/allow_anything_parser.dart b/lib/src/allow_anything_parser.dart index 39f8151..69472b3 100644 --- a/lib/src/allow_anything_parser.dart +++ b/lib/src/allow_anything_parser.dart @@ -36,7 +36,7 @@ class AllowAnythingParser implements ArgParser { bool negatable = true, void Function(bool)? callback, bool hide = false, - bool hideNegatable = false, + bool hideNegatedUsage = false, List aliases = const []}) { throw UnsupportedError( "ArgParser.allowAnything().addFlag() isn't supported."); diff --git a/lib/src/arg_parser.dart b/lib/src/arg_parser.dart index a3cb989..7bb21c7 100644 --- a/lib/src/arg_parser.dart +++ b/lib/src/arg_parser.dart @@ -119,9 +119,9 @@ class ArgParser { /// /// If [hide] is `true`, this option won't be included in [usage]. /// - /// If [hideNegatable] is `true`, the fact that this flag can be negated will + /// If [hideNegatedUsage] is `true`, the fact that this flag can be negated will /// not be documented in [usage]. - /// It is an error for [hideNegatable] to be `true` if [negatable] is `false`. + /// 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. @@ -137,7 +137,7 @@ class ArgParser { bool negatable = true, void Function(bool)? callback, bool hide = false, - bool hideNegatable = false, + bool hideNegatedUsage = false, List aliases = const []}) { _addOption( name, @@ -151,7 +151,7 @@ class ArgParser { OptionType.flag, negatable: negatable, hide: hide, - hideNegatable: hideNegatable, + hideNegatedUsage: hideNegatedUsage, aliases: aliases); } @@ -291,7 +291,7 @@ class ArgParser { bool? splitCommas, bool mandatory = false, bool hide = false, - bool hideNegatable = false, + bool hideNegatedUsage = false, List aliases = const []}) { var allNames = [name, ...aliases]; if (allNames.any((name) => findByNameOrAlias(name) != null)) { @@ -313,9 +313,9 @@ class ArgParser { 'The option $name cannot be mandatory and have a default value.'); } - if (!negatable && hideNegatable) { + if (!negatable && hideNegatedUsage) { throw ArgumentError( - 'The option $name cannot have `hideNegatable` without being negatable.', + 'The option $name cannot have `hideNegatedUsage` without being negatable.', ); } @@ -325,7 +325,7 @@ class ArgParser { splitCommas: splitCommas, mandatory: mandatory, hide: hide, - hideNegatable: hideNegatable, + hideNegatedUsage: hideNegatedUsage, aliases: aliases); _options[name] = option; _optionsAndSeparators.add(option); diff --git a/lib/src/option.dart b/lib/src/option.dart index 58717ee..463e5e2 100644 --- a/lib/src/option.dart +++ b/lib/src/option.dart @@ -20,7 +20,7 @@ Option newOption( bool? splitCommas, bool mandatory = false, bool hide = false, - bool hideNegatable = false, + bool hideNegatedUsage = false, List aliases = const []}) { return Option._(name, abbr, help, valueHelp, allowed, allowedHelp, defaultsTo, callback, type, @@ -28,7 +28,7 @@ Option newOption( splitCommas: splitCommas, mandatory: mandatory, hide: hide, - hideNegatable: hideNegatable, + hideNegatedUsage: hideNegatedUsage, aliases: aliases); } @@ -71,7 +71,7 @@ class Option { /// Whether to document that this flag is [negatable]. /// /// This is `null` unless [type] is [OptionType.flag]. - final bool? hideNegatable; + final bool? hideNegatedUsage; /// The callback to invoke with the option's value when the option is parsed. final Function? callback; @@ -115,7 +115,7 @@ class Option { bool? splitCommas, this.mandatory = false, this.hide = false, - this.hideNegatable, + this.hideNegatedUsage, this.aliases = const []}) : allowed = allowed == null ? null : List.unmodifiable(allowed), allowedHelp = diff --git a/lib/src/usage.dart b/lib/src/usage.dart index c19d784..bd39f11 100644 --- a/lib/src/usage.dart +++ b/lib/src/usage.dart @@ -121,7 +121,7 @@ class _Usage { String _longOption(Option option) { String result; - if (option.negatable! && !option.hideNegatable!) { + if (option.negatable! && !option.hideNegatedUsage!) { result = '--[no-]${option.name}'; } else { result = '--${option.name}'; diff --git a/test/usage_test.dart b/test/usage_test.dart index 3edc5dc..6f5315b 100644 --- a/test/usage_test.dart +++ b/test/usage_test.dart @@ -16,9 +16,10 @@ void main() { '''); }); - test('negatable flags with hideNegatable don\'t show "no-" in title', () { + test('negatable flags with hideNegatedUsage don\'t show "no-" in title', + () { var parser = ArgParser(); - parser.addFlag('mode', help: 'The mode', hideNegatable: true); + parser.addFlag('mode', help: 'The mode', hideNegatedUsage: true); validateUsage(parser, ''' --mode The mode From 31b460bd76e8a71d670684d1603269bef06c16ad Mon Sep 17 00:00:00 2001 From: Sigurd Meldgaard Date: Fri, 11 Oct 2024 08:48:24 +0000 Subject: [PATCH 3/3] Changelog+pubspec update --- CHANGELOG.md | 4 +++- pubspec.yaml | 2 +- 2 files changed, 4 insertions(+), 2 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index a205299..0bd9cfc 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,8 +1,10 @@ -## 2.6.0-wip +## 2.6.0 * Added source argument when throwing a `ArgParserException`. * Fix inconsistent `FormatException` messages * Require Dart 3.3 +* Added option `hideNegatedUsage` to `ArgParser.flag()` allowing a flag to be + `negatable` without showing it in the usage text. ## 2.5.0 diff --git a/pubspec.yaml b/pubspec.yaml index 8e59181..8222f41 100644 --- a/pubspec.yaml +++ b/pubspec.yaml @@ -1,5 +1,5 @@ name: args -version: 2.6.0-wip +version: 2.6.0 description: >- Library for defining parsers for parsing raw command-line arguments into a set of options and values using GNU and POSIX style options.