Skip to content
Bring the power of Clap to shell scripts. Bring the power of Clap to shell scripts.

Claptrap

Bring the power of Clap to shell scripts.
hello.sh
#!/usr/bin/env bash
set -euo pipefail
eval "$(claptrap --spec - -- "$@" <<'SPEC'
name = "hello"
[args]
name = { short = 'n', long = "name", default-value = "world" }
SPEC
)"
echo "hello, ${claptrap_name}!"
Terminal window
$ ./hello.sh -n fuji
hello, fuji!

Powered by Clap

Claptrap brings the full power of Clap command line parsing to shell scripts.

Declarative Specification

Command line interface specifications can be declared in toml, yaml or json and used as standalone files or embedded directly in scripts.

Beyond Parsing

Automatically generate shell completions, man pages, markdown documentation and template scripts.

Run Anywhere

Supports bash and zsh shells and can run on Linux, BSD, macOS, and Windows.

A simple toml specification embedded in a bash script:

simple.sh
#!/usr/bin/env bash
set -euo pipefail
eval "$(claptrap --spec - -- "$@" <<'SPEC'
name = "simple"
version = "0.1.0"
arg-required-else-help = true
[args]
format = { short = "f", long = "format", required = true, value-parser = ["toml", "yaml", "json"] }
exts = { long = "extensions", num-args = "1..", default-values = ["txt", "sh", "rs"] }
SPEC
)"
echo "format: $claptrap_format"
for i in "${!claptrap_exts[@]}"; do
echo "extensions[$i]: ${claptrap_exts[$i]}"
done

Show usage:

Terminal window
$ ./hello.sh
Usage: hello [OPTIONS] --format <format>
Options:
-f, --format <format> [possible values: toml, yaml, json]
--extensions <exts>... [default: txt sh rs]
-h, --help Print help
-V, --version Print version

Parse arguments:

Terminal window
$ ./hello.sh -f json
protocol: json
extensions[0]: txt
extensions[1]: sh
extensions[2]: rs

Error handling:

Terminal window
$ ./hello.sh -f yml --extensions md ts js
error: invalid value 'yml' for '--format <format>'
[possible values: toml, yaml, json]
tip: a similar value exists: 'yaml'
For more information, try '--help'.