diff options
author | katherine <shmibs@airen-no-jikken.icu> | 2019-05-08 07:06:27 -0700 |
---|---|---|
committer | katherine <shmibs@airen-no-jikken.icu> | 2019-05-08 07:06:27 -0700 |
commit | ca0d95e26663e05d702c6f3a5627812dbf0c9f90 (patch) | |
tree | 3ed14314639ffa1421877c766ddc2c3d72fe9df9 /src/opt.c | |
download | confconf-ca0d95e26663e05d702c6f3a5627812dbf0c9f90.tar.gz |
initial commit
Diffstat (limited to 'src/opt.c')
-rw-r--r-- | src/opt.c | 62 |
1 files changed, 62 insertions, 0 deletions
diff --git a/src/opt.c b/src/opt.c new file mode 100644 index 0000000..f4a1969 --- /dev/null +++ b/src/opt.c @@ -0,0 +1,62 @@ +#include "opt.h" + +#include "../reqs/simple-opt/simple-opt.h" + +#include <stdbool.h> +#include <stdlib.h> +#include <stdio.h> + +static struct simple_opt options[] = { + { SIMPLE_OPT_FLAG, 'h', "help", false, + "print this help message and exit" }, + { SIMPLE_OPT_FLAG, 'v', "version", false, + "print the version of confconf in use and exit" }, + { SIMPLE_OPT_STRING, 'i', "input", true, + "specify file to read from (default is stdin)", "<file>" }, + { SIMPLE_OPT_STRING, 'o', "output", true, + "specify file to write to (default is stdout)", "<file>" }, + { SIMPLE_OPT_END } +}; + +void opt_parse(int argc, char **argv) +{ + struct simple_opt_result result; + const char version[] = "confconf develop"; + + result = simple_opt_parse(argc, argv, options); + + /* parse err */ + if (result.result_type != SIMPLE_OPT_RESULT_SUCCESS) { + simple_opt_print_error(stderr, 80, argv[0], result); + exit(EXIT_FAILURE); + } + + /* help */ + if (options[0].was_seen) { + simple_opt_print_usage(stdout, 70, argv[0], + "[-i input] [-o output]", + "confconf is a config file parser generator for C", + options); + exit(EXIT_SUCCESS); + } + + /* version */ + if (options[1].was_seen) { + puts(version); + exit(EXIT_SUCCESS); + } +} + +const char* opt_infile_str(void) +{ + return (options[2].was_seen + ? options[2].val_string + : NULL); +} + +const char* opt_outfile_str(void) +{ + return (options[3].was_seen + ? options[3].val_string + : NULL); +} |