Skip to content

fix: ArgResults.operator[] should throw Exception not Error #888

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

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
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
5 changes: 5 additions & 0 deletions pkgs/args/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,8 @@
## 3.0.0

* Breaking: ArgResults.operator[] and ArgResults.option now correctly throw
Exceptions rather than Errors when defined arguments are missing values.

## 2.7.0

* Remove sorting of the `allowedHelp` argument in usage output. Ordering will
Expand Down
5 changes: 3 additions & 2 deletions pkgs/args/lib/src/arg_results.dart
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
import 'dart:collection';

import 'arg_parser.dart';
import 'arg_parser_exception.dart';

/// Creates a new [ArgResults].
///
Expand Down Expand Up @@ -71,7 +72,7 @@ class ArgResults {

final option = _parser.options[name]!;
if (option.mandatory && !_parsed.containsKey(name)) {
throw ArgumentError('Option $name is mandatory.');
throw ArgParserException('Option $name is mandatory.');
}

return option.valueOrDefault(_parsed[name]);
Expand Down Expand Up @@ -103,7 +104,7 @@ class ArgResults {
throw ArgumentError('"$name" is a multi-option.');
}
if (option.mandatory && !_parsed.containsKey(name)) {
throw ArgumentError('Option $name is mandatory.');
throw ArgParserException('Option $name is mandatory.');
}
return option.valueOrDefault(_parsed[name]) as String?;
}
Expand Down
3 changes: 2 additions & 1 deletion pkgs/args/test/command_runner_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
// for details. All rights reserved. Use of this source code is governed by a
// BSD-style license that can be found in the LICENSE file.

import 'package:args/args.dart';
import 'package:args/command_runner.dart';
import 'package:test/test.dart';

Expand Down Expand Up @@ -736,7 +737,7 @@ Run "test help" to see global options.'''));
runner.addCommand(subcommand);
expect(
() => runner.run([subcommand.name]),
throwsA(isA<ArgumentError>().having((e) => e.message, 'message',
throwsA(isA<ArgParserException>().having((e) => e.message, 'message',
contains('Option mandatory-option is mandatory'))));
expect(await runner.run([subcommand.name, '--mandatory-option', 'foo']),
'foo');
Expand Down
15 changes: 11 additions & 4 deletions pkgs/args/test/parse_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -595,7 +595,10 @@ void main() {
var parser = ArgParser();
parser.addOption('username', mandatory: true);
var results = parser.parse([]);
expect(() => results['username'], throwsA(isA<ArgumentError>()));
expect(
() => results['username'],
throwsA(isA<ArgParserException>().having((e) => e.message,
'message', 'Option username is mandatory.')));
});

test('throw if no mandatory args', () {
Expand All @@ -604,7 +607,10 @@ void main() {
parser.addOption('username', mandatory: true);
var results = parser.parse(['--test', 'test']);
expect(results['test'], equals('test'));
expect(() => results['username'], throwsA(isA<ArgumentError>()));
expect(
() => results['username'],
throwsA(isA<ArgParserException>().having((e) => e.message,
'message', 'Option username is mandatory.')));
});

test('parse successfully', () {
Expand All @@ -620,8 +626,9 @@ void main() {
parser.addOption('test', mandatory: true);
var results = parser.parse(['-h']);
expect(results['help'], true);
expect(() => results['test'], throwsA(isA<ArgumentError>()));
expect(() => results.option('test'), throwsA(isA<ArgumentError>()));
expect(() => results['test'], throwsA(isA<ArgParserException>()));
expect(
() => results.option('test'), throwsA(isA<ArgParserException>()));
});
});
});
Expand Down