Janet 1.39.1-e9c6678 Documentation
(Other Versions: 1.38.0 1.37.1 1.36.0 1.35.0 1.34.0 1.31.0 1.29.1 1.28.0 1.27.0 1.26.0 1.25.1 1.24.0 1.23.0 1.22.0 1.21.0 1.20.0 1.19.0 1.18.1 1.17.1 1.16.1 1.15.0 1.13.1 1.12.2 1.11.1 1.10.1 1.9.1 1.8.1 1.7.0 1.6.0 1.5.1 1.5.0 1.4.0 1.3.1 )

argparse

A moderately opinionated argument parser for janet. Use this for writing CLI scripts that need to have UNIX style switches and options.

Sample

#!/usr/bin/env janet

(import spork/argparse :prefix "")

(def argparse-params
  ["A simple CLI tool. An example to show the capabilities of argparse."
   "debug" {:kind :flag
            :short "d"
            :help "Set debug mode."}
   "verbose" {:kind :multi
              :short "v"
              :help "Print debug information to stdout."}
   "key" {:kind :option
          :short "k"
          :help "An API key for getting stuff from a server."
          :required true}
   "expr" {:kind :accumulate
           :short "e"
           :help "Search for all patterns given."}
   "thing" {:kind :option
            :help "Some option?"
            :default "123"}])

(let [res (argparse ;argparse-params)]
  (unless res
    (os/exit 1))
  (pp res))

Usage

Call argparse/argparse to attempt to parse the command line args (available at (dyn :args)).

The first argument should be a description to be displayed as help text.

All subsequent options should be alternating keys and values where the keys are options to accept and the values are definitions of each option.

To accept positional arguments, include a definition for the special value :default. For instance, to gather all positional arguments into an array, include :default {:kind :accumulate} in your arguments to argparse.

Run (doc argparse/argparse) after importing for more information.

Reference

argparse/argparse


argparse/argparse function source
(argparse description &keys options)

Parse `(dyn :args)` according to options. If the arguments are incorrect,
will return nil and print usage information.

Each option is a table or struct that specifies a flag or option
for the script. The name of the option should be a string, specified
via `(argparse/argparse "..." op1-name {...} op2-name {...} ...)`. A help option
and usage text is automatically generated for you.

The keys in each option table are as follows:

* `:kind` - What kind of option is this? One of `:flag`, `:multi`, `:option`, or
 `:accumulate`. A flag can either be on or off, a multi is a flag that can be provided
 multiple times, each time adding 1 to a returned integer, an option is a key that
 will be set in the returned table, and accumulate means an option can be specified
 0 or more times, each time appending a value to an array.
* `:short` - Single letter for shorthand access.
* `:help` - Help text for the option, explaining what it is.
* `:default` - Default value for the option.
* `:required` - Whether or not an option is required.
* `:short-circuit` - Whether or not to stop parsing if this option is hit.
 Result will also contain the rest of the arguments under the :rest key.
* `:action` - A function that will be invoked when the option is parsed.
* `:map` - A function that is applied to the value of the option to transform it

There is also a special option name `:default` that will be invoked on arguments
that do not start with a -- or -. Use this option to collect unnamed
arguments to your script. This is separate from the `:default` key in option specifiers.

After "--", every argument is treated as an unnamed argument.

Once parsed, values are accessible in the returned table by the name
of the option. For example `(result "verbose")` will check if the verbose
flag is enabled.
You may also use a custom args array when specified via the special option `:args`.