(42 lines)
1 #!/usr/bin/env nomsu -V7.0.02 ###3 Auto-format Nomsu code. Usage:4 nomsu -t format [-i] file1 file2...6 If the "-i" flag is used, the file will be edited in-place.7 If the "-q" flag is used and an error occurs, the original file will be printed.8 If no files are passed in, this will read from stdin.10 use "filesystem"11 use "commandline"13 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~15 command line program with $args:16 $filenames = $args.extras17 if (#$filenames == 0):18 say ("19 Warning: reading from stdin (ctrl-d to abort). To avoid this message, use nomsu -t format -20 ")21 $filenames = ["stdin"]23 for $filename in $filenames:24 $file = (read file $filename)25 unless $file:26 fail "File does not exist: \$filename"27 $leading_indent = ($file, matching "^\n*([ ]*)")28 $code = (NomsuCode from (Source $filename 1 #$file) $file)29 try:30 $tree = ($code parsed)31 $formatted = ($tree as nomsu, text, indented $leading_indent)32 ..if it fails with $msg:33 if $args.q:34 $formatted = $file35 ..else:36 say $msg37 do next $filename39 if $args.i:40 write $formatted to file $filename41 ..else:42 say $formatted inline