Skip to content

Check for mandatory when using option #871

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Mar 12, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion pkgs/args/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
## 2.6.1-wip
## 2.7.0

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

## 2.6.0

Expand Down
9 changes: 6 additions & 3 deletions pkgs/args/lib/src/arg_results.dart
Original file line number Diff line number Diff line change
Expand Up @@ -81,9 +81,9 @@ class ArgResults {
///
/// [name] must be a valid flag name in the parser.
bool flag(String name) {
var option = _parser.options[name];
final option = _parser.options[name];
if (option == null) {
throw ArgumentError('Could not find an option named "--$name".');
throw ArgumentError('Could not find a flag named "--$name".');
}
if (!option.isFlag) {
throw ArgumentError('"$name" is not a flag.');
Expand All @@ -95,13 +95,16 @@ class ArgResults {
///
/// [name] must be a valid option name in the parser.
String? option(String name) {
var option = _parser.options[name];
final option = _parser.options[name];
if (option == null) {
throw ArgumentError('Could not find an option named "--$name".');
}
if (!option.isSingle) {
throw ArgumentError('"$name" is a multi-option.');
}
if (option.mandatory && !_parsed.containsKey(name)) {
throw ArgumentError('Option $name is mandatory.');
}
return option.valueOrDefault(_parsed[name]) as String?;
}

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.1-wip
version: 2.7.0
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
1 change: 1 addition & 0 deletions pkgs/args/test/parse_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -621,6 +621,7 @@ void main() {
var results = parser.parse(['-h']);
expect(results['help'], true);
expect(() => results['test'], throwsA(isA<ArgumentError>()));
expect(() => results.option('test'), throwsA(isA<ArgumentError>()));
});
});
});
Expand Down