diff options
Diffstat (limited to 'src/opt.c')
-rw-r--r-- | src/opt.c | 68 |
1 files changed, 49 insertions, 19 deletions
@@ -1,15 +1,12 @@ -#include "version.h" #include "opt.h" +#include "version.h" +#include "err.h" + #include "../reqs/simple-opt/simple-opt.h" #include <stdlib.h> - -const char *set[] = { - [EVERY_OPT_OUTPUTMODE_CONSOLE] = "console", - [EVERY_OPT_OUTPUTMODE_SCRIPT] = "script", - NULL, -}; +#include <string.h> static struct simple_opt options[] = { { SIMPLE_OPT_FLAG, 'h', "help", false, @@ -18,16 +15,15 @@ static struct simple_opt options[] = { "print the version of every in use and exit" }, { SIMPLE_OPT_STRING, 'c', "calendar", true, "path to calendar", "<file>" }, - { SIMPLE_OPT_STRING_SET, 'm', "output-mode", true, - "output mode (default is console)", - "(console|script)", set }, + { SIMPLE_OPT_STRING, 'e', "editor", true, + "text editor for editing calendars", "<file>" }, { SIMPLE_OPT_END } }; +static struct simple_opt_result result; + void opt_parse(int argc, char **argv) { - struct simple_opt_result result; - result = simple_opt_parse(argc, argv, options); /* parse err */ @@ -51,11 +47,27 @@ void opt_parse(int argc, char **argv) exit(EXIT_SUCCESS); } - /* bad calendar path*/ - if (options[2].was_seen && options[2].val.v_string[0] == '\0') { - printf("err: could not read calendar at ``\n"); - exit(EXIT_FAILURE); + /* bad command */ + if (result.argc > 0) { + if (result.argc > 1) + ERR("too many commands"); + + if (strlen(result.argv[0]) > 1) + ERR("unrecognised command `%s`", result.argv[0]); + + switch (result.argv[0][0]) { + case 'c': + case 's': + case 'e': + break; + default: + ERR("unrecognised command `%s`", result.argv[0]); + } } + + /* bad calendar path */ + if (options[2].was_seen && options[2].val.v_string[0] == '\0') + ERR("err: could not read calendar at ``"); } char* opt_calpath(void) @@ -66,10 +78,28 @@ char* opt_calpath(void) return NULL; } -enum every_opt_outputmode opt_outputmode(void) +char* opt_editor(void) { if (options[3].was_seen) - return options[3].val.v_string_set_idx; + return options[3].val.v_string; + + return getenv("EDITOR"); +} + +enum every_opt_command opt_command(void) +{ + if (result.argc > 0) { + switch (result.argv[0][0]) { + case 'c': + return OPT_COMMAND_CONSOLE; + case 's': + return OPT_COMMAND_SCRIPT; + case 'e': + return OPT_COMMAND_EDIT; + default: + break; + } + } - return EVERY_OPT_OUTPUTMODE_CONSOLE; + return OPT_COMMAND_CONSOLE; } |