Skip to content

Commit ba4b4af

Browse files
authored
Merge pull request #132 from dart-lang/recreateSigurdsPR
Add option `hideNegatable` to `ArgParser.flag()`
2 parents dec28c1 + 25efc0f commit ba4b4af

File tree

6 files changed

+39
-2
lines changed

6 files changed

+39
-2
lines changed

pkgs/args/CHANGELOG.md

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
## 2.6.1-wip
22

3-
* Fix the reporitory URL in `pubspec.yaml`.
3+
* Fix the repository URL in `pubspec.yaml`.
4+
* Added option `hideNegatedUsage` to `ArgParser.flag()` allowing a flag to be
5+
`negatable` without showing it in the usage text.
46

57
## 2.6.0
68

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: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -119,6 +119,11 @@ 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
123+
/// will not be documented in [usage].
124+
/// It is an error for [hideNegatedUsage] to be `true` if [negatable] is
125+
/// `false`.
126+
///
122127
/// If [aliases] is provided, these are used as aliases for [name]. These
123128
/// aliases will not appear as keys in the [options] map.
124129
///
@@ -133,6 +138,7 @@ class ArgParser {
133138
bool negatable = true,
134139
void Function(bool)? callback,
135140
bool hide = false,
141+
bool hideNegatedUsage = false,
136142
List<String> aliases = const []}) {
137143
_addOption(
138144
name,
@@ -146,6 +152,7 @@ class ArgParser {
146152
OptionType.flag,
147153
negatable: negatable,
148154
hide: hide,
155+
hideNegatedUsage: hideNegatedUsage,
149156
aliases: aliases);
150157
}
151158

@@ -285,6 +292,7 @@ class ArgParser {
285292
bool? splitCommas,
286293
bool mandatory = false,
287294
bool hide = false,
295+
bool hideNegatedUsage = false,
288296
List<String> aliases = const []}) {
289297
var allNames = [name, ...aliases];
290298
if (allNames.any((name) => findByNameOrAlias(name) != null)) {
@@ -306,12 +314,20 @@ class ArgParser {
306314
'The option $name cannot be mandatory and have a default value.');
307315
}
308316

317+
if (!negatable && hideNegatedUsage) {
318+
throw ArgumentError(
319+
'The option $name cannot have `hideNegatedUsage` '
320+
'without being negatable.',
321+
);
322+
}
323+
309324
var option = newOption(name, abbr, help, valueHelp, allowed, allowedHelp,
310325
defaultsTo, callback, type,
311326
negatable: negatable,
312327
splitCommas: splitCommas,
313328
mandatory: mandatory,
314329
hide: hide,
330+
hideNegatedUsage: hideNegatedUsage,
315331
aliases: aliases);
316332
_options[name] = option;
317333
_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/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)