1 .\" $NetBSD: getopt.1,v 1.8 1997/10/19 02:16:57 lukem Exp $
7 .Nd parse command options
9 .Li args=\`getopt optstring $*\`
11 .Li set \-\- \`getopt optstring $*\`
14 is used to break up options in command lines for easy parsing by
15 shell procedures, and to check for legal options.
17 is a string of recognized option letters (see
20 if a letter is followed by a colon, the option
21 is expected to have an argument which may or may not be
22 separated from it by white space.
25 is used to delimit the end of the options.
29 in the arguments at the end of the options,
30 or recognize it if used explicitly.
32 (\fB$1 $2\fR ...) are reset so that each option is
35 and in its own shell argument;
36 each option argument is also in its own shell argument.
38 The following code fragment shows how one might process the arguments
39 for a command that can take the options
45 which requires an argument.
47 .Bd -literal -offset indent
48 args=\`getopt abo: $*\`
62 oarg=$2; shift; shift;;
69 This code will accept any of the following as equivalent:
71 .Bd -literal -offset indent
73 cmd \-a \-o arg file file
74 cmd \-oarg -a file file
75 cmd \-a \-oarg \-\- file file
81 set command return the value of 0 for the exit status. Therefore,
82 the exit status of the
88 set command are used on the same line. The example given
89 is one way to detect errors found by
96 prints an error message on the standard error output when it
97 encounters an option letter not included in
100 Written by Henry Spencer, working from a Bell Labs manual page.
101 Behavior believed identical to the Bell version.
107 Arguments containing white space or embedded shell metacharacters
108 generally will not survive intact; this looks easy to fix but isn't.
110 The error message for an invalid option is identified as coming
113 rather than from the shell procedure containing the invocation
116 this again is hard to fix.
118 The precise best way to use the
120 command to set the arguments without disrupting the value(s) of
121 shell options varies from one shell version to another.