]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - usr.bin/getopt/getopt.1
Merge llvm-project release/18.x llvmorg-18.1.3-0-gc13b7485b879
[FreeBSD/FreeBSD.git] / usr.bin / getopt / getopt.1
1 .\"
2 .Dd August 1, 2015
3 .Dt GETOPT 1
4 .Os
5 .Sh NAME
6 .Nm getopt
7 .Nd parse command options
8 .Sh SYNOPSIS
9 .Nm args=\`getopt Ar optstring $*\`
10 ; errcode=$?; set \-\- $args
11 .Sh DESCRIPTION
12 The
13 .Nm
14 utility is used to break up options in command lines for easy parsing by
15 shell procedures, and to check for legal options.
16 .Ar Optstring
17 is a string of recognized option letters (see
18 .Xr getopt 3 ) ;
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.
22 The special option
23 .Ql \-\-
24 is used to delimit the end of the options.
25 The
26 .Nm
27 utility will place
28 .Ql \-\-
29 in the arguments at the end of the options,
30 or recognize it if used explicitly.
31 The shell arguments
32 (\fB$1 $2\fR ...) are reset so that each option is
33 preceded by a
34 .Ql \-
35 and in its own shell argument;
36 each option argument is also in its own shell argument.
37 .Sh EXIT STATUS
38 The
39 .Nm
40 utility prints an error message on the standard error output and exits with
41 status > 0 when it encounters an option letter not included in
42 .Ar optstring .
43 .Sh EXAMPLES
44 The following code fragment shows how one might process the arguments
45 for a command that can take the options
46 .Fl a
47 and
48 .Fl b ,
49 and the option
50 .Fl o ,
51 which requires an argument.
52 .Bd -literal -offset indent
53 args=\`getopt abo: $*\`
54 # you should not use \`getopt abo: "$@"\` since that would parse
55 # the arguments differently from what the set command below does.
56 if [ $? -ne 0 ]; then
57         echo 'Usage: ...'
58         exit 2
59 fi
60 set \-\- $args
61 # You cannot use the set command with a backquoted getopt directly,
62 # since the exit code from getopt would be shadowed by those of set,
63 # which is zero by definition.
64 while :; do
65         case "$1" in
66         \-a|\-b)
67                 echo "flag $1 set"; sflags="${1#-}$sflags"
68                 shift
69                 ;;
70         \-o)
71                 echo "oarg is '$2'"; oarg="$2"
72                 shift; shift
73                 ;;
74         \-\-)
75                 shift; break
76                 ;;
77         esac
78 done
79 echo "single-char flags: '$sflags'"
80 echo "oarg is '$oarg'"
81 .Ed
82 .Pp
83 This code will accept any of the following as equivalent:
84 .Bd -literal -offset indent
85 cmd \-aoarg file1 file2
86 cmd \-a \-o arg file1 file2
87 cmd \-oarg -a file1 file2
88 cmd \-a \-oarg \-\- file1 file2
89 .Ed
90 .Sh SEE ALSO
91 .Xr getopts 1 ,
92 .Xr sh 1 ,
93 .Xr getopt 3
94 .Sh HISTORY
95 Written by
96 .An Henry Spencer ,
97 working from a Bell Labs manual page.
98 Behavior believed identical to the Bell version.
99 Example changed in
100 .Fx
101 version 3.2 and 4.0.
102 .Sh BUGS
103 Whatever
104 .Xr getopt 3
105 has.
106 .Pp
107 Arguments containing white space or embedded shell metacharacters
108 generally will not survive intact; this looks easy to fix but
109 is not.
110 People trying to fix
111 .Nm
112 or the example in this manpage should check the history of this file
113 in
114 .Fx .
115 .Pp
116 The error message for an invalid option is identified as coming
117 from
118 .Nm
119 rather than from the shell procedure containing the invocation
120 of
121 .Nm ;
122 this again is hard to fix.
123 .Pp
124 The precise best way to use the
125 .Nm set
126 command to set the arguments without disrupting the value(s) of
127 shell options varies from one shell version to another.
128 .Pp
129 Each shellscript has to carry complex code to parse arguments halfway
130 correctly (like the example presented here).
131 A better getopt-like tool
132 would move much of the complexity into the tool and keep the client
133 shell scripts simpler.