|
| 1 | +--- |
| 2 | +layout: post |
| 3 | +title: "parse_quick_parameters.sh" |
| 4 | +description: "" |
| 5 | +category: |
| 6 | +tags: [] |
| 7 | +--- |
| 8 | +{% include JB/setup %} |
| 9 | +layout: post |
| 10 | +title: "parse-quick-parameters" |
| 11 | +description: "Given a docstring, configures named parameters for a bash function" |
| 12 | +category: bash |
| 13 | +tags: [bash] |
| 14 | +--- |
| 15 | + |
| 16 | +Provide a documentation string to get named function parameters. Fork of msknapp's. Similar to docopt |
| 17 | + |
| 18 | + |
| 19 | +## Installation |
| 20 | + |
| 21 | +Just copy the file or use package managers. |
| 22 | + |
| 23 | +### Use bkpg |
| 24 | + |
| 25 | +Get [bpkg](https://www.bpkg.sh/). |
| 26 | + |
| 27 | +To get the source version: |
| 28 | +```bash |
| 29 | +bpkg install matthewdeanmartin/parse_quick_parameters.sh |
| 30 | +``` |
| 31 | + |
| 32 | +To get the hosted version: |
| 33 | +```bash |
| 34 | +bpkg install parse_quick_parameters |
| 35 | +``` |
| 36 | + |
| 37 | +### Use git-remote-get |
| 38 | + |
| 39 | +This is a fancy way of just downloading git files. |
| 40 | +``` |
| 41 | +pip install git-remote-get |
| 42 | +git-remote-get ./ --owner matthewdeanmartin --repo "parse_quick_parameters.sh" deps |
| 43 | +``` |
| 44 | + |
| 45 | +## Usage |
| 46 | + |
| 47 | +```bash |
| 48 | +#!/usr/bin/env bash |
| 49 | +source deps/parse_quick_parameters/parse_quick_parameters.sh |
| 50 | + |
| 51 | +function do_something_quick { |
| 52 | + parse_quick_parameters "my_file=-f|--file,bool:run=-r|--run,name=-n|--name" "$@" || return 0 |
| 53 | + echo "my file is: $my_file" |
| 54 | + echo "my name is: $name" |
| 55 | + echo "run is: $run" |
| 56 | +} |
| 57 | +``` |
| 58 | + |
| 59 | +```terminal |
| 60 | +>> source parse_quick_parameters.sh |
| 61 | +>> do_something_quick --name name --run --file file |
| 62 | +my file is: file |
| 63 | +my name is: name |
| 64 | +run is: true |
| 65 | +>> do_something_quick "file" "name" --run |
| 66 | +my file is: file |
| 67 | +my name is: name |
| 68 | +run is: true |
| 69 | +``` |
| 70 | + |
| 71 | + |
| 72 | + |
| 73 | +## Notes |
| 74 | + |
| 75 | +Shellcheck doesn't understand the pattern and will raise [SC2154](https://github.com/koalaman/shellcheck/wiki/SC2154) |
| 76 | + |
| 77 | +## Alternatives |
| 78 | + |
| 79 | +- [getopts](https://en.wikipedia.org/wiki/Getopts) Builtin bash command |
| 80 | +- [docopts](https://github.com/docopt/docopts) Shell version of `docopts` |
| 81 | +- [argparse-bash](https://github.com/nhoffman/argparse-bash) Use python's argparse in bash |
| 82 | + |
| 83 | +## Contributing |
| 84 | + |
| 85 | +Use `Makefile`. `bats` tests assume `bats` was installed with `npm`. |
| 86 | +``` |
| 87 | +make format lint test |
| 88 | +``` |
| 89 | + |
| 90 | + |
| 91 | +## Credits |
| 92 | + |
| 93 | +- [msknapp](https://github.com/msknapp/maintainable-bash/blob/master/1_init/3.1_parameters/shortcut.sh#L3) |
0 commit comments