From 290aa13ddd8d05bde86f14a82cd76c18b4b8f082 Mon Sep 17 00:00:00 2001 From: jvech Date: Wed, 21 Jun 2023 20:23:29 -0500 Subject: add: CLI support added --- src/args.c | 34 ++++++++++++++++++++++++++++++++++ 1 file changed, 34 insertions(+) create mode 100644 src/args.c (limited to 'src/args.c') diff --git a/src/args.c b/src/args.c new file mode 100644 index 0000000..6a516da --- /dev/null +++ b/src/args.c @@ -0,0 +1,34 @@ +#include "args.h" +int args_optind = 1; + +int +args_getopt(int argc, char *argv[], const char *optstring, char **args_optarg) +{ + char option; + if (args_optind >= argc) return -1; + + if (argv[args_optind][0] != '-' + || strlen(argv[args_optind]) > 2 + || !strcmp(argv[args_optind], "-")) { + + *args_optarg = argv[args_optind]; + args_optind++; + return 0; + } + + int i; + option = argv[args_optind][1]; + for (i = 0; i < strlen(optstring); i++) { + if (optstring[i + 1] == ':' && optstring[i] == option) { + *args_optarg = argv[++args_optind]; + args_optind++; + return option; + } else if (optstring[i + 1] != ':' && optstring[i] == option) { + args_optind++; + return option; + } + } + args_optarg = 0; + args_optind++; + return -2; +} -- cgit v1.2.3-70-g09d2