Give a specific function type for callback (#168)
Closes #149
diff --git a/CHANGELOG.md b/CHANGELOG.md
index a17da8d..69eee2c 100644
--- a/CHANGELOG.md
+++ b/CHANGELOG.md
@@ -10,6 +10,7 @@
* Instead of `Option.defaultValue`, use `Option.defaultsTo`.
* Instead of `OptionType.FLAG/SINGLE/MULTIPLE`, use
`OptionType.flag/single/multiple`.
+* Add a more specific function type to the `callback` argument of `addOption`.
## 1.6.0
diff --git a/lib/src/allow_anything_parser.dart b/lib/src/allow_anything_parser.dart
index 64040b8..a91e155 100644
--- a/lib/src/allow_anything_parser.dart
+++ b/lib/src/allow_anything_parser.dart
@@ -48,7 +48,7 @@
Iterable<String>? allowed,
Map<String, String>? allowedHelp,
String? defaultsTo,
- Function? callback,
+ void Function(String?)? callback,
bool allowMultiple = false,
bool? splitCommas,
bool hide = false}) {
diff --git a/lib/src/arg_parser.dart b/lib/src/arg_parser.dart
index 6f25efb..72d09da 100644
--- a/lib/src/arg_parser.dart
+++ b/lib/src/arg_parser.dart
@@ -167,9 +167,10 @@
/// user doesn't explicitly pass it in (or `null` by default).
///
/// The [callback] argument is invoked with the option's value when the option
- /// is parsed. Note that this makes argument parsing order-dependent in ways
- /// that are often surprising, and its use is discouraged in favor of reading
- /// values from the [ArgResults].
+ /// is parsed, or with `null` if the option was not parsed.
+ /// Note that this makes argument parsing order-dependent in ways that are
+ /// often surprising, and its use is discouraged in favor of reading values
+ /// from the [ArgResults].
///
/// If [hide] is `true`, this option won't be included in [usage].
///
@@ -184,7 +185,7 @@
Iterable<String>? allowed,
Map<String, String>? allowedHelp,
String? defaultsTo,
- Function? callback,
+ void Function(String?)? callback,
bool hide = false}) {
_addOption(name, abbr, help, valueHelp, allowed, allowedHelp, defaultsTo,
callback, OptionType.single,
diff --git a/test/parse_test.dart b/test/parse_test.dart
index c54e142..6f56db4 100644
--- a/test/parse_test.dart
+++ b/test/parse_test.dart
@@ -145,15 +145,16 @@
});
test('for absent options are invoked with the default value', () {
- var a;
var parser = ArgParser();
- parser.addOption('a', defaultsTo: 'v', callback: (value) => a = value);
+ parser.addOption('a',
+ defaultsTo: 'v',
+ callback: expectAsync1((value) => expect(value, 'v')));
parser.parse([]);
- expect(a, equals('v'));
});
- test('are invoked even if the option is not present', () {
+ test('for absent options are invoked with null if there is no default',
+ () {
var parser = ArgParser();
parser.addOption('a',
callback: expectAsync1((value) => expect(value, isNull)));