1 .\" $FreeBSD: src/usr.bin/getopt/getopt.1,v 1.10.2.5 2002/12/29 16:35:39 schweikh Exp $
8 .Nd parse command options
10 .Nm args=\`getopt Ar optstring $*\`
11 ; errcode=$?; set \-\- $args
15 utility is deprecated.
16 New shell scripts should use the POSIX
18 shell builtin, as described in the
24 utility is used to break up options in command lines for easy parsing by
25 shell procedures, and to check for legal options.
27 is a string of recognized option letters (see
29 if a letter is followed by a colon, the option
30 is expected to have an argument which may or may not be
31 separated from it by white space.
34 is used to delimit the end of the options.
39 in the arguments at the end of the options,
40 or recognize it if used explicitly.
42 (\fB$1 $2\fR ...) are reset so that each option is
45 and in its own shell argument;
46 each option argument is also in its own shell argument.
50 The following code fragment shows how one might process the arguments
51 for a command that can take the options
57 which requires an argument.
58 .Bd -literal -offset indent
59 args=\`getopt abo: $*\`
60 # you should not use \`getopt abo: "$@"\` since that would parse
61 # the arguments differently from what the set command below does.
68 # You cannot use the set command with a backquoted getopt directly,
69 # since the exit code from getopt would be shadowed by those of set,
70 # which is zero by definition.
76 echo flag $i set; sflags="${i#-}$sflags";
79 echo oarg is "'"$2"'"; oarg="$2"; shift;
85 echo single-char flags: "'"$sflags"'"
86 echo oarg is "'"$oarg"'"
89 This code will accept any of the following as equivalent:
90 .Bd -literal -offset indent
92 cmd \-a \-o arg file file
93 cmd \-oarg -a file file
94 cmd \-a \-oarg \-\- file file
99 utility prints an error message on the standard error output
100 when it encounters an option letter not included in
108 working from a Bell Labs manual page.
109 Behavior believed identical to the Bell version.
118 Arguments containing white space or embedded shell metacharacters
119 generally will not survive intact; this looks easy to fix but
120 isn't. People trying to fix
122 or the example in this manpage should check the history of this file
126 The error message for an invalid option is identified as coming
129 rather than from the shell procedure containing the invocation
132 this again is hard to fix.
134 The precise best way to use the
136 command to set the arguments without disrupting the value(s) of
137 shell options varies from one shell version to another.
139 Each shellscript has to carry complex code to parse arguments halfway
140 correctly (like the example presented here). A better getopt-like tool
141 would move much of the complexity into the tool and keep the client
142 shell scripts simpler.
147 shell builtin provides a better way to perform this task.