2016-01-01 21:12:40 +00:00
getopts(1) -- Parse CLI options
===============================
## SYNOPSIS
`getopts` [*options* ...]< br >
`getopts` [*options* ...] `|` `while` read -l key value; ...; `end` < br >
## DESCRIPTION
2016-02-20 13:19:25 +00:00
**Getopts** is a command line options parser for fish.
2016-01-01 21:12:40 +00:00
## USAGE
2016-02-20 13:19:25 +00:00
The best way to understand how getopts work is by studying a basic example.
2016-01-01 21:12:40 +00:00
```
getopts -ab1 --foo=bar baz
```
2016-02-07 11:06:04 +00:00
And its output.
2016-01-01 21:12:40 +00:00
```
a
b 1
foo bar
_ baz
```
The items on the left represent the option flags or *keys* associated with the CLI. The items on the right are the option *values* . The underscore `_` character is the default *key* for arguments without a key.
2016-02-20 13:19:25 +00:00
Use read(1) to process the generated stream and switch(1) to match patterns:
2016-01-01 21:12:40 +00:00
```
getopts -ab1 --foo=bar baz | while read -l key option
switch $key
case _
case a
case b
case foo
end
end
```
## EXAMPLES
2016-02-20 13:19:25 +00:00
The following is a mock of fish(1) CLI.
2016-01-01 21:12:40 +00:00
```
function fish
set -l mode
set -l flags
set -l commands
set -l debug_level
getopts $argv | while read -l key value
switch $key
case c command
set commands $commands $value
case d debug-level
set debug_level $value
case i interactive
set mode $value
case l login
set mode $value
case n no-execute
set mode $value
case p profile
set flags $flags $value
case h help
2016-02-15 09:34:25 +00:00
printf "Usage: $_ [OPTIONS] [-c command] [FILE [ARGUMENTS...]]\n"
2016-01-01 21:12:40 +00:00
return
case \*
printf "$_: '%s' is not a valid option.\n" $key
return 1
end
end
# Implementation
end
```
2016-02-20 13:19:25 +00:00
## NOTES
2016-01-01 21:12:40 +00:00
* A double dash, `--` , marks the end of options. Arguments after this sequence are placed in the default underscore key, `_` .
## AUTHORS
Jorge Bucaran < j @ bucaran . me > .