diff --git a/lib/commands/create_command.dart b/lib/commands/create_command.dart index 6f817fd..5eb6851 100644 --- a/lib/commands/create_command.dart +++ b/lib/commands/create_command.dart @@ -24,7 +24,13 @@ Future createCommand(ArgResults command) async { 'Choose a name for your project: ', desc: 'Note: this must be a valid dart identifier (no dashes). ' 'For example: my_game', - validate: (it) => !it.contains('-') && it != 'test', + validate: (it) => switch (it) { + _ when it.isEmpty => 'Name cannot be empty', + _ when it.contains('-') => 'Name cannot contain dashes', + _ when it == 'test' => 'Name cannot be "test", ' + 'as it conflicts with the Dart package', + _ => null, + }, ); final org = getString( @@ -35,7 +41,11 @@ Future createCommand(ArgResults command) async { desc: 'Note: this is a dot separated list of "packages", ' 'normally in reverse domain notation. ' 'For example: org.flame_engine.games', - validate: (it) => !it.contains('-'), + validate: (it) => switch (it) { + _ when it.isEmpty => 'Org cannot be empty', + _ when it.contains('-') => 'Org cannot contain dashes', + _ => null, + }, ); final versions = FlameVersionManager.singleton.versions; diff --git a/lib/commands/version_command.dart b/lib/commands/version_command.dart index f6f0185..3d0a6e9 100644 --- a/lib/commands/version_command.dart +++ b/lib/commands/version_command.dart @@ -8,4 +8,4 @@ Future versionCommand() async { await runExecutableArguments('dart', ['--version'], verbose: true); print(''); await runExecutableArguments('flutter', ['--version'], verbose: true); -} \ No newline at end of file +} diff --git a/lib/utils.dart b/lib/utils.dart index 0b6e621..3ab2d06 100644 --- a/lib/utils.dart +++ b/lib/utils.dart @@ -17,7 +17,7 @@ String getString( String message, { required bool isInteractive, String? desc, - bool Function(String)? validate, + String? Function(String)? validate, }) { var value = results[name] as String?; if (!isInteractive) { @@ -25,12 +25,31 @@ String getString( print('Missing parameter $name is required.'); exit(1); } + final error = validate?.call(value); + if (error != null) { + print('Invalid value $value provided: $error'); + exit(1); + } } while (value == null || value.isEmpty) { if (desc != null) { stdout.write(ansi.darkGray.wrap('\n$desc\u{1B}[1A\r')); } - value = prompts.get(message, validate: validate); + value = prompts.get( + message, + validate: (e) { + final error = validate?.call(e); + if (error != null) { + // clear the line + stdout.write('\n\r\u{1B}[K'); + stdout.write(ansi.red.wrap('$error\u{1B}[1A\r')); + return false; + } else { + stdout.write('\n\r\u{1B}[K'); + return true; + } + }, + ); if (desc != null) { stdout.write('\r\u{1B}[K'); } @@ -77,14 +96,14 @@ String getOption( } List _unwrap(dynamic value) { - return switch(value) { + return switch (value) { null => [], String _ => [value], List _ => value, List _ => value.map((e) => e.toString()).toList(), _ => throw ArgumentError( - 'Invalid type for value (${value.runtimeType}): $value', - ), + 'Invalid type for value (${value.runtimeType}): $value', + ), }; } @@ -118,9 +137,7 @@ List getMultiOption( if (desc != null) { stdout.write(ansi.darkGray.wrap('\n$desc\u{1B}[1A\r')); } - final selectedOptions = value.isEmpty - ? startingOptions - : value; + final selectedOptions = value.isEmpty ? startingOptions : value; value = cbx(message, options, selectedOptions); if (desc != null) { stdout.write('\r\u{1B}[K');