Painless shell argument parsing and dependency check.
No Data
clap) - painless argument parsing and dependency check.
Writing code to parse arguments in a shell scripting language (
bash,
zsh,
fishetc...) is an extremly verbose, repetitive, error prone, and painful process.
getoptor something similiar?
getoptand similiar tools are not only language-dependent (slap works with a number of shells), but are also just tools to write your own argument parser.
getoptif you want) that takes in consideration things like argument order, possible values, subcommands, argument groups, automatic color-enabled help menus, most similiar argument name suggestions etc.
You declare your CLI in YAML and pass it to slap's
stdinand pass all your script's arguments to slap as arguments.
stderr.
config="path to your YAML config" eval "$(slap parse bash -- "[email protected]"The
slap-parsesubcommand, if the passed arguments conform to the YAML description, outputs code in the language specified, so you can evaluate it to have access to the variables containing the parsed arguments.
Relax, slap writes tostdoutONLY if the YAML config is valid and the arguments passed conform to it, otherwise it doesn't.Installation
If you're an Arch Linux user, you can install slap from the AUR:
# This will install a binary named `slap`. yay -S slap-cli-binIf you're a Rust programmer, you can install slap with
cargo. Make sure to add~/.cargo/binto your$PATH.# This will install a binary named `slap`. cargo install slap-cliYou can also download a pre-compiled binary (for
linux,linux-arm,macos,win-msvc,win-gnu,win32-msvc) from the Releases.Supported platforms
bash,zsh,fish,elvishandpowershell.
We are planning to support more shells.
If your favourite shell is not supported, make sure to open an issue.Completions script generation
Thanks to clap, slap's underlying engine, automatic completions-script generation is supported. For example in bash:
config="path to your YAML config" slap completions bash completions.bashcompletions.bashnow contains a bash script that provides command autocompletion for the CLI described in your YAML config file.Dependency check
If your script depends on some programs you can check if they are in
$PATHwith thedepssubcommand:slap deps curl jq || exit 1If
curlandjqare found in$PATHthe script will continue its execution and nothing will be printed, otherwise an error will be written tostderrand slap will exit with a non-zero exit code.Absolute path of a script
slap includes a
pathsubcommand that simplifies getting the absolute path of a script:# before abs="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"# with slap abs="$(slap path -d "${BASH_SOURCE[0]}")"Both the snippets never dereference symlinks. If you want to dereference symlinks use the
-Doption.Demo
Example
Here are two useful bash scripts:
slap deps curl jq || exit 1eval "$(slap parse bash _ -- "[email protected]" <
slap deps jq git || exit 1eval "$(slap parse bash _ -- "[email protected]" <
Learning material
This YAML config probably contains all the options you'll ever need.
For additional informations look atclap's docs.powershell,fish,zshand other examples look here.Elvish
As of
v0.14.1, elvish doesn't supportevalyet, so you can use slap to generate elvish code, but you can't yet use the generated code inside an elvish script.
Luckily there is some work going on for this functionality.Credits
This program is solely made possible by clap, so many thanks to its authors.
License
Licensed under either of Apache License, Version 2.0 or MIT license at your option.
Unless you explicitly state otherwise, any contribution intentionally submitted for inclusion in this crate by you, as defined in the Apache-2.0 license, shall be dual licensed as above, without any additional terms or conditions.