From b048953c1ab439f9888855766c39eba33c569b2c Mon Sep 17 00:00:00 2001 From: Eric Fischer Date: Wed, 16 May 2018 16:31:23 -0700 Subject: [PATCH] Also support JSON options as a command-line literal --- README.md | 12 ++++++++++-- main.cpp | 35 ++++++++++++++++++++++++++--------- man/tippecanoe.1 | 15 ++++++++++++++- 3 files changed, 50 insertions(+), 12 deletions(-) diff --git a/README.md b/README.md index 4a1715c..b9c77cb 100644 --- a/README.md +++ b/README.md @@ -314,9 +314,17 @@ Example: to retain only major TIGER roads at low zoom levels: ### Meta-options + * `-h` _options_ or `--options=`_options_: Process additional options from the specified literal JSON object. * `-H` _config.json_ or `--options-from-file=`_config.json_: Read additional options from the specified _config.json_ file. - The file should contain one JSON object. Its keys must be long or short option names. The value corresponding - to each key should be a string or number, for options with arguments, or `null` or `true` for options that do not take arguments. + The file should contain one JSON object. + +Either with directly-specified options or options from a file, the options should be a single JSON object. +Its keys must be long or short option names. The value corresponding +to each key should be a string or number, for options with arguments, or `null` or `true` for options that do not take arguments. + +Example: + + tippecanoe -h '{ "maximum-zoom": "g", "force": true, "output": "countries.mbtiles" }' ne_10m_admin_0_countries.json ### Filters diff --git a/main.cpp b/main.cpp index db2d961..0cd71e7 100644 --- a/main.cpp +++ b/main.cpp @@ -2378,22 +2378,34 @@ struct flag { std::string arg; }; -void read_flags(std::vector &flags, const char *fname) { - FILE *fp = fopen(fname, "r"); - if (fp == NULL) { - perror(fname); - exit(EXIT_FAILURE); +void read_flags(std::vector &flags, const char *fname, bool direct) { + json_pull *jp; + json_object *obj; + FILE *fp = NULL; + + if (direct) { + jp = json_begin_string(fname); + } else { + fp = fopen(fname, "r"); + if (fp == NULL) { + perror(fname); + exit(EXIT_FAILURE); + } + + jp = json_begin_file(fp); } - json_pull *jp = json_begin_file(fp); - json_object *obj = json_read_tree(jp); + obj = json_read_tree(jp); if (obj == NULL) { fprintf(stderr, "%s: %s\n", fname, jp->error); exit(EXIT_FAILURE); } json_disconnect(obj); json_end(jp); - fclose(fp); + + if (fp != NULL) { + fclose(fp); + } if (obj->type != JSON_HASH) { fprintf(stderr, "%s: %s: contents are not a JSON object\n", *av, fname); @@ -2594,6 +2606,7 @@ int main(int argc, char **argv) { {"version", no_argument, 0, 'v'}, {"Meta-options", 0, 0, 0}, + {"options", required_argument, 0, 'h'}, {"options-from-file", required_argument, 0, 'H'}, {"", 0, 0, 0}, @@ -2698,8 +2711,12 @@ int main(int argc, char **argv) { case 0: break; + case 'h': + read_flags(flags, optarg, true); + break; + case 'H': - read_flags(flags, optarg); + read_flags(flags, optarg, false); break; case 'I': { diff --git a/man/tippecanoe.1 b/man/tippecanoe.1 index a061cd6..ff09252 100644 --- a/man/tippecanoe.1 +++ b/man/tippecanoe.1 @@ -389,9 +389,22 @@ If you don't specify, it will use \fB\fC/tmp\fR\&. .SS Meta\-options .RS .IP \(bu 2 +\fB\fC\-h\fR \fIoptions\fP or \fB\fC\-\-options=\fR\fIoptions\fP: Process additional options from the specified literal JSON object. +.IP \(bu 2 \fB\fC\-H\fR \fIconfig.json\fP or \fB\fC\-\-options\-from\-file=\fR\fIconfig.json\fP: Read additional options from the specified \fIconfig.json\fP file. -The file should contain one JSON object. Its keys must be long or short option names. The value corresponding +The file should contain one JSON object. +.RE +.PP +Either with directly\-specified options or options from a file, the options should be a single JSON object. +Its keys must be long or short option names. The value corresponding to each key should be a string or number, for options with arguments, or \fB\fCnull\fR or \fB\fCtrue\fR for options that do not take arguments. +.PP +Example: +.PP +.RS +.nf +tippecanoe \-h '{ "maximum\-zoom": "g", "force": true, "output": "countries.mbtiles" }' ne_10m_admin_0_countries.json +.fi .RE .SS Filters .RS