wmail: fixed comparisons between signed and unsigned.
[dockapps.git] / wmframepic / src / arg_parser.c
blob566a8beec45bc834da9f0367604e2c751142b7df
1 #include <stdlib.h>
2 #include <string.h>
3 #include "arg_parser.h"
7 int get_values_from_command_line(int argc, char **argv,
8 char *my_name,
9 char **my_file,
10 int *my_day,
11 int *my_month,
12 int *my_year) {
14 struct arg_str *name = arg_str1("n", "name", "<string>", "the name of the person");
15 struct arg_file *xpm_file = arg_file1("f", "xpm_file", "<file>", "the xpm file path");
16 struct arg_int *day = arg_int1("d", "day", "int", "the day of birth");
17 struct arg_int *month = arg_int1("m", "month", "int", "the month of birth");
18 struct arg_int *year = arg_int1("y", "year", "int", "the year of birth");
19 struct arg_lit *version = arg_lit0("v", "version", "print the version and exit");
20 struct arg_lit *help = arg_lit0(NULL, "help", "print this help and exit");
21 struct arg_end *end = arg_end(20);
22 void* argtable[] = {name, xpm_file, day, month, year, help, version, end};
23 const char* progname = "wmframepic";
24 int nerrors;
25 int exitcode = COMMAND_LINE_SUCCESS;
26 int i;
28 /* verify the argtable[] entries were allocated sucessfully */
29 if (arg_nullcheck(argtable) != 0) {
30 /* NULL entries were detected, some allocations must have failed */
31 printf("%s: insufficient memory\n", progname);
32 exitcode = COMMAND_LINE_FAIL;
33 goto exit;
36 /* Parse the command line as defined by argtable[] */
37 nerrors = arg_parse(argc,argv,argtable);
39 /* special case: '--help' takes precedence over error reporting */
40 if (help->count > 0) {
41 printf(
42 "This is a dockapp written for Window Maker and any other window manager\n\
43 that suports the dock (or slit), like the Window Maker itself, Fluxbox, \n\
44 Blackbox and others.\n\n");
45 printf(
46 "It's meant to show the picture of your kid (or any beloved one) on your\n\
47 screen and when pressed, it will show how old he/she is. Pretty silly but\n\
48 I'm sure you will enjoy!\n\n");
50 printf("Usage: %s", progname);
51 arg_print_syntax(stdout, argtable, "\n");
53 arg_print_glossary(stdout, argtable, " %-25s %s\n");
54 exitcode = COMMAND_LINE_HELP;
55 goto exit;
58 if (version->count > 0) {
59 printf("%s\n", PACKAGE_STRING);
60 exitcode = COMMAND_LINE_HELP;
61 goto exit;
64 /* If the parser returned any errors then display them and exit */
65 if (nerrors > 0) {
66 /* Display the error details contained in the arg_end struct.*/
67 arg_print_errors(stdout, end, progname);
68 printf("Try '%s --help' for more information.\n", progname);
69 exitcode = COMMAND_LINE_FAIL;
70 goto exit;
73 /* only get here is command line arguments were parsed sucessfully */
75 strncpy(my_name, *name->sval, 9);
76 *my_file = malloc(strlen(xpm_file->filename[0]) * sizeof(char) + 1);
77 strcpy(*my_file, xpm_file->filename[0]);
78 my_name[9] = '\0';
79 *my_day = *day->ival;
80 *my_month = *month->ival;
81 *my_year = *year->ival;
83 exit:
84 /* deallocate each non-null entry in argtable[] */
85 arg_freetable(argtable,sizeof(argtable)/sizeof(argtable[0]));
87 return exitcode;