@@ -23,6 +23,15 @@ function readScript(arg) {
2323 return fs . readFileSync ( arg , "utf8" ) ;
2424}
2525
26+ function parseMax ( value ) {
27+ if ( value === undefined ) throw new Error ( "moshcode run: --max requires a positive integer" ) ;
28+ const max = Number ( value ) ;
29+ if ( ! Number . isInteger ( max ) || max < 1 ) {
30+ throw new Error ( `moshcode run: --max must be a positive integer, got ${ JSON . stringify ( value ) } ` ) ;
31+ }
32+ return max ;
33+ }
34+
2635function help ( ) {
2736 console . log ( `moshcode — metal scripting toolkit 🤘
2837
@@ -80,7 +89,10 @@ async function main() {
8089 let max = 3 , dryRun = false , file = null ;
8190 for ( let k = 0 ; k < rest . length ; k ++ ) {
8291 const a = rest [ k ] ;
83- if ( a === "--max" || a === "-n" ) max = Number ( rest [ ++ k ] ) ;
92+ if ( a === "--max" || a === "-n" ) {
93+ try { max = parseMax ( rest [ ++ k ] ) ; }
94+ catch ( e ) { console . error ( String ( e . message || e ) ) ; process . exit ( 1 ) ; }
95+ }
8496 else if ( a === "--dry-run" ) dryRun = true ;
8597 else file = a ;
8698 }
@@ -92,7 +104,7 @@ async function main() {
92104 const ctx = {
93105 vars : { alive : true } ,
94106 iter : 0 ,
95- maxIterations : Number . isFinite ( max ) && max > 0 ? max : 3 ,
107+ maxIterations : max ,
96108 dryRun,
97109 out : ( s ) => console . log ( s ) ,
98110 commands : defaultCommands ( ) ,
0 commit comments