@@ -17,20 +17,39 @@ String getString(
1717 String message, {
1818 required bool isInteractive,
1919 String ? desc,
20- bool Function (String )? validate,
20+ String ? Function (String )? validate,
2121}) {
2222 var value = results[name] as String ? ;
2323 if (! isInteractive) {
2424 if (value == null || value.isEmpty) {
2525 print ('Missing parameter $name is required.' );
2626 exit (1 );
2727 }
28+ final error = validate? .call (value);
29+ if (error != null ) {
30+ print ('Invalid value $value provided: $error ' );
31+ exit (1 );
32+ }
2833 }
2934 while (value == null || value.isEmpty) {
3035 if (desc != null ) {
3136 stdout.write (ansi.darkGray.wrap ('\n $desc \u {1B}[1A\r ' ));
3237 }
33- value = prompts.get (message, validate: validate);
38+ value = prompts.get (
39+ message,
40+ validate: (e) {
41+ final error = validate? .call (e);
42+ if (error != null ) {
43+ // clear the line
44+ stdout.write ('\n\r\u {1B}[K' );
45+ stdout.write (ansi.red.wrap ('$error \u {1B}[1A\r ' ));
46+ return false ;
47+ } else {
48+ stdout.write ('\n\r\u {1B}[K' );
49+ return true ;
50+ }
51+ },
52+ );
3453 if (desc != null ) {
3554 stdout.write ('\r\u {1B}[K' );
3655 }
@@ -77,14 +96,14 @@ String getOption(
7796}
7897
7998List <String > _unwrap (dynamic value) {
80- return switch (value) {
99+ return switch (value) {
81100 null => [],
82101 String _ => [value],
83102 List <String > _ => value,
84103 List _ => value.map ((e) => e.toString ()).toList (),
85104 _ => throw ArgumentError (
86- 'Invalid type for value (${value .runtimeType }): $value ' ,
87- ),
105+ 'Invalid type for value (${value .runtimeType }): $value ' ,
106+ ),
88107 };
89108}
90109
@@ -118,9 +137,7 @@ List<String> getMultiOption(
118137 if (desc != null ) {
119138 stdout.write (ansi.darkGray.wrap ('\n $desc \u {1B}[1A\r ' ));
120139 }
121- final selectedOptions = value.isEmpty
122- ? startingOptions
123- : value;
140+ final selectedOptions = value.isEmpty ? startingOptions : value;
124141 value = cbx (message, options, selectedOptions);
125142 if (desc != null ) {
126143 stdout.write ('\r\u {1B}[K' );
0 commit comments