Treat `-T's optional argument as optional

`-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.
weechat-hashes
Klemens Nanni 2021-06-13 23:39:23 +00:00 committed by C. McEnroe
parent e18c585701
commit 1ccadd7c72
2 changed files with 5 additions and 4 deletions

View File

@ -15,7 +15,7 @@
.Op Fl N Ar notify .Op Fl N Ar notify
.Op Fl O Ar open .Op Fl O Ar open
.Op Fl S Ar bind .Op Fl S Ar bind
.Op Fl T Ar timestamp .Op Fl T Ns Op Ar timestamp
.Op Fl a Ar plain .Op Fl a Ar plain
.Op Fl c Ar cert .Op Fl c Ar cert
.Op Fl h Ar host .Op Fl h Ar host
@ -188,7 +188,7 @@ Bind to source address
.Ar host .Ar host
when connecting to the server. when connecting to the server.
. .
.It Fl T Ar format | Cm timestamp Op = Ar format .It Fl T Ns Oo Ar format Oc | Cm timestamp Op = Ar format
Show timestamps by default, Show timestamps by default,
in the specified in the specified
.Xr strftime 3 .Xr strftime 3

5
chat.c
View File

@ -190,10 +190,11 @@ int main(int argc, char *argv[]) {
{ .val = 'w', .name = "pass", required_argument }, { .val = 'w', .name = "pass", required_argument },
{0}, {0},
}; };
char opts[2 * ARRAY_LEN(options)]; char opts[3 * ARRAY_LEN(options)];
for (size_t i = 0, j = 0; i < ARRAY_LEN(options); ++i) { for (size_t i = 0, j = 0; i < ARRAY_LEN(options); ++i) {
opts[j++] = options[i].val; 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));) { for (int opt; 0 < (opt = getopt_config(argc, argv, opts, options, NULL));) {