parse_quick_parameters.sh
layout: post title: “parse-quick-parameters” description: “Given a docstring, configures named parameters for a bash function” category: bash tags: [bash] —
Provide a documentation string to get named function parameters. Fork of msknapp’s. Similar to docopt
Installation
Just copy the file or use package managers.
Use bkpg
Get bpkg.
To get the source version:
bpkg install matthewdeanmartin/parse_quick_parameters.sh
To get the hosted version:
bpkg install parse_quick_parameters
Use git-remote-get
This is a fancy way of just downloading git files.
pip install git-remote-get
git-remote-get ./ --owner matthewdeanmartin --repo "parse_quick_parameters.sh" deps
Usage
#!/usr/bin/env bash
source deps/parse_quick_parameters/parse_quick_parameters.sh
function do_something_quick {
parse_quick_parameters "my_file=-f|--file,bool:run=-r|--run,name=-n|--name" "$@" || return 0
echo "my file is: $my_file"
echo "my name is: $name"
echo "run is: $run"
}
>> source parse_quick_parameters.sh
>> do_something_quick --name name --run --file file
my file is: file
my name is: name
run is: true
>> do_something_quick "file" "name" --run
my file is: file
my name is: name
run is: true
Notes
Shellcheck doesn’t understand the pattern and will raise SC2154
Alternatives
- getopts Builtin bash command
- docopts Shell version of
docopts
- argparse-bash Use python’s argparse in bash
Contributing
Use Makefile
. bats
tests assume bats
was installed with npm
.
make format lint test
Credits
blog comments powered by Disqus