diff options
author | Vito Caputo <vcaputo@pengaru.com> | 2021-10-03 17:15:09 -0700 |
---|---|---|
committer | Vito Caputo <vcaputo@pengaru.com> | 2021-10-03 17:24:24 -0700 |
commit | 9a98cfe6224757ad21c223f74efb537d9edca24f (patch) | |
tree | c2ccfe5d4d4b1a56c7f02af54bec88bb2a0fe87e /src/til_args.c | |
parent | b686b405c6a22b26e9b8082c92ed91513608bea3 (diff) |
args: move argument parsing/help output to libtil
This is totally opt-in for libtil callers, but is a step
towards enabling uniform cli invocations across frontends.
The help side of this is particularly janky, but since what's
appropriate there is directly related to the args parsing it
seems appropriate to bring along. The janky part is the
implicit output formatting assumptions being made, as-is it
doesn't really lend itself well to being augmented into broader
frontend help output. Alas, this is rototiller playground, so
let's just go easy and assume frontends will largely spit out
whatever this provides - or completely replace it if appropriate.
Diffstat (limited to 'src/til_args.c')
-rw-r--r-- | src/til_args.c | 47 |
1 files changed, 47 insertions, 0 deletions
diff --git a/src/til_args.c b/src/til_args.c new file mode 100644 index 0000000..ae1346c --- /dev/null +++ b/src/til_args.c @@ -0,0 +1,47 @@ +#include <assert.h> +#include <errno.h> +#include <string.h> + +#include "til_args.h" + +/* + * ./rototiller --video=drm,dev=/dev/dri/card3,connector=VGA-1,mode=640x480@60 + * ./rototiller --video=sdl,size=640x480 + * ./rototiller --module=roto,foo=bar,module=settings + * ./rototiller --defaults + */ +int til_args_parse(int argc, const char *argv[], til_args_t *res_args) +{ + assert(argc > 0); + assert(argv); + assert(res_args); + + /* this is intentionally being kept very simple, no new dependencies like getopt. */ + + for (int i = 1; i < argc; i++) { + if (!strncmp("--video=", argv[i], 8)) { + res_args->video = &argv[i][8]; + } else if (!strncmp("--module=", argv[i], 9)) { + res_args->module = &argv[i][9]; + } else if (!strcmp("--defaults", argv[i])) { + res_args->use_defaults = 1; + } else if (!strcmp("--help", argv[i])) { + res_args->help = 1; + } else { + return -EINVAL; + } + } + + return 0; +} + + +int til_args_help(FILE *out) +{ + return fprintf(out, + " --defaults use defaults for unspecified settings\n" + " --help this help\n" + " --module= module settings\n" + " --video= video settings\n" + ); +} |