Also support JSON options as a command-line literal

This commit is contained in:
Eric Fischer 2018-05-16 16:31:23 -07:00
parent fbb3af3612
commit b048953c1a
3 changed files with 50 additions and 12 deletions

View File

@ -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

View File

@ -2378,22 +2378,34 @@ struct flag {
std::string arg;
};
void read_flags(std::vector<flag> &flags, const char *fname) {
FILE *fp = fopen(fname, "r");
if (fp == NULL) {
perror(fname);
exit(EXIT_FAILURE);
void read_flags(std::vector<flag> &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': {

View File

@ -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