Display the default values for options with allowedHelp specified (#100)
Closes #99
diff --git a/CHANGELOG.md b/CHANGELOG.md
index 1299d58..3a3630d 100644
--- a/CHANGELOG.md
+++ b/CHANGELOG.md
@@ -1,3 +1,7 @@
+## 1.4.3
+
+* Display the default values for options with `allowedHelp` specified.
+
## 1.4.2
* Narrow the SDK constraint to only allow SDK versions that support `FutureOr`.
diff --git a/lib/src/usage.dart b/lib/src/usage.dart
index 3ce6d6b..aa3ed07 100644
--- a/lib/src/usage.dart
+++ b/lib/src/usage.dart
@@ -82,7 +82,7 @@
allowedNames.sort();
newline();
for (var name in allowedNames) {
- write(1, getAllowedTitle(name));
+ write(1, getAllowedTitle(option, name));
write(2, option.allowedHelp[name]);
}
newline();
@@ -132,13 +132,16 @@
return result;
}
- String getAllowedTitle(String allowed) {
- return ' [$allowed]';
+ String getAllowedTitle(Option option, String allowed) {
+ var isDefault = option.defaultsTo is List
+ ? option.defaultsTo.contains(allowed)
+ : option.defaultsTo == allowed;
+ return ' [$allowed]' + (isDefault ? ' (default)' : '');
}
void calculateColumnWidths() {
- int abbr = 0;
- int title = 0;
+ var abbr = 0;
+ var title = 0;
for (var option in optionsAndSeparators) {
if (option is! Option) continue;
if (option.hide) continue;
@@ -152,7 +155,7 @@
// Make room for the allowed help.
if (option.allowedHelp != null) {
for (var allowed in option.allowedHelp.keys) {
- title = max(title, getAllowedTitle(allowed).length);
+ title = max(title, getAllowedTitle(option, allowed).length);
}
}
}
diff --git a/pubspec.yaml b/pubspec.yaml
index 91e184e..23dd525 100644
--- a/pubspec.yaml
+++ b/pubspec.yaml
@@ -1,5 +1,5 @@
name: args
-version: 1.4.2
+version: 1.4.3
author: "Dart Team <misc@dartlang.org>"
homepage: https://github.com/dart-lang/args
description: >
diff --git a/test/usage_test.dart b/test/usage_test.dart
index ef0b01a..48a33dd 100644
--- a/test/usage_test.dart
+++ b/test/usage_test.dart
@@ -201,6 +201,30 @@
test('the allowed help is shown', () {
var parser = new ArgParser();
+ parser.addOption('suit', help: 'Like in cards', allowed: [
+ 'spades',
+ 'clubs',
+ 'diamonds',
+ 'hearts'
+ ], allowedHelp: {
+ 'spades': 'Swords of a soldier',
+ 'clubs': 'Weapons of war',
+ 'diamonds': 'Money for this art',
+ 'hearts': 'The shape of my heart'
+ });
+
+ validateUsage(parser, '''
+ --suit Like in cards
+
+ [clubs] Weapons of war
+ [diamonds] Money for this art
+ [hearts] The shape of my heart
+ [spades] Swords of a soldier
+ ''');
+ });
+
+ test('the default is highlighted in the allowed help', () {
+ var parser = new ArgParser();
parser.addOption('suit',
help: 'Like in cards',
defaultsTo: 'clubs',
@@ -218,12 +242,39 @@
});
validateUsage(parser, '''
- --suit Like in cards
+ --suit Like in cards
- [clubs] Weapons of war
- [diamonds] Money for this art
- [hearts] The shape of my heart
- [spades] Swords of a soldier
+ [clubs] (default) Weapons of war
+ [diamonds] Money for this art
+ [hearts] The shape of my heart
+ [spades] Swords of a soldier
+ ''');
+ });
+
+ test('multiple defaults are highlighted in the allowed help', () {
+ var parser = new ArgParser();
+ parser.addMultiOption('suit', help: 'Like in cards', defaultsTo: [
+ 'clubs',
+ 'hearts'
+ ], allowed: [
+ 'spades',
+ 'clubs',
+ 'diamonds',
+ 'hearts'
+ ], allowedHelp: {
+ 'spades': 'Swords of a soldier',
+ 'clubs': 'Weapons of war',
+ 'diamonds': 'Money for this art',
+ 'hearts': 'The shape of my heart'
+ });
+
+ validateUsage(parser, '''
+ --suit Like in cards
+
+ [clubs] (default) Weapons of war
+ [diamonds] Money for this art
+ [hearts] (default) The shape of my heart
+ [spades] Swords of a soldier
''');
});