On Sat, Jan 09 2021, Christian Weisgerber <[email protected]> wrote:
> Edgar Pettijohn:
>
>> In the BUGS section for the getopt(3) manual it mentions not using
>> single digits for options. I know spamd uses -4 and -6 there are
>> probably others. Should they be changed? Or is the manual mistaken?
>
> You misunderstand. The manual warns against the use of digits to
> pass numerical arguments. This usage exists in some historical
> cases, e.g. "nice -10" where <10> is the number 10.
IMO giving a *number* as an example would make things clearer.
Index: getopt.3
===================================================================
RCS file: /d/cvs/src/lib/libc/stdlib/getopt.3,v
retrieving revision 1.46
diff -u -p -p -u -r1.46 getopt.3
--- getopt.3 4 Jan 2016 19:43:13 -0000 1.46
+++ getopt.3 9 Jan 2021 13:39:06 -0000
@@ -326,7 +326,7 @@ It is possible to handle digits as optio
This allows
.Fn getopt
to be used with programs that expect a number
-.Pq Dq Li \-3
+.Pq Dq Li \-389
as an option.
This practice is wrong, and should not be used in any current development.
It is provided for backward compatibility
Other ideas for improvement:
- give a real life example
Index: getopt.3
===================================================================
RCS file: /d/cvs/src/lib/libc/stdlib/getopt.3,v
retrieving revision 1.46
diff -u -p -p -u -r1.46 getopt.3
--- getopt.3 4 Jan 2016 19:43:13 -0000 1.46
+++ getopt.3 9 Jan 2021 13:44:29 -0000
@@ -326,7 +326,7 @@ It is possible to handle digits as optio
This allows
.Fn getopt
to be used with programs that expect a number
-.Pq Dq Li \-3
+.Pq Dq Li nice \-10 program
as an option.
This practice is wrong, and should not be used in any current development.
It is provided for backward compatibility
- move this warning to CAVEATS, since it's not a bug
Index: getopt.3
===================================================================
RCS file: /d/cvs/src/lib/libc/stdlib/getopt.3,v
retrieving revision 1.46
diff -u -p -p -u -r1.46 getopt.3
--- getopt.3 4 Jan 2016 19:43:13 -0000 1.46
+++ getopt.3 9 Jan 2021 13:47:08 -0000
@@ -309,24 +309,12 @@ The
.Fn getopt
function appeared in
.Bx 4.3 .
-.Sh BUGS
-The
-.Fn getopt
-function was once specified to return
-.Dv EOF
-instead of \-1.
-This was changed by
-.St -p1003.2-92
-to decouple
-.Fn getopt
-from
-.In stdio.h .
-.Pp
+.Sh CAVEATS
It is possible to handle digits as option letters.
This allows
.Fn getopt
to be used with programs that expect a number
-.Pq Dq Li \-3
+.Pq Dq Li nice \-10 program
as an option.
This practice is wrong, and should not be used in any current development.
It is provided for backward compatibility
@@ -361,3 +349,15 @@ while ((ch = getopt(argc, argv, "0123456
prevoptind = optind;
}
.Ed
+.Sh BUGS
+The
+.Fn getopt
+function was once specified to return
+.Dv EOF
+instead of \-1.
+This was changed by
+.St -p1003.2-92
+to decouple
+.Fn getopt
+from
+.In stdio.h .
Would the mention of EOF vs -1 fit better in HISTORY or CAVEATS too?
--
jca | PGP : 0x1524E7EE / 5135 92C1 AD36 5293 2BDF DDCC 0DFA 74AE 1524 E7EE