add -Wpedantic to default CFLAGS
[baseutils.git] / getopt.1
1 .\"
2 .Dd May 2026
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 .Sh BUGS
100 Whatever
101 .Xr getopt 3
102 has.
103 .Pp
104 Arguments containing white space or embedded shell metacharacters
105 generally will not survive intact; this looks easy to fix but
106 is not.
107 People trying to fix
108 .Nm
109 or the example in this manpage should check the history of this file
110 in
111 .Fx .
112 .Pp
113 The error message for an invalid option is identified as coming
114 from
115 .Nm
116 rather than from the shell procedure containing the invocation
117 of
118 .Nm ;
119 this again is hard to fix.
120 .Pp
121 The precise best way to use the
122 .Nm set
123 command to set the arguments without disrupting the value(s) of
124 shell options varies from one shell version to another.
125 .Pp
126 Each shellscript has to carry complex code to parse arguments halfway
127 correctly (like the example presented here).
128 A better getopt-like tool
129 would move much of the complexity into the tool and keep the client
130 shell scripts simpler.