diff --git a/awesome-message.c b/awesome-message.c index d30042828..982aefcd4 100644 --- a/awesome-message.c +++ b/awesome-message.c @@ -21,6 +21,7 @@ #define _GNU_SOURCE #include + #include #include @@ -122,7 +123,7 @@ main(int argc, char **argv) Bool running = True; Area geometry = { 0, 0, 200, 50, NULL }, icon_geometry = { -1, -1, -1, -1, NULL }; - int opt, option_index = 0; + int opt; char *configfile = NULL; static struct option long_options[] = { @@ -137,7 +138,7 @@ main(int argc, char **argv) globalconf.display = disp; while((opt = getopt_long(argc, argv, "vhf:b:x:y:n:c:", - long_options, &option_index)) != -1) + long_options, NULL)) != -1) switch(opt) { case 'v': diff --git a/awesome.c b/awesome.c index d7e8428b7..2325f125a 100644 --- a/awesome.c +++ b/awesome.c @@ -19,6 +19,9 @@ * */ +#define _GNU_SOURCE +#include + #include #include #include @@ -214,30 +217,36 @@ main(int argc, char *argv[]) int i, cmdlen; event_handler **handler; struct sockaddr_un *addr; - int args_ok = 1; + int opt; + static struct option long_options[] = { + {"help", 0, NULL, 'h'}, + {"version", 0, NULL, 'v'}, + {NULL, 0, NULL, 0} + }; /* check args */ - /* XXX switch to getopt */ - if(argc >= 2) - { - args_ok = 0; - if(!a_strcmp("-v", argv[1]) || !a_strcmp("--version", argv[1])) - eprint_version("awesome"); - else if(!a_strcmp("-h", argv[1]) || !a_strcmp("--help", argv[1])) - exit_help(EXIT_SUCCESS); - else if(!a_strcmp("-c", argv[1])) + while((opt = getopt_long(argc, argv, "vhkc:", + long_options, NULL)) != -1) + switch(opt) { - if(a_strlen(argv[2])) - confpath = argv[2], args_ok = 1; + case 'v': + eprint_version("awesome"); + break; + case 'h': + exit_help(EXIT_SUCCESS); + break; + case 'c': + if(a_strlen(optarg)) + confpath = optarg; else eprint("-c option requires a file name\n"); - } - else if(!a_strcmp("-k", argv[1])) + break; + case 'k': return config_check(confpath); - else - exit_help(EXIT_FAILURE); - } - if(!args_ok) + break; + } + + if(argc - optind < 1) exit_help(EXIT_FAILURE); /* Text won't be printed correctly otherwise */