summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorKarl Palsson <karlp@remake.is>2016-02-03 17:11:57 +0000
committerJo-Philipp Wich <jow@openwrt.org>2016-02-03 19:16:34 +0100
commit479ca5def5b6bd5c79de488806299064346e2339 (patch)
tree86e71e1f99223f97a92e54fd1998d6a3a8a433ec
parent0c5f596da75c58a1fd0999133e18291a680dd040 (diff)
downloadjsonpath-479ca5def5b6bd5c79de488806299064346e2339.tar.gz
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 <karlp@remake.is> Signed-off-by: Jo-Philipp Wich <jow@openwrt.org>
-rw-r--r--main.c48
1 files changed, 47 insertions, 1 deletions
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 <file> | -s \"json...\"] {-t <pattern> | -e <pattern>}\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 <pattern> Print the type of values matched by pattern\n"
+ " -e <pattern> Print the values matched by pattern\n"
+ " -e VAR=<pat> 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");