From 7c133c73a3cbd660171c4928e547597bafbc3aa3 Mon Sep 17 00:00:00 2001 From: Natalie Weizenbaum Date: Thu, 13 Jul 2017 01:25:14 -0700 Subject: [PATCH] Gracefully handle argument parse errors --- lib/src/executable.dart | 21 +++++++++++++++++---- 1 file changed, 17 insertions(+), 4 deletions(-) diff --git a/lib/src/executable.dart b/lib/src/executable.dart index 6652716f..21cda50a 100644 --- a/lib/src/executable.dart +++ b/lib/src/executable.dart @@ -27,7 +27,15 @@ main(List args) async { abbr: 'h', help: 'Print this usage information.', negatable: false) ..addFlag('version', 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) { _loadVersion().then((version) { @@ -38,9 +46,7 @@ main(List args) async { } if (options['help'] as bool || options.rest.isEmpty) { - print("Compile Sass to CSS.\n"); - print("Usage: dart-sass \n"); - print(argParser.usage); + _printUsage(argParser, "Compile Sass to CSS."); exitCode = 64; return; } @@ -94,3 +100,10 @@ Future _loadVersion() async { .split(" ") .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 \n"); + print(parser.usage); +}