Gracefully handle argument parse errors

This commit is contained in:
Natalie Weizenbaum 2017-07-13 01:25:14 -07:00
parent 9f85a4dc34
commit 7c133c73a3

View File

@ -27,7 +27,15 @@ main(List<String> args) async {
abbr: 'h', help: 'Print this usage information.', negatable: false) abbr: 'h', help: 'Print this usage information.', negatable: false)
..addFlag('version', ..addFlag('version',
help: 'Print the version of Dart Sass.', negatable: false); help: 'Print the version of Dart Sass.', negatable: false);
var options = argParser.parse(args);
ArgResults options;
try {
options = argParser.parse(args);
} on FormatException catch (error) {
_printUsage(argParser, error.message);
exitCode = 64;
return;
}
if (options['version'] as bool) { if (options['version'] as bool) {
_loadVersion().then((version) { _loadVersion().then((version) {
@ -38,9 +46,7 @@ main(List<String> args) async {
} }
if (options['help'] as bool || options.rest.isEmpty) { if (options['help'] as bool || options.rest.isEmpty) {
print("Compile Sass to CSS.\n"); _printUsage(argParser, "Compile Sass to CSS.");
print("Usage: dart-sass <input>\n");
print(argParser.usage);
exitCode = 64; exitCode = 64;
return; return;
} }
@ -94,3 +100,10 @@ Future<String> _loadVersion() async {
.split(" ") .split(" ")
.last; .last;
} }
/// Print the usage information for Sass, with [message] as a header.
void _printUsage(ArgParser parser, String message) {
print("$message\n");
print("Usage: dart-sass <input>\n");
print(parser.usage);
}