summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorVito Caputo <vcaputo@pengaru.com>2021-10-03 17:15:09 -0700
committerVito Caputo <vcaputo@pengaru.com>2021-10-03 17:24:24 -0700
commit9a98cfe6224757ad21c223f74efb537d9edca24f (patch)
treec2ccfe5d4d4b1a56c7f02af54bec88bb2a0fe87e
parentb686b405c6a22b26e9b8082c92ed91513608bea3 (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.
-rw-r--r--src/Makefile.am2
-rw-r--r--src/main.c61
-rw-r--r--src/til_args.c47
-rw-r--r--src/til_args.h17
4 files changed, 73 insertions, 54 deletions
diff --git a/src/Makefile.am b/src/Makefile.am
index c0a20b5..6ca1d77 100644
--- a/src/Makefile.am
+++ b/src/Makefile.am
@@ -1,7 +1,7 @@
SUBDIRS = libs modules
noinst_LTLIBRARIES = libtil.la
-libtil_la_SOURCES = til_fb.c til_fb.h til_knobs.h til.c til.h til_settings.h til_settings.c til_threads.c til_threads.h til_util.c til_util.h
+libtil_la_SOURCES = til_args.c til_args.h til_fb.c til_fb.h til_knobs.h til.c til.h til_settings.h til_settings.c til_threads.c til_threads.h til_util.c til_util.h
libtil_la_CPPFLAGS = -I@top_srcdir@/src
libtil_la_LIBADD = modules/compose/libcompose.la modules/drizzle/libdrizzle.la modules/flui2d/libflui2d.la modules/julia/libjulia.la modules/meta2d/libmeta2d.la modules/montage/libmontage.la modules/pixbounce/libpixbounce.la modules/plasma/libplasma.la modules/plato/libplato.la modules/ray/libray.la modules/roto/libroto.la modules/rtv/librtv.la modules/snow/libsnow.la modules/sparkler/libsparkler.la modules/spiro/libspiro.la modules/stars/libstars.la modules/submit/libsubmit.la modules/swab/libswab.la modules/swarm/libswarm.la libs/grid/libgrid.la libs/puddle/libpuddle.la libs/ray/libray.la libs/sig/libsig.la libs/txt/libtxt.la libs/ascii/libascii.la libs/din/libdin.la
diff --git a/src/main.c b/src/main.c
index c028998..d07015b 100644
--- a/src/main.c
+++ b/src/main.c
@@ -11,6 +11,7 @@
#include <unistd.h>
#include "til.h"
+#include "til_args.h"
#include "til_settings.h"
#include "til_fb.h"
#include "til_util.h"
@@ -44,48 +45,6 @@ typedef struct rototiller_t {
static rototiller_t rototiller;
-typedef struct argv_t {
- const char *module;
- const char *video;
-
- unsigned use_defaults:1;
- unsigned help:1;
-} argv_t;
-
-/*
- * ./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
- */
-static int parse_argv(int argc, const char *argv[], argv_t *res_args)
-{
- int i;
-
- assert(argc > 0);
- assert(argv);
- assert(res_args);
-
- /* this is intentionally being kept very simple, no new dependencies like getopt. */
-
- for (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;
-}
-
-
typedef struct setup_t {
til_settings_t *module;
til_settings_t *video;
@@ -148,7 +107,7 @@ static int setup_video(til_settings_t *settings, til_setting_desc_t **next_setti
/* turn args into settings, automatically applying defaults if appropriate, or interactively if appropriate. */
/* returns negative value on error, 0 when settings unchanged from args, 1 when changed */
-static int setup_from_args(argv_t *args, setup_t *res_setup)
+static int setup_from_args(til_args_t *args, setup_t *res_setup)
{
int r, changes = 0;
setup_t setup;
@@ -232,15 +191,11 @@ _out:
static int print_help(void)
{
- return printf(
- "Run without any flags or partial settings for interactive mode.\n"
+ printf("Run without any flags or partial settings for interactive mode.\n"
"\n"
- "Supported flags:\n"
- " --defaults use defaults for unspecified settings\n"
- " --help this help\n"
- " --module= module settings\n"
- " --video= video settings\n"
- );
+ "Supported flags:\n");
+
+ return til_args_help(stdout);
}
@@ -282,10 +237,10 @@ static void * rototiller_thread(void *_rt)
int main(int argc, const char *argv[])
{
setup_t setup = {};
- argv_t args = {};
+ til_args_t args = {};
int r;
- exit_if(parse_argv(argc, argv, &args) < 0,
+ exit_if(til_args_parse(argc, argv, &args) < 0,
"unable to process arguments");
if (args.help)
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"
+ );
+}
diff --git a/src/til_args.h b/src/til_args.h
new file mode 100644
index 0000000..018ae80
--- /dev/null
+++ b/src/til_args.h
@@ -0,0 +1,17 @@
+#ifndef _TIL_ARGS_H
+#define _TIL_ARGS_H
+
+#include <stdio.h>
+
+typedef struct til_args_t {
+ const char *module;
+ const char *video;
+
+ unsigned use_defaults:1;
+ unsigned help:1;
+} til_args_t;
+
+int til_args_parse(int argc, const char *argv[], til_args_t *res_args);
+int til_args_help(FILE *out);
+
+#endif
© All Rights Reserved