diff options
| author | 2022-06-03 19:45:06 -0500 | |
|---|---|---|
| committer | 2022-06-03 19:45:06 -0500 | |
| commit | ab1e784f7fa3cc6dda98613bafbfaf9ffbd1bf2f (patch) | |
| tree | b21ff264f2d9b8347e3a15404d6b614276f242e4 /bin | |
| parent | Update suite0014 (diff) | |
Add janet-format binary script.
Diffstat (limited to 'bin')
| -rw-r--r-- | bin/janet-format | 46 |
1 files changed, 46 insertions, 0 deletions
diff --git a/bin/janet-format b/bin/janet-format new file mode 100644 index 0000000..bb23873 --- /dev/null +++ b/bin/janet-format @@ -0,0 +1,46 @@ +#!/usr/bin/env janet + +(import spork/fmt) +(import spork/argparse) + +(defn- main + [&] + + (def ap + (argparse/argparse + "Format Janet source code in files and write output to those files." + + :default + {:kind :accumulate + :help "Files to format"} + + "files" + {:short "f" + :help "Format a list of source files." + :kind :flag} + + "output" + {:short "o" + :kind :option + :help "Where to direct output to. By default, output goes to stdout."} + + "input" + {:short "i" + :kind :option + :help "Read from an input file"})) + + # Break on help text + (unless ap (break)) + + (if (or (ap "files") (ap :default)) + (each file (ap :default) + (eprint "formatting " file "...") + (fmt/format-file file)) + (if-let [ofile (ap "output")] + (with [output (file/open ofile :wb)] + (if-let [ifile (ap "input")] + (xprin output (fmt/format (slurp ifile))) + (xprin output (fmt/format (file/read stdin :all))))) + (if-let [ifile (ap "input")] + (prin (fmt/format (slurp ifile))) + (prin (fmt/format (file/read stdin :all))))))) |
