[PATCH] Treat `-T's optional argument as optional

[PATCH] Treat `-T's optional argument as optional

From: Klemens Nanni
`-T[format]' is not possible with getopt(3) but getopt_long(3) supports
"T::" exactly for that, so make the command line option go in line with
configuration files and documentation.

While here, check `has_arg' explicitly as getopt_long(3) only documents
mnemonic values not numerical ones.
---
 catgirl.1 | 4 ++--
 chat.c    | 3 ++-
 2 files changed, 4 insertions(+), 3 deletions(-)

diff --git a/catgirl.1 b/catgirl.1
index 5412a52..55c4205 100644
--- a/catgirl.1
+++ b/catgirl.1
@@ -15,7 +15,7 @@
 .Op Fl N Ar notify
 .Op Fl O Ar open
 .Op Fl S Ar bind
-.Op Fl T Ar timestamp
+.Op Fl T Op Ar timestamp
 .Op Fl a Ar plain
 .Op Fl c Ar cert
 .Op Fl h Ar host
@@ -188,7 +188,7 @@ Bind to source address
 .Ar host
 when connecting to the server.
 .
-.It Fl T Ar format | Cm timestamp Op = Ar format
+.It Fl T Oo Ar format Oc | Cm timestamp Op = Ar format
 Show timestamps by default,
 in the specified
 .Xr strftime 3
diff --git a/chat.c b/chat.c
index 3f6aa71..6cc7a6b 100644
--- a/chat.c
+++ b/chat.c
@@ -193,7 +193,8 @@ int main(int argc, char *argv[]) {
 	char opts[2 * ARRAY_LEN(options)];
 	for (size_t i = 0, j = 0; i < ARRAY_LEN(options); ++i) {
 		opts[j++] = options[i].val;
-		if (options[i].has_arg) opts[j++] = ':';
+		if (options[i].has_arg != no_argument) opts[j++] = ':';
+		if (options[i].has_arg == optional_argument) opts[j++] = ':';
 	}
 
 	for (int opt; 0 < (opt = getopt_config(argc, argv, opts, options, NULL));) {
-- 
2.32.0

Re: [PATCH] Treat `-T's optional argument as optional

From: june
On Sun, Jun 13, 2021, at 19:39, Klemens Nanni wrote:
> `-T[format]' is not possible with getopt(3) but getopt_long(3) supports
> "T::" exactly for that, so make the command line option go in line with
> configuration files and documentation.
> 
> While here, check `has_arg' explicitly as getopt_long(3) only documents
> mnemonic values not numerical ones.

Huh, I went reading the manuals for getopt_long(3) and getopt(3)
on various platforms and missed the "::" behaviour a couple times.
I'm going to add Ns between the Fl and the Op Ar though, since
optional arguments can't be preceded by whitespace.