-
Notifications
You must be signed in to change notification settings - Fork 63
Added arg argument when throwing a ArgParserException
.
#283
Conversation
Signed-off-by: ふぁ <[email protected]>
Signed-off-by: ふぁ <[email protected]>
Thanks for your pull request! It looks like this may be your first contribution to a Google open source project. Before we can look at your pull request, you'll need to sign a Contributor License Agreement (CLA). View this failed invocation of the CLA check for more information. For the most up to date status, view the checks section at the bottom of the pull request. |
Signed-off-by: ふぁ <[email protected]>
The current For instance:
Additionally, error messages might differ from the format of the user’s input, especially when aliases or abbreviated forms are used. For example: For instance:
Do you have any thoughts on this? I’m considering standardizing the error messages to include hyphens. |
Signed-off-by: ふぁ <[email protected]>
Signed-off-by: ふぁ <[email protected]>
Signed-off-by: ふぁ <[email protected]>
This Pull Request improves the clarity of error messages. Additionally, it adds related tests. |
I think that Here’s an example:
My motivation for this is to display user-friendly error messages in software that utilizes this. |
Hi! I just wanted to mention that I will circle back here next week, we had an off-site this week so I haven't had time to review, but I haven't forgotten about this PR either. |
One thing I am struggling with here is what the actual user visible impact of this change is, can you show me an example of how this affects the user experience when you pass an invalid argument etc? We probably should have some integration tests or something, but that is out of scope for this PR. |
I was also wondering about this, but worried about how much work it would be and if it would be breaking. If you are willing to give it a shot, I would be happy to review that. |
This pull request addresses several issues, but the main user-facing change is the improvement in error messages when invalid arguments are passed. Below are some examples: import 'package:args/args.dart';
void main() {
try {
final parser = ArgParser();
final _ = parser.parse(['-f', 'bar']);
} on FormatException catch (e) {
print(e.message); // Could not find an option or flag "-f".
}
try {
final parser = ArgParser();
final _ = parser.parse(['--foo', 'bar']);
} on FormatException catch (e) {
print(e.message); // Could not find an option named "foo".
}
try {
final parser = ArgParser();
parser.addOption('input', abbr: 'i');
final _ = parser.parse(['--input']);
} on FormatException catch (e) {
print(e.message); // Missing argument for "input".
}
try {
final parser = ArgParser();
parser.addOption('input', abbr: 'i');
final _ = parser.parse(['-i']);
} on FormatException catch (e) {
print(e.message); // Missing argument for "input".
}
try {
final parser = ArgParser();
parser.addOption('input', abbr: 'i', aliases: ['add']);
final _ = parser.parse(['--add']);
} on FormatException catch (e) {
print(e.message); // Missing argument for "input".
}
}
With this PR, error messages will now look like this: import 'package:args/args.dart';
void main() {
try {
final parser = ArgParser();
final _ = parser.parse(['-f', 'bar']);
} on FormatException catch (e) {
print(e.message); // Could not find an option or flag "-f".
}
try {
final parser = ArgParser();
final _ = parser.parse(['--foo', 'bar']);
} on FormatException catch (e) {
print(e.message); // Could not find an option named "--foo".
}
try {
final parser = ArgParser();
parser.addOption('input', abbr: 'i');
final _ = parser.parse(['--input']);
} on FormatException catch (e) {
print(e.message); // Missing argument for "--input".
}
try {
final parser = ArgParser();
parser.addOption('input', abbr: 'i');
final _ = parser.parse(['-i']);
} on FormatException catch (e) {
print(e.message); // Missing argument for "-i".
}
try {
final parser = ArgParser();
parser.addOption('input', abbr: 'i', aliases: ['add']);
final _ = parser.parse(['--add']);
} on FormatException catch (e) {
print(e.message); // Missing argument for "--add".
}
} The changes make the messages more intuitive and consistent. Here's a detailed explanation of the key improvements: Standardizing Hyphen: In the current version, the use of hyphen is inconsistent. This PR will ensure that hyphen are uniform throughout the messages. Example before: try {
final parser = ArgParser();
final _ = parser.parse(['-f', 'bar']);
} on FormatException catch (e) {
print(e.message); // Could not find an option or flag "-f".
}
try {
final parser = ArgParser();
final _ = parser.parse(['--foo', 'bar']);
} on FormatException catch (e) {
print(e.message); // Could not find an option named "foo".
} The second point is about unifying the argument names displayed when an error occurs. For errors like The latter approach (displaying what the user entered) is more intuitive for users, so we are adopting it consistently. For example: try {
final parser = ArgParser();
parser.addOption('input', abbr: 'i');
final _ = parser.parse(['-i']);
} on FormatException catch (e) {
print(e.message); // Missing argument for "input".
}
try {
final parser = ArgParser();
parser.addFlag('apple', abbr: 'a');
parser.addOption('banana', abbr: 'b');
parser.addFlag('cherry', abbr: 'c');
final _ = parser.parse(['-abc']);
} on FormatException catch (e) {
print(e.message); // Option "-b" must be a flag to be in a collapsed "-".
} |
Signed-off-by: ふぁ <[email protected]>
Signed-off-by: ふぁ <[email protected]>
ArgParserException
.ArgParserException
.
I gave it a try in #285. |
@natebosch did you want to take a look at this? |
Co-authored-by: Nate Bosch <[email protected]>
Code using the new argument should depend on this version.
Avoiding the acronym to follow Effective Dart, even though other names still use the abbreviation. Adding "Name" so the meaning is unambiguous.
…/args#283) Add an `argumentName` field which tracks the argument that was being parse when the exception is thrown.
Although ArgParserException inherits from FormatException, the source is always null.
This is a very small fix, but please let me know if testing is needed.
Contribution guidelines:
dart format
.Note that many Dart repos have a weekly cadence for reviewing PRs - please allow for some latency before initial review feedback.