. \" generated with Ronn/v0.7.3
. \" http://github.com/rtomayko/ronn/tree/0.7.3
.
.TH "GETOPTS" "1" "February 2016" "" "fisherman"
.
.SH "NAME"
\fB getopts\fR \- Parse CLI options
.
.SH "SYNOPSIS"
\fB getopts\fR [\fI options\fR \. \. \. ]
.
.br
\fB getopts\fR [\fI options\fR \. \. \. ] \fB |\fR \fB while\fR read \- l key value; \. \. \. ; \fB end\fR
.
.br
.
.SH "DESCRIPTION"
\fB Getopts\fR is a command line options parser for fish\.
.
.SH "USAGE"
The best way to understand how getopts work is by studying a basic example\.
.
.IP "" 4
.
.nf
getopts \- ab1 \- \- foo=bar baz
.
.fi
.
.IP "" 0
.
.P
And its output\.
.
.IP "" 4
.
.nf
a
b 1
foo bar
_ baz
.
.fi
.
.IP "" 0
.
.P
The items on the left represent the option flags or \fI keys\fR associated with the CLI\. The items on the right are the option \fI values\fR \. The underscore \fB _\fR character is the default \fI key\fR for arguments without a key\.
.
.P
Use read(1) to process the generated stream and switch(1) to match patterns:
.
.IP "" 4
.
.nf
getopts \- ab1 \- \- foo=bar baz | while read \- l key option
switch $key
case _
case a
case b
case foo
end
end
.
.fi
.
.IP "" 0
.
.SH "EXAMPLES"
The following is a mock of fish(1) CLI\.
.
.IP "" 4
.
.nf
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
printf "Usage: $_ [OPTIONS] [\- c command] [FILE [ARGUMENTS\. \. \. ]]\e n"
return
case \e *
printf "$_: \' %s\' is not a valid option\. \e n" $key
return 1
end
end
# Implementation
end
.
.fi
.
.IP "" 0
.
.SH "NOTES"
.
.IP "\(bu" 4
A double dash, \fB \- \- \fR , marks the end of options\. Arguments after this sequence are placed in the default underscore key, \fB _\fR \.
.
.IP "" 0
.
.SH "AUTHORS"
Jorge Bucaran \fI j@bucaran\. me\fR \.