From 479ca5def5b6bd5c79de488806299064346e2339 Mon Sep 17 00:00:00 2001 From: Karl Palsson Date: Wed, 3 Feb 2016 17:11:57 +0000 Subject: [PATCH] main: Add basic help output Add basic usage text to the binary and print it when invoked with -h or with no arguments at all. Signed-off-by: Karl Palsson Signed-off-by: Jo-Philipp Wich --- main.c | 48 +++++++++++++++++++++++++++++++++++++++++++++++- 1 file changed, 47 insertions(+), 1 deletion(-) diff --git a/main.c b/main.c index e4902d9..960b003 100644 --- a/main.c +++ b/main.c @@ -37,6 +37,42 @@ struct match_item { struct list_head list; }; +static void +print_usage(char *app) +{ + printf( + "== Usage ==\n\n" + " # %s [-i | -s \"json...\"] {-t | -e }\n" + " -q Quiet, no errors are printed\n" + " -h, --help Print this help\n" + " -i path Specify a JSON file to parse\n" + " -s \"json\" Specify a JSON string to parse\n" + " -l limit Specify max number of results to show\n" + " -F separator Specify a field separator when using export\n" + " -t Print the type of values matched by pattern\n" + " -e Print the values matched by pattern\n" + " -e VAR= Serialize matched value for shell \"eval\"\n\n" + "== Patterns ==\n\n" + " Patterns are JsonPath: http://goessner.net/articles/JsonPath/\n" + " This tool implements $, @, [], * and the union operator ','\n" + " plus the usual expressions and literals.\n" + " It does not support the recursive child search operator '..' or\n" + " the '?()' and '()' filter expressions as those would require a\n" + " complete JavaScript engine to support them.\n\n" + "== Examples ==\n\n" + " Display the first IPv4 address on lan:\n" + " # ifstatus lan | %s -e '@[\"ipv4-address\"][0].address'\n\n" + " Extract the release string from the board information:\n" + " # ubus call system board | %s -e '@.release.description'\n\n" + " Find all interfaces which are up:\n" + " # ubus call network.interface dump | \\\n" + " %s -e '@.interface[@.up=true].interface'\n\n" + " Export br-lan traffic counters for shell eval:\n" + " # devstatus br-lan | %s -e 'RX=@.statistics.rx_bytes' \\\n" + " -e 'TX=@.statistics.tx_bytes'\n", + app, app, app, app, app); +} + static struct json_object * parse_json(FILE *fd, const char *source, const char **error) { @@ -382,10 +418,20 @@ int main(int argc, char **argv) struct json_object *jsobj = NULL; const char *jserr = NULL, *source = NULL, *separator = " "; - while ((opt = getopt(argc, argv, "i:s:e:t:F:l:q")) != -1) + if (argc == 1) + { + print_usage(argv[0]); + goto out; + } + + while ((opt = getopt(argc, argv, "hi:s:e:t:F:l:q")) != -1) { switch (opt) { + case 'h': + print_usage(argv[0]); + goto out; + case 'i': input = fopen(optarg, "r"); -- 2.30.2