Skip to content
This repository has been archived by the owner on Oct 17, 2024. It is now read-only.

Commit

Permalink
update: rename source to arg
Browse files Browse the repository at this point in the history
Signed-off-by: ふぁ <yuki@yuki0311.com>
  • Loading branch information
fa0311 committed Sep 25, 2024
1 parent 4be08f6 commit 7550d12
Show file tree
Hide file tree
Showing 4 changed files with 25 additions and 19 deletions.
5 changes: 4 additions & 1 deletion lib/src/arg_parser_exception.dart
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,10 @@ class ArgParserException extends FormatException {
/// This will be empty if the error was on the root parser.
final List<String> commands;

/// The argument that were being parsed when the error was discovered.
final String? arg;

ArgParserException(super.message,
[Iterable<String>? commands, super.source, super.offset])
[Iterable<String>? commands, this.arg, super.source, super.offset])
: commands = commands == null ? const [] : List.unmodifiable(commands);
}
30 changes: 17 additions & 13 deletions lib/src/parser.dart
Original file line number Diff line number Diff line change
Expand Up @@ -71,8 +71,12 @@ class Parser {
try {
commandResults = commandParser.parse();
} on ArgParserException catch (error) {
throw ArgParserException(error.message,
[commandName, ...error.commands], error.source, error.offset);
throw ArgParserException(
error.message,
[commandName, ...error.commands],
error.arg,
error.source,
error.offset);
}

// All remaining arguments were passed to command so clear them here.
Expand Down Expand Up @@ -119,11 +123,11 @@ class Parser {
/// Pulls the value for [option] from the second argument in [_args].
///
/// Validates that there is a valid value there.
void _readNextArgAsValue(Option option, String source) {
void _readNextArgAsValue(Option option, String arg) {
// Take the option argument from the next command line arg.
_validate(_args.isNotEmpty, 'Missing argument for "$source".', source);
_validate(_args.isNotEmpty, 'Missing argument for "$arg".', arg);

_setOption(_results, option, _current, source);
_setOption(_results, option, _current, arg);
_args.removeFirst();
}

Expand Down Expand Up @@ -315,19 +319,19 @@ class Parser {
///
/// Throws an [ArgParserException] if [condition] is `false`.
void _validate(bool condition, String message,
[dynamic source, int? offset]) {
[String? args, List<String>? source, int? offset]) {
if (!condition) {
throw ArgParserException(message, null, source, offset);
throw ArgParserException(message, null, args, source, offset);
}
}

/// Validates and stores [value] as the value for [option], which must not be
/// a flag.
void _setOption(Map results, Option option, String value, String source) {
void _setOption(Map results, Option option, String value, String arg) {
assert(!option.isFlag);

if (!option.isMultiple) {
_validateAllowed(option, value, source);
_validateAllowed(option, value, arg);
results[option.name] = value;
return;
}
Expand All @@ -336,11 +340,11 @@ class Parser {

if (option.splitCommas) {
for (var element in value.split(',')) {
_validateAllowed(option, element, source);
_validateAllowed(option, element, arg);
list.add(element);
}
} else {
_validateAllowed(option, value, source);
_validateAllowed(option, value, arg);
list.add(value);
}
}
Expand All @@ -353,11 +357,11 @@ class Parser {
}

/// Validates that [value] is allowed as a value of [option].
void _validateAllowed(Option option, String value, String source) {
void _validateAllowed(Option option, String value, String arg) {
if (option.allowed == null) return;

_validate(option.allowed!.contains(value),
'"$value" is not an allowed value for option "$source".', source);
'"$value" is not an allowed value for option "$arg".', arg);
}
}

Expand Down
5 changes: 2 additions & 3 deletions test/test_utils.dart
Original file line number Diff line number Diff line change
Expand Up @@ -343,14 +343,13 @@ void throwsIllegalArg(void Function() function, {String? reason}) {
expect(function, throwsArgumentError, reason: reason);
}

void throwsArgParserException(
ArgParser parser, List<String> args, String? source,
void throwsArgParserException(ArgParser parser, List<String> args, String? arg,
{String? reason}) {
try {
parser.parse(args);
fail('Expected an ArgParserException');
} on ArgParserException catch (e) {
expect(e.source, source);
expect(e.arg, arg);
} catch (e) {
fail('Expected an ArgParserException, but got $e');
}
Expand Down
4 changes: 2 additions & 2 deletions test/trailing_options_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,8 @@ void main() {
parser = ArgParser(allowTrailingOptions: true);
});

void expectThrows(List<String> args, String source) {
throwsArgParserException(parser, args, source,
void expectThrows(List<String> args, String arg) {
throwsArgParserException(parser, args, arg,
reason: 'with allowTrailingOptions: true');
}

Expand Down

0 comments on commit 7550d12

Please sign in to comment.