xref: /freebsd/usr.bin/getopt/getopt.1 (revision b2c76c41)
149364d3cSMike Pritchard.\"
2c47f6acfSJohn-Mark Gurney.Dd August 1, 2015
3b37b9a6dSNate Williams.Dt GETOPT 1
4b37b9a6dSNate Williams.Os
5b37b9a6dSNate Williams.Sh NAME
6b37b9a6dSNate Williams.Nm getopt
7b37b9a6dSNate Williams.Nd parse command options
8b37b9a6dSNate Williams.Sh SYNOPSIS
9eec64f74SMartin Cracauer.Nm args=\`getopt Ar optstring $*\`
10eec64f74SMartin Cracauer; errcode=$?; set \-\- $args
11b37b9a6dSNate Williams.Sh DESCRIPTION
123898680cSPhilippe CharnierThe
133898680cSPhilippe Charnier.Nm
143898680cSPhilippe Charnierutility is used to break up options in command lines for easy parsing by
15b37b9a6dSNate Williamsshell procedures, and to check for legal options.
160b6471c9SJoerg Wunsch.Ar Optstring
17b37b9a6dSNate Williamsis a string of recognized option letters (see
1894ba280cSRuslan Ermilov.Xr getopt 3 ) ;
19b37b9a6dSNate Williamsif a letter is followed by a colon, the option
20b37b9a6dSNate Williamsis expected to have an argument which may or may not be
21b37b9a6dSNate Williamsseparated from it by white space.
22b37b9a6dSNate WilliamsThe special option
230b6471c9SJoerg Wunsch.Ql \-\-
24b37b9a6dSNate Williamsis used to delimit the end of the options.
253898680cSPhilippe CharnierThe
263898680cSPhilippe Charnier.Nm
273898680cSPhilippe Charnierutility will place
280b6471c9SJoerg Wunsch.Ql \-\-
29b37b9a6dSNate Williamsin the arguments at the end of the options,
30b37b9a6dSNate Williamsor recognize it if used explicitly.
31b37b9a6dSNate WilliamsThe shell arguments
32b37b9a6dSNate Williams(\fB$1 $2\fR ...) are reset so that each option is
33b37b9a6dSNate Williamspreceded by a
340b6471c9SJoerg Wunsch.Ql \-
35b37b9a6dSNate Williamsand in its own shell argument;
36b37b9a6dSNate Williamseach option argument is also in its own shell argument.
376c7216dfSRuslan Ermilov.Sh EXIT STATUS
386c7216dfSRuslan ErmilovThe
396c7216dfSRuslan Ermilov.Nm
406c7216dfSRuslan Ermilovutility prints an error message on the standard error output and exits with
416c7216dfSRuslan Ermilovstatus > 0 when it encounters an option letter not included in
426c7216dfSRuslan Ermilov.Ar optstring .
43251c176fSRuslan Ermilov.Sh EXAMPLES
44b37b9a6dSNate WilliamsThe following code fragment shows how one might process the arguments
45b37b9a6dSNate Williamsfor a command that can take the options
460b6471c9SJoerg Wunsch.Fl a
47b37b9a6dSNate Williamsand
480b6471c9SJoerg Wunsch.Fl b ,
49b37b9a6dSNate Williamsand the option
500b6471c9SJoerg Wunsch.Fl o ,
51b37b9a6dSNate Williamswhich requires an argument.
52b37b9a6dSNate Williams.Bd -literal -offset indent
53eec64f74SMartin Cracauerargs=\`getopt abo: $*\`
54eec64f74SMartin Cracauer# you should not use \`getopt abo: "$@"\` since that would parse
55eec64f74SMartin Cracauer# the arguments differently from what the set command below does.
56200520dfSUlrich Spörleinif [ $? -ne 0 ]; then
57b37b9a6dSNate Williams	echo 'Usage: ...'
58b37b9a6dSNate Williams	exit 2
59b37b9a6dSNate Williamsfi
60eec64f74SMartin Cracauerset \-\- $args
61eec64f74SMartin Cracauer# You cannot use the set command with a backquoted getopt directly,
62eec64f74SMartin Cracauer# since the exit code from getopt would be shadowed by those of set,
63eec64f74SMartin Cracauer# which is zero by definition.
64c47f6acfSJohn-Mark Gurneywhile :; do
65200520dfSUlrich Spörlein	case "$1" in
66b37b9a6dSNate Williams	\-a|\-b)
67200520dfSUlrich Spörlein		echo "flag $1 set"; sflags="${1#-}$sflags"
68200520dfSUlrich Spörlein		shift
69200520dfSUlrich Spörlein		;;
70b37b9a6dSNate Williams	\-o)
71200520dfSUlrich Spörlein		echo "oarg is '$2'"; oarg="$2"
72200520dfSUlrich Spörlein		shift; shift
73200520dfSUlrich Spörlein		;;
74b37b9a6dSNate Williams	\-\-)
75200520dfSUlrich Spörlein		shift; break
76200520dfSUlrich Spörlein		;;
77b37b9a6dSNate Williams	esac
78b37b9a6dSNate Williamsdone
79200520dfSUlrich Spörleinecho "single-char flags: '$sflags'"
80200520dfSUlrich Spörleinecho "oarg is '$oarg'"
81b37b9a6dSNate Williams.Ed
82b37b9a6dSNate Williams.Pp
83b37b9a6dSNate WilliamsThis code will accept any of the following as equivalent:
84b37b9a6dSNate Williams.Bd -literal -offset indent
85c47f6acfSJohn-Mark Gurneycmd \-aoarg file1 file2
86c47f6acfSJohn-Mark Gurneycmd \-a \-o arg file1 file2
87c47f6acfSJohn-Mark Gurneycmd \-oarg -a file1 file2
88c47f6acfSJohn-Mark Gurneycmd \-a \-oarg \-\- file1 file2
89b37b9a6dSNate Williams.Ed
90b37b9a6dSNate Williams.Sh SEE ALSO
91b2a75fdfSRuslan Ermilov.Xr getopts 1 ,
92b37b9a6dSNate Williams.Xr sh 1 ,
93b37b9a6dSNate Williams.Xr getopt 3
94b37b9a6dSNate Williams.Sh HISTORY
950fdf8af9SAlexey ZelkinWritten by
960fdf8af9SAlexey Zelkin.An Henry Spencer ,
970fdf8af9SAlexey Zelkinworking from a Bell Labs manual page.
9887faa07bSSheldon HearnBehavior believed identical to the Bell version.
9987faa07bSSheldon HearnExample changed in
10018b3ba26SMartin Cracauer.Fx
10118b3ba26SMartin Cracauerversion 3.2 and 4.0.
102b37b9a6dSNate Williams.Sh BUGS
103b37b9a6dSNate WilliamsWhatever
104b37b9a6dSNate Williams.Xr getopt 3
105b37b9a6dSNate Williamshas.
106b37b9a6dSNate Williams.Pp
1070ab2a7aeSMartin CracauerArguments containing white space or embedded shell metacharacters
108eec64f74SMartin Cracauergenerally will not survive intact; this looks easy to fix but
1090227791bSRuslan Ermilovis not.
1106a3e8b0aSRuslan ErmilovPeople trying to fix
1118fe908efSRuslan Ermilov.Nm
112eec64f74SMartin Cracaueror the example in this manpage should check the history of this file
113eec64f74SMartin Cracauerin
114eec64f74SMartin Cracauer.Fx .
115b37b9a6dSNate Williams.Pp
116b37b9a6dSNate WilliamsThe error message for an invalid option is identified as coming
117b37b9a6dSNate Williamsfrom
1188fe908efSRuslan Ermilov.Nm
119b37b9a6dSNate Williamsrather than from the shell procedure containing the invocation
120b37b9a6dSNate Williamsof
1218fe908efSRuslan Ermilov.Nm ;
1220ab2a7aeSMartin Cracauerthis again is hard to fix.
1230ab2a7aeSMartin Cracauer.Pp
1240ab2a7aeSMartin CracauerThe precise best way to use the
1250ab2a7aeSMartin Cracauer.Nm set
1260ab2a7aeSMartin Cracauercommand to set the arguments without disrupting the value(s) of
1270ab2a7aeSMartin Cracauershell options varies from one shell version to another.
128eec64f74SMartin Cracauer.Pp
129eec64f74SMartin CracauerEach shellscript has to carry complex code to parse arguments halfway
1306a3e8b0aSRuslan Ermilovcorrectly (like the example presented here).
1316a3e8b0aSRuslan ErmilovA better getopt-like tool
132eec64f74SMartin Cracauerwould move much of the complexity into the tool and keep the client
133eec64f74SMartin Cracauershell scripts simpler.
134