summaryrefslogtreecommitdiffhomepage
path: root/main.c
diff options
context:
space:
mode:
authorJo-Philipp Wich <jo@mein.io>2020-10-31 23:27:54 +0100
committerJo-Philipp Wich <jo@mein.io>2020-10-31 23:27:54 +0100
commit5fb260f1fcd302cb529bec52520345c77bbbadf8 (patch)
treef7303404dca1012d27241fe4fdb1207ea013f486 /main.c
parent67d7d0f263c1c20fa1e29b755f4f74cda64d4a42 (diff)
main: allow prefixing -e and -E options
By specifying a name, followed by an equal sign before the actual option value, the corresponding JSON data is stored as global variable with the given name, instead of turning each object key into a variable itself. For example while `utpl -e '{ "foo": true, "bar": false }' ...` will set two variables `foo` and `bar`, the alternative syntax `utpl -e 'baz={ "foo": true, "bar": false }' ...` will declare a single variable `baz` holding the object `{ "foo": true, "bar": false }`. Signed-off-by: Jo-Philipp Wich <jo@mein.io>
Diffstat (limited to 'main.c')
-rw-r--r--main.c38
1 files changed, 30 insertions, 8 deletions
diff --git a/main.c b/main.c
index 881b8fd..78d218f 100644
--- a/main.c
+++ b/main.c
@@ -38,7 +38,7 @@ print_usage(char *app)
{
printf(
"== Usage ==\n\n"
- " # %s [-d] [-l] [-r] [-S] [-e '{\"var\": ...}'] [-E env.json] {-i <file> | -s \"utpl script...\"}\n"
+ " # %s [-d] [-l] [-r] [-S] [-e '[prefix=]{\"var\": ...}'] [-E [prefix=]env.json] {-i <file> | -s \"utpl script...\"}\n"
" -h, --help Print this help\n"
" -i file Specify an utpl script to parse\n"
" -s \"utpl script...\" Specify an utpl code fragment to parse\n"
@@ -257,13 +257,13 @@ parse_envfile(FILE *fp)
int
main(int argc, char **argv)
{
- struct json_object *env = NULL, *modules = NULL, *o;
+ struct json_object *env = NULL, *modules = NULL, *o, *p;
struct ut_state *state = NULL;
struct ut_source source = {};
+ char *stdin = NULL, *c;
bool dumponly = false;
bool shebang = false;
FILE *envfile = NULL;
- char *stdin = NULL;
int opt, rv = 0;
if (argc == 1)
@@ -329,18 +329,32 @@ main(int argc, char **argv)
break;
case 'e':
- envfile = fmemopen(optarg, strlen(optarg), "rb");
+ c = strchr(optarg, '=');
+
+ if (c)
+ *c++ = 0;
+ else
+ c = optarg;
+
+ envfile = fmemopen(c, strlen(c), "rb");
/* fallthrough */
case 'E':
if (!envfile) {
- if (!strcmp(optarg, "-"))
+ c = strchr(optarg, '=');
+
+ if (c)
+ *c++ = 0;
+ else
+ c = optarg;
+
+ if (!strcmp(c, "-"))
envfile = read_stdin(&stdin);
else
- envfile = fopen(optarg, "rb");
+ envfile = fopen(c, "rb");
if (!envfile) {
- fprintf(stderr, "Failed to open %s: %s\n", optarg, strerror(errno));
+ fprintf(stderr, "Failed to open %s: %s\n", c, strerror(errno));
rv = 1;
goto out;
}
@@ -358,8 +372,16 @@ main(int argc, char **argv)
env = env ? env : xjs_new_object();
+ if (c > optarg && optarg[0]) {
+ p = xjs_new_object();
+ json_object_object_add(env, optarg, p);
+ }
+ else {
+ p = env;
+ }
+
json_object_object_foreach(o, key, val)
- json_object_object_add(env, key, json_object_get(val));
+ json_object_object_add(p, key, json_object_get(val));
json_object_put(o);