JanetDocsSourcePlaygroundTutorialsI'm Feeling luckyCommunityGitHub sign in
# Expanding the example from https://janetdocs.org/core-api/stdin # We use argparse to provide automatically add flags # --help comes for free, try it! (import spork/argparse) (defn main [&] (let [opts (argparse/argparse "Sum numbers from files, stdin or data." "multiply" {:kind :option :short "m" :default "1" :map scan-number :help "Multiply the result"} "verbose" {:kind :flag :help "Print the input first"} :default {:kind :accumulate}) _ (unless opts (os/exit 0)) # lest it crash on help etc. args (or (opts :default) []) # give empty? empty array of :default is nil data (cond (empty? args) (file/read stdin :all) (os/stat (first args)) (slurp (first args)) (string/join args " ")) total (* (opts "multiply") (sum (flatten (parse-all data))))] (if (opts "verbose") (print "Data: " data)) (print total)))