Skip to content

Commit

Permalink
Add option hideNegatable to ArgParser.flag()
Browse files Browse the repository at this point in the history
  • Loading branch information
mosuem committed Oct 14, 2024
1 parent 8c5dca8 commit f8f3672
Show file tree
Hide file tree
Showing 7 changed files with 40 additions and 2 deletions.
5 changes: 5 additions & 0 deletions pkgs/args/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -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`.
Expand Down
1 change: 1 addition & 0 deletions pkgs/args/lib/src/allow_anything_parser.dart
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ class AllowAnythingParser implements ArgParser {
bool negatable = true,
void Function(bool)? callback,
bool hide = false,
bool hideNegatedUsage = false,
List<String> aliases = const []}) {
throw UnsupportedError(
"ArgParser.allowAnything().addFlag() isn't supported.");
Expand Down
14 changes: 14 additions & 0 deletions pkgs/args/lib/src/arg_parser.dart
Original file line number Diff line number Diff line change
Expand Up @@ -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.
///
Expand All @@ -133,6 +137,7 @@ class ArgParser {
bool negatable = true,
void Function(bool)? callback,
bool hide = false,
bool hideNegatedUsage = false,
List<String> aliases = const []}) {
_addOption(
name,
Expand All @@ -146,6 +151,7 @@ class ArgParser {
OptionType.flag,
negatable: negatable,
hide: hide,
hideNegatedUsage: hideNegatedUsage,
aliases: aliases);
}

Expand Down Expand Up @@ -285,6 +291,7 @@ class ArgParser {
bool? splitCommas,
bool mandatory = false,
bool hide = false,
bool hideNegatedUsage = false,
List<String> aliases = const []}) {
var allNames = [name, ...aliases];
if (allNames.any((name) => findByNameOrAlias(name) != null)) {
Expand All @@ -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);
Expand Down
8 changes: 8 additions & 0 deletions pkgs/args/lib/src/option.dart
Original file line number Diff line number Diff line change
Expand Up @@ -20,13 +20,15 @@ Option newOption(
bool? splitCommas,
bool mandatory = false,
bool hide = false,
bool hideNegatedUsage = false,
List<String> aliases = const []}) {
return Option._(name, abbr, help, valueHelp, allowed, allowedHelp, defaultsTo,
callback, type,
negatable: negatable,
splitCommas: splitCommas,
mandatory: mandatory,
hide: hide,
hideNegatedUsage: hideNegatedUsage,
aliases: aliases);
}

Expand Down Expand Up @@ -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;

Expand Down Expand Up @@ -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 =
Expand Down
2 changes: 1 addition & 1 deletion pkgs/args/lib/src/usage.dart
Original file line number Diff line number Diff line change
Expand Up @@ -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}';
Expand Down
2 changes: 1 addition & 1 deletion pkgs/args/pubspec.yaml
Original file line number Diff line number Diff line change
@@ -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.
Expand Down
10 changes: 10 additions & 0 deletions pkgs/args/test/usage_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -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');
Expand Down

0 comments on commit f8f3672

Please sign in to comment.