Generate short option string with a loop

Also change the way option structs are initialized so that the array
sorts the same way as the switch statement.
master
C. McEnroe 2020-04-02 14:14:43 -04:00
parent a0f012c614
commit 06543b7030
1 changed files with 30 additions and 25 deletions

55
chat.c
View File

@ -127,35 +127,40 @@ int main(int argc, char *argv[]) {
const char *user = NULL; const char *user = NULL;
const char *real = NULL; const char *real = NULL;
const char *Opts = "!C:H:N:O:RS:a:c:eg:h:i:j:k:ln:p:r:s:u:vw:"; struct option options[] = {
const struct option LongOpts[] = { { .val = '!', .name = "insecure", no_argument },
{ "insecure", no_argument, NULL, '!' }, { .val = 'C', .name = "copy", required_argument },
{ "copy", required_argument, NULL, 'C' }, { .val = 'H', .name = "hash", required_argument },
{ "hash", required_argument, NULL, 'H' }, { .val = 'N', .name = "notify", required_argument },
{ "notify", required_argument, NULL, 'N' }, { .val = 'O', .name = "open", required_argument },
{ "open", required_argument, NULL, 'O' }, { .val = 'R', .name = "restrict", no_argument },
{ "restrict", no_argument, NULL, 'R' }, { .val = 'S', .name = "bind", required_argument },
{ "bind", required_argument, NULL, 'S' }, { .val = 'a', .name = "sasl-plain", required_argument },
{ "sasl-plain", required_argument, NULL, 'a' }, { .val = 'c', .name = "cert", required_argument },
{ "cert", required_argument, NULL, 'c' }, { .val = 'e', .name = "sasl-external", no_argument },
{ "sasl-external", no_argument, NULL, 'e' }, { .val = 'g', .name = "generate", required_argument },
{ "host", required_argument, NULL, 'h' }, { .val = 'h', .name = "host", required_argument },
{ "ignore", required_argument, NULL, 'i' }, { .val = 'i', .name = "ignore", required_argument },
{ "join", required_argument, NULL, 'j' }, { .val = 'j', .name = "join", required_argument },
{ "priv", required_argument, NULL, 'k' }, { .val = 'k', .name = "priv", required_argument },
{ "log", no_argument, NULL, 'l' }, { .val = 'l', .name = "log", no_argument },
{ "nick", required_argument, NULL, 'n' }, { .val = 'n', .name = "nick", required_argument },
{ "port", required_argument, NULL, 'p' }, { .val = 'p', .name = "port", required_argument },
{ "real", required_argument, NULL, 'r' }, { .val = 'r', .name = "real", required_argument },
{ "save", required_argument, NULL, 's' }, { .val = 's', .name = "save", required_argument },
{ "user", required_argument, NULL, 'u' }, { .val = 'u', .name = "user", required_argument },
{ "debug", no_argument, NULL, 'v' }, { .val = 'v', .name = "debug", no_argument },
{ "pass", required_argument, NULL, 'w' }, { .val = 'w', .name = "pass", required_argument },
{0}, {0},
}; };
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++] = ':';
}
int opt; int opt;
while (0 < (opt = getopt_config(argc, argv, Opts, LongOpts, NULL))) { while (0 < (opt = getopt_config(argc, argv, opts, options, NULL))) {
switch (opt) { switch (opt) {
break; case '!': insecure = true; break; case '!': insecure = true;
break; case 'C': utilPush(&urlCopyUtil, optarg); break; case 'C': utilPush(&urlCopyUtil, optarg);