1 .\" $NetBSD: getopt.1,v 1.17 2009/03/11 13:53:30 joerg 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
19 if a letter is followed by a colon, the option
20 is expected to have an argument which may or may not be
21 separated from it by white space.
24 is used to delimit the end of the options.
28 in the arguments at the end of the options,
29 or recognize it if used explicitly.
31 .Pq Ev $1 , Ev $2 , ...
32 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.
39 should not be used in new scripts, use the shell builtin
43 The following code fragment shows how one might process the arguments
44 for a command that can take the options
50 which requires an argument.
52 .Bd -literal -offset indent
53 args=\`getopt abc: $*\`
54 if [ $? \-ne 0 ]; then
59 while [ $# \-gt 0 ]; do
75 This code will accept any of the following as equivalent:
77 .Bd -literal -offset indent
79 cmd \-a \-c arg file file
80 cmd \-carg -a file file
81 cmd \-a \-carg \-\- file file
87 set command return the value of 0 for the exit status.
88 Therefore, the exit status of the
94 set command are used on the same line.
95 The example given is one way to detect errors found by
99 prints an error message on the standard error output when it
100 encounters an option letter not included in
106 Written by Henry Spencer, working from a Bell Labs manual page.
107 Behavior believed identical to the Bell version.
113 Arguments containing white space or embedded shell metacharacters
114 generally will not survive intact; this looks easy to fix but isn't.
116 The error message for an invalid option is identified as coming
119 rather than from the shell procedure containing the invocation
122 this again is hard to fix.
124 The precise best way to use the
126 command to set the arguments without disrupting the value(s) of
127 shell options varies from one shell version to another.