]> Pileus Git - lackey/commitdiff
Add args and usage output
authorAndy Spencer <andy753421@gmail.com>
Thu, 24 Nov 2016 19:33:05 +0000 (19:33 +0000)
committerAndy Spencer <andy753421@gmail.com>
Mon, 28 Nov 2016 09:54:02 +0000 (09:54 +0000)
makefile
src/args.c [new file with mode: 0644]
src/args.h [new file with mode: 0644]
src/conf.c
src/conf.h
src/main.c

index 5dde712bc6c3a0af533208cf76b0356f56e421b2..f50f07445b9229007b7df637a2df4241953d08a8 100644 (file)
--- a/makefile
+++ b/makefile
@@ -16,7 +16,7 @@ LDFLAGS   ?= -lncursesw -lical
 
 # Sources
 PROG      ?= lackey
-PROG_SRC  ?= main view date cal conf util
+PROG_SRC  ?= main view date cal args conf util
 TEST      ?= test
 TEST_SRC  ?= test date cal conf util
 VIEWS     ?= day week month year events todo settings help edit
diff --git a/src/args.c b/src/args.c
new file mode 100644 (file)
index 0000000..afc0b48
--- /dev/null
@@ -0,0 +1,68 @@
+/*
+ * Copyright (C) 2016 Andy Spencer <andy753421@gmail.com>
+ *
+ * This program is free software: you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation, either version 3 of the License, or
+ * (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program.  If not, see <http://www.gnu.org/licenses/>.
+ */
+
+#include <stdlib.h>
+#include <stdio.h>
+#include <getopt.h>
+
+#include "args.h"
+#include "util.h"
+
+/* Setup info */
+static int    argc;
+static char **argv;
+
+/* Options */
+const char *short_options = "h";
+
+struct option long_options[] = {
+       {"help", 0, NULL, 'h'},
+};
+
+/* Usage */
+static void usage(char *name)
+{
+       printf("Usage:\n");
+       printf("  %s [OPTION...]\n", name);
+       printf("\n");
+       printf("Options:\n");
+       printf("  -h, --help  Print usage information\n");
+}
+
+/* Initialize */
+void args_setup(int _argc, char **_argv)
+{
+       argc = _argc;
+       argv = _argv;
+}
+
+/* Initialize */
+void args_init(void)
+{
+       while (1) {
+               int c = getopt_long(argc, argv,
+                               short_options, long_options, NULL);
+               if (c == -1)
+                       break;
+               switch (c) {
+                       case 'h':
+                               usage(argv[0]);
+                               exit(0);
+                               break;
+               }
+       }
+}
diff --git a/src/args.h b/src/args.h
new file mode 100644 (file)
index 0000000..cca1b5e
--- /dev/null
@@ -0,0 +1,20 @@
+/*
+ * Copyright (C) 2016 Andy Spencer <andy753421@gmail.com>
+ *
+ * This program is free software: you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation, either version 3 of the License, or
+ * (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program.  If not, see <http://www.gnu.org/licenses/>.
+ */
+
+/* Functions */
+void args_setup(int argc, char **argv);
+void args_init(void);
index e379cbf4f598bab3cafefacf93ede4499ae8068d..1a81b91c2e160f4035c45d3d4a9cc388e9d3fb01 100644 (file)
@@ -44,8 +44,6 @@ static const char *booleans[] = {
 /* Setup info */
 static char     *filename;
 static parser_t  parser;
-static int       argc;
-static char    **argv;
 
 /* Static data */
 static line_t   *settings;
@@ -316,14 +314,12 @@ void conf_save(const char *path)
 }
 
 /* Initialize */
-void conf_setup(int _argc, char **_argv, const char *_name, parser_t _parser)
+void conf_setup(const char *_name, parser_t _parser)
 {
        const char *home = getenv("HOME");
        filename = alloc0(strlen(home) + 1 + strlen(_name) + 1);
        sprintf(filename, "%s/%s", home, _name);
        parser   = _parser;
-       argc     = _argc;
-       argv     = _argv;
 }
 
 /* Initialize */
index 9b0c5939fadbeadada1c424fcca95ae0de32a85b..b716cf6b2140d5954667110f4209c629f638d416 100644 (file)
@@ -40,6 +40,6 @@ void set_name(const char *group, const char *name,
                const char *value);
 
 /* Functions */
-void conf_setup(int argc, char **argv, const char *name, parser_t parser);
+void conf_setup(const char *name, parser_t parser);
 void conf_init(void);
 void conf_sync(void);
index c10ea67135d785c86882d3d72cefa13d7e024018..f957575f7ffd13fb6c4f7c2fbd951feedf72be32 100644 (file)
@@ -18,6 +18,7 @@
 #include <stdlib.h>
 #include <signal.h>
 
+#include "args.h"
 #include "util.h"
 #include "conf.h"
 #include "date.h"
@@ -45,9 +46,11 @@ int main(int argc, char **argv)
        signal(SIGINT, on_sigint);
 
        /* Configuration */
-       conf_setup(argc, argv, ".lackeyrc", on_config);
+       args_setup(argc, argv);
+       conf_setup(".lackeyrc", on_config);
 
        /* Initialize */
+       args_init();
        util_init();
        conf_init();
        date_init();