Skip to content

Commit 61e6771

Browse files
authored
Check for mandatory when using option (#871)
1 parent 77d33c8 commit 61e6771

File tree

4 files changed

+10
-5
lines changed

4 files changed

+10
-5
lines changed

Diff for: pkgs/args/CHANGELOG.md

+2-1
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,11 @@
1-
## 2.6.1-wip
1+
## 2.7.0
22

33
* Remove sorting of the `allowedHelp` argument in usage output. Ordering will
44
depend on key order for the passed `Map`.
55
* Fix the repository URL in `pubspec.yaml`.
66
* Added option `hideNegatedUsage` to `ArgParser.flag()` allowing a flag to be
77
`negatable` without showing it in the usage text.
8+
* Fixed #101, adding check for mandatory when using `.option()`.
89

910
## 2.6.0
1011

Diff for: pkgs/args/lib/src/arg_results.dart

+6-3
Original file line numberDiff line numberDiff line change
@@ -81,9 +81,9 @@ class ArgResults {
8181
///
8282
/// [name] must be a valid flag name in the parser.
8383
bool flag(String name) {
84-
var option = _parser.options[name];
84+
final option = _parser.options[name];
8585
if (option == null) {
86-
throw ArgumentError('Could not find an option named "--$name".');
86+
throw ArgumentError('Could not find a flag named "--$name".');
8787
}
8888
if (!option.isFlag) {
8989
throw ArgumentError('"$name" is not a flag.');
@@ -95,13 +95,16 @@ class ArgResults {
9595
///
9696
/// [name] must be a valid option name in the parser.
9797
String? option(String name) {
98-
var option = _parser.options[name];
98+
final option = _parser.options[name];
9999
if (option == null) {
100100
throw ArgumentError('Could not find an option named "--$name".');
101101
}
102102
if (!option.isSingle) {
103103
throw ArgumentError('"$name" is a multi-option.');
104104
}
105+
if (option.mandatory && !_parsed.containsKey(name)) {
106+
throw ArgumentError('Option $name is mandatory.');
107+
}
105108
return option.valueOrDefault(_parsed[name]) as String?;
106109
}
107110

Diff for: pkgs/args/pubspec.yaml

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
name: args
2-
version: 2.6.1-wip
2+
version: 2.7.0
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.

Diff for: pkgs/args/test/parse_test.dart

+1
Original file line numberDiff line numberDiff line change
@@ -621,6 +621,7 @@ void main() {
621621
var results = parser.parse(['-h']);
622622
expect(results['help'], true);
623623
expect(() => results['test'], throwsA(isA<ArgumentError>()));
624+
expect(() => results.option('test'), throwsA(isA<ArgumentError>()));
624625
});
625626
});
626627
});

0 commit comments

Comments
 (0)