Skip to content

Commit f8f3672

Browse files
committed
Add option hideNegatable to ArgParser.flag()
1 parent 8c5dca8 commit f8f3672

File tree

7 files changed

+40
-2
lines changed

7 files changed

+40
-2
lines changed

pkgs/args/CHANGELOG.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,8 @@
1+
## 2.6.1-wip
2+
3+
* Added option `hideNegatedUsage` to `ArgParser.flag()` allowing a flag to be
4+
`negatable` without showing it in the usage text.
5+
16
## 2.6.0
27

38
* Added source argument when throwing a `ArgParserException`.

pkgs/args/lib/src/allow_anything_parser.dart

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@ class AllowAnythingParser implements ArgParser {
3636
bool negatable = true,
3737
void Function(bool)? callback,
3838
bool hide = false,
39+
bool hideNegatedUsage = false,
3940
List<String> aliases = const []}) {
4041
throw UnsupportedError(
4142
"ArgParser.allowAnything().addFlag() isn't supported.");

pkgs/args/lib/src/arg_parser.dart

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -119,6 +119,10 @@ class ArgParser {
119119
///
120120
/// If [hide] is `true`, this option won't be included in [usage].
121121
///
122+
/// If [hideNegatedUsage] is `true`, the fact that this flag can be negated will
123+
/// not be documented in [usage].
124+
/// It is an error for [hideNegatedUsage] to be `true` if [negatable] is `false`.
125+
///
122126
/// If [aliases] is provided, these are used as aliases for [name]. These
123127
/// aliases will not appear as keys in the [options] map.
124128
///
@@ -133,6 +137,7 @@ class ArgParser {
133137
bool negatable = true,
134138
void Function(bool)? callback,
135139
bool hide = false,
140+
bool hideNegatedUsage = false,
136141
List<String> aliases = const []}) {
137142
_addOption(
138143
name,
@@ -146,6 +151,7 @@ class ArgParser {
146151
OptionType.flag,
147152
negatable: negatable,
148153
hide: hide,
154+
hideNegatedUsage: hideNegatedUsage,
149155
aliases: aliases);
150156
}
151157

@@ -285,6 +291,7 @@ class ArgParser {
285291
bool? splitCommas,
286292
bool mandatory = false,
287293
bool hide = false,
294+
bool hideNegatedUsage = false,
288295
List<String> aliases = const []}) {
289296
var allNames = [name, ...aliases];
290297
if (allNames.any((name) => findByNameOrAlias(name) != null)) {
@@ -306,12 +313,19 @@ class ArgParser {
306313
'The option $name cannot be mandatory and have a default value.');
307314
}
308315

316+
if (!negatable && hideNegatedUsage) {
317+
throw ArgumentError(
318+
'The option $name cannot have `hideNegatedUsage` without being negatable.',
319+
);
320+
}
321+
309322
var option = newOption(name, abbr, help, valueHelp, allowed, allowedHelp,
310323
defaultsTo, callback, type,
311324
negatable: negatable,
312325
splitCommas: splitCommas,
313326
mandatory: mandatory,
314327
hide: hide,
328+
hideNegatedUsage: hideNegatedUsage,
315329
aliases: aliases);
316330
_options[name] = option;
317331
_optionsAndSeparators.add(option);

pkgs/args/lib/src/option.dart

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,13 +20,15 @@ Option newOption(
2020
bool? splitCommas,
2121
bool mandatory = false,
2222
bool hide = false,
23+
bool hideNegatedUsage = false,
2324
List<String> aliases = const []}) {
2425
return Option._(name, abbr, help, valueHelp, allowed, allowedHelp, defaultsTo,
2526
callback, type,
2627
negatable: negatable,
2728
splitCommas: splitCommas,
2829
mandatory: mandatory,
2930
hide: hide,
31+
hideNegatedUsage: hideNegatedUsage,
3032
aliases: aliases);
3133
}
3234

@@ -66,6 +68,11 @@ class Option {
6668
/// This is `null` unless [type] is [OptionType.flag].
6769
final bool? negatable;
6870

71+
/// Whether to document that this flag is [negatable].
72+
///
73+
/// This is `null` unless [type] is [OptionType.flag].
74+
final bool? hideNegatedUsage;
75+
6976
/// The callback to invoke with the option's value when the option is parsed.
7077
final Function? callback;
7178

@@ -108,6 +115,7 @@ class Option {
108115
bool? splitCommas,
109116
this.mandatory = false,
110117
this.hide = false,
118+
this.hideNegatedUsage,
111119
this.aliases = const []})
112120
: allowed = allowed == null ? null : List.unmodifiable(allowed),
113121
allowedHelp =

pkgs/args/lib/src/usage.dart

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -121,7 +121,7 @@ class _Usage {
121121

122122
String _longOption(Option option) {
123123
String result;
124-
if (option.negatable!) {
124+
if (option.negatable! && !option.hideNegatedUsage!) {
125125
result = '--[no-]${option.name}';
126126
} else {
127127
result = '--${option.name}';

pkgs/args/pubspec.yaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
name: args
2-
version: 2.6.0
2+
version: 2.6.1-wip
33
description: >-
44
Library for defining parsers for parsing raw command-line arguments into a set
55
of options and values using GNU and POSIX style options.

pkgs/args/test/usage_test.dart

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,16 @@ void main() {
1616
''');
1717
});
1818

19+
test('negatable flags with hideNegatedUsage don\'t show "no-" in title',
20+
() {
21+
var parser = ArgParser();
22+
parser.addFlag('mode', help: 'The mode', hideNegatedUsage: true);
23+
24+
validateUsage(parser, '''
25+
--mode The mode
26+
''');
27+
});
28+
1929
test('non-negatable flags don\'t show "no-" in title', () {
2030
var parser = ArgParser();
2131
parser.addFlag('mode', negatable: false, help: 'The mode');

0 commit comments

Comments
 (0)