main: Add basic help output
[project/jsonpath.git] / main.c
diff --git a/main.c b/main.c
index e4902d9c9a11744bd993be9908bb28ef9f0b1108..960b003c15d7b944c2ddc89ee64df618098124c5 100644 (file)
--- 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");