Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/svenfuchs/simple_opts.sh
Simple Bash option parser
https://github.com/svenfuchs/simple_opts.sh
Last synced: about 2 months ago
JSON representation
Simple Bash option parser
- Host: GitHub
- URL: https://github.com/svenfuchs/simple_opts.sh
- Owner: svenfuchs
- License: mit
- Created: 2014-11-16T17:28:09.000Z (about 10 years ago)
- Default Branch: master
- Last Pushed: 2014-11-17T16:45:35.000Z (about 10 years ago)
- Last Synced: 2024-10-26T22:39:56.459Z (2 months ago)
- Language: Shell
- Size: 227 KB
- Stars: 31
- Watchers: 3
- Forks: 3
- Open Issues: 1
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# simple\_opts.sh
Simple Bash options parser, no nonsense.
```shell
# define some options
opt --file-name= -f
opt --no-prompt # now defaults to prompt=true
opt --verbose -v
# or on one line: opt --file-name= -f --verbose -v --no-prompt# run the parser
opt_parse --file-name path/to/file.sh -v --no-prompt foo bar
# or: parse foo bar --file-name=path/to/file.sh -v --no-prompt
# or: parse foo --file_name path/to/file.sh bar -v --no-prompt
# or: parse --file_name=path/to/file.sh foo -v bar --no-prompt# will set the following variables
echo "file_name: $file_name"
echo "verbose: $verbose"
echo "prompt: $prompt"
echo "ARGS: ${ARGS[@]}"# file_name: path/to/file.sh
# verbose: true
# prompt:
# ARGS: foo bar
```It is possible to define alternate variable names for options:
```shell
opt --file:path= -f:path=
opt --quiet:silent
opt -p:promptopt_parse --file path/to/file.sh --quiet -p
echo "path: $path"
echo "silent: $silent"
echo "prompt: $prompt"# path: path/to/file.sh
# silent: true
# prompt: true
```Short options can only be given with a long option that starts with the same letter, or with an alternate name.
Options can be given multiple times:
```shell
opt --tag=
opt_parse --tag foo --tag bar
echo "tags: $tags"
# tags: foo bar
```Of course, in reality `opt_parse` would be called with `$@`:
```shell
opt --path= -p --verbose -v
opt_parse $@
```