Use getopt_long() in awesome
Simplify getopt_long usage in awesome-message. Signed-off-by: Julien Danjou <julien@danjou.info>
This commit is contained in:
parent
a28876b5ce
commit
c4345f1be4
|
@ -21,6 +21,7 @@
|
||||||
|
|
||||||
#define _GNU_SOURCE
|
#define _GNU_SOURCE
|
||||||
#include <getopt.h>
|
#include <getopt.h>
|
||||||
|
|
||||||
#include <unistd.h>
|
#include <unistd.h>
|
||||||
#include <stdlib.h>
|
#include <stdlib.h>
|
||||||
|
|
||||||
|
@ -122,7 +123,7 @@ main(int argc, char **argv)
|
||||||
Bool running = True;
|
Bool running = True;
|
||||||
Area geometry = { 0, 0, 200, 50, NULL },
|
Area geometry = { 0, 0, 200, 50, NULL },
|
||||||
icon_geometry = { -1, -1, -1, -1, NULL };
|
icon_geometry = { -1, -1, -1, -1, NULL };
|
||||||
int opt, option_index = 0;
|
int opt;
|
||||||
char *configfile = NULL;
|
char *configfile = NULL;
|
||||||
static struct option long_options[] =
|
static struct option long_options[] =
|
||||||
{
|
{
|
||||||
|
@ -137,7 +138,7 @@ main(int argc, char **argv)
|
||||||
globalconf.display = disp;
|
globalconf.display = disp;
|
||||||
|
|
||||||
while((opt = getopt_long(argc, argv, "vhf:b:x:y:n:c:",
|
while((opt = getopt_long(argc, argv, "vhf:b:x:y:n:c:",
|
||||||
long_options, &option_index)) != -1)
|
long_options, NULL)) != -1)
|
||||||
switch(opt)
|
switch(opt)
|
||||||
{
|
{
|
||||||
case 'v':
|
case 'v':
|
||||||
|
|
45
awesome.c
45
awesome.c
|
@ -19,6 +19,9 @@
|
||||||
*
|
*
|
||||||
*/
|
*/
|
||||||
|
|
||||||
|
#define _GNU_SOURCE
|
||||||
|
#include <getopt.h>
|
||||||
|
|
||||||
#include <errno.h>
|
#include <errno.h>
|
||||||
#include <locale.h>
|
#include <locale.h>
|
||||||
#include <stdio.h>
|
#include <stdio.h>
|
||||||
|
@ -214,30 +217,36 @@ main(int argc, char *argv[])
|
||||||
int i, cmdlen;
|
int i, cmdlen;
|
||||||
event_handler **handler;
|
event_handler **handler;
|
||||||
struct sockaddr_un *addr;
|
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 */
|
/* check args */
|
||||||
/* XXX switch to getopt */
|
while((opt = getopt_long(argc, argv, "vhkc:",
|
||||||
if(argc >= 2)
|
long_options, NULL)) != -1)
|
||||||
{
|
switch(opt)
|
||||||
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]))
|
|
||||||
{
|
{
|
||||||
if(a_strlen(argv[2]))
|
case 'v':
|
||||||
confpath = argv[2], args_ok = 1;
|
eprint_version("awesome");
|
||||||
|
break;
|
||||||
|
case 'h':
|
||||||
|
exit_help(EXIT_SUCCESS);
|
||||||
|
break;
|
||||||
|
case 'c':
|
||||||
|
if(a_strlen(optarg))
|
||||||
|
confpath = optarg;
|
||||||
else
|
else
|
||||||
eprint("-c option requires a file name\n");
|
eprint("-c option requires a file name\n");
|
||||||
}
|
break;
|
||||||
else if(!a_strcmp("-k", argv[1]))
|
case 'k':
|
||||||
return config_check(confpath);
|
return config_check(confpath);
|
||||||
else
|
break;
|
||||||
exit_help(EXIT_FAILURE);
|
}
|
||||||
}
|
|
||||||
if(!args_ok)
|
if(argc - optind < 1)
|
||||||
exit_help(EXIT_FAILURE);
|
exit_help(EXIT_FAILURE);
|
||||||
|
|
||||||
/* Text won't be printed correctly otherwise */
|
/* Text won't be printed correctly otherwise */
|
||||||
|
|
Loading…
Reference in New Issue