Merge "logcat: Add -h and --help flags"
am: 23e308398f

Change-Id: Icffb5b13a46f9fe3e1903cf1255b7218948c2db8
diff --git a/logcat/logcat.cpp b/logcat/logcat.cpp
index 8134936..64d1d2f 100644
--- a/logcat/logcat.cpp
+++ b/logcat/logcat.cpp
@@ -882,6 +882,7 @@
           { "grep",          required_argument, nullptr, 'e' },
           // hidden and undocumented reserved alias for --max-count
           { "head",          required_argument, nullptr, 'm' },
+          { "help",          no_argument,       nullptr, 'h' },
           { id_str,          required_argument, nullptr, 0 },
           { "last",          no_argument,       nullptr, 'L' },
           { "max-count",     required_argument, nullptr, 'm' },
@@ -900,9 +901,8 @@
         };
         // clang-format on
 
-        ret = getopt_long_r(argc, argv,
-                            ":cdDLt:T:gG:sQf:r:n:v:b:BSpP:m:e:", long_options,
-                            &option_index, &optctx);
+        ret = getopt_long_r(argc, argv, ":cdDhLt:T:gG:sQf:r:n:v:b:BSpP:m:e:",
+                            long_options, &option_index, &optctx);
         if (ret < 0) break;
 
         switch (ret) {
@@ -1304,6 +1304,11 @@
                              "Option -%c needs an argument\n", optctx.optopt);
                 goto exit;
 
+            case 'h':
+                show_help(context);
+                show_format_help(context);
+                goto exit;
+
             default:
                 logcat_panic(context, HELP_TRUE, "Unrecognized Option %c\n",
                              optctx.optopt);
diff --git a/logcat/tests/logcat_test.cpp b/logcat/tests/logcat_test.cpp
index a3a0176..d802b26 100644
--- a/logcat/tests/logcat_test.cpp
+++ b/logcat/tests/logcat_test.cpp
@@ -1668,3 +1668,22 @@
     EXPECT_TRUE(reportedSecurity("logcat -b security -c 2>&1"));
     EXPECT_TRUE(reportedSecurity("logcat -b security -G 256K 2>&1"));
 }
+
+static size_t commandOutputSize(const char* command) {
+    logcat_define(ctx);
+    FILE* fp = logcat_popen(ctx, command);
+    if (!fp) return 0;
+
+    std::string ret;
+    if (!android::base::ReadFdToString(fileno(fp), &ret)) return 0;
+    if (logcat_pclose(ctx, fp) != 0) return 0;
+
+    return ret.size();
+}
+
+TEST(logcat, help) {
+    size_t logcatHelpTextSize = commandOutputSize("logcat -h 2>&1");
+    EXPECT_LT(4096UL, logcatHelpTextSize);
+    size_t logcatLastHelpTextSize = commandOutputSize("logcat -L -h 2>&1");
+    EXPECT_EQ(logcatHelpTextSize, logcatLastHelpTextSize);
+}