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