From e08d7acc3af177bd46a3ee6703f57cb66bf82ac8 Mon Sep 17 00:00:00 2001 From: David Phillips Date: Fri, 7 Aug 2015 16:58:11 +0200 Subject: [PATCH] Allow squashing of command line flags together --- abduco.c | 36 +++++++++++++++++------------------- 1 file changed, 17 insertions(+), 19 deletions(-) diff --git a/abduco.c b/abduco.c index 89429d2..798b224 100644 --- a/abduco.c +++ b/abduco.c @@ -574,38 +574,28 @@ static int list_session(void) { } int main(int argc, char *argv[]) { + int opt; bool force = false; char **cmd = NULL, action = '\0'; server.name = basename(argv[0]); gethostname(server.host+1, sizeof(server.host) - 1); if (argc == 1) exit(list_session()); - for (int arg = 1; arg < argc; arg++) { - if (argv[arg][0] != '-') { - if (!server.session_name) { - server.session_name = argv[arg]; - continue; - } else if (!cmd) { - cmd = &argv[arg]; - break; - } - } - if (server.session_name) - usage(); - switch (argv[arg][1]) { + + while ((opt = getopt(argc, argv, "aAcne:frv")) != -1) { + switch (opt) { case 'a': case 'A': case 'c': case 'n': - action = argv[arg][1]; + action = opt; break; case 'e': - if (arg + 1 >= argc) + if (!optarg) usage(); - char *esc = argv[++arg]; - if (esc[0] == '^' && esc[1]) - *esc = CTRL(esc[1]); - KEY_DETACH = *esc; + if (optarg[0] == '^' && optarg[1]) + optarg[0] = CTRL(optarg[1]); + KEY_DETACH = optarg[0]; break; case 'f': force = true; @@ -621,6 +611,14 @@ int main(int argc, char *argv[]) { } } + /* collect the session name if trailing args */ + if (optind < argc) + server.session_name = argv[optind]; + + /* if yet more trailing arguments, they must be the command */ + if (optind + 1 < argc) + cmd = &argv[optind + 1]; + if (!cmd) { cmd = (char*[]){ getenv("ABDUCO_CMD"), NULL }; if (!cmd[0]) -- 2.11.4.GIT