mirror of
https://github.com/tldr-pages/tldr.git
synced 2025-07-23 01:55:32 +02:00
getopts: add page (#15237)
This commit is contained in:
parent
d0838c6531
commit
352ee6de86
1 changed files with 25 additions and 0 deletions
25
pages/common/getopts.md
Normal file
25
pages/common/getopts.md
Normal file
|
@ -0,0 +1,25 @@
|
|||
# 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`
|
Loading…
Add table
Reference in a new issue