1
0
Fork 0
mirror of https://github.com/tldr-pages/tldr.git synced 2025-04-23 09:42:07 +02:00
tldr/pages/common/getopts.md
2024-12-26 08:18:29 +01:00

25 lines
769 B
Markdown

# getopts
> Parse shell options from arguments.
> This command does not support longform options and thus using `getopt` is recommended instead.
> More information: <https://www.gnu.org/software/bash/manual/bash.html#index-getopts>.
- Check if an option is set:
`getopts {{x}} {{opt}}; echo $opt`
- Set an option to require an argument and check said argument:
`getopts {{x}}: {{opt}}; echo $OPTARG`
- Check for multiple options:
`while getopts {{xyz}} {{opt}}; do case $opt in x) echo x is set;; y) echo y is set;; z) echo z is set;; esac; done`
- Set `getopts` to silent mode and handle option errors:
`while getopts :{{x:}} {{opt}}; do case $opt in x) ;; :) echo "Argument required";; ?) echo "Invalid argument" esac;; done`
- Reset `getopts`:
`OPTIND=1`