brillo_update_payload: Improve help message and flags.

Different sub-commands have different flags. This patch improves the
help message of each sub-command including only the flag used by that
sub-command.

BUG=None
TEST=`brillo_update_payload command --help` for the different commands.

Change-Id: I2a1d5c0f0a7cb04bf6d0f4495de83fb66e134c1b
Reviewed-on: https://chromium-review.googlesource.com/302625
Commit-Ready: Alex Deymo <deymo@chromium.org>
Tested-by: Alex Deymo <deymo@chromium.org>
Reviewed-by: Jason Kusuma <jkusuma@chromium.org>
diff --git a/host/brillo_update_payload b/host/brillo_update_payload
index 68fb13a..911bfae 100755
--- a/host/brillo_update_payload
+++ b/host/brillo_update_payload
@@ -64,44 +64,83 @@
 find_common_sh
 . "${SCRIPT_ROOT}/common.sh" || exit 1
 
-# Check that a command is specified
+HELP_GENERATE="generate: Generate an unsigned update payload."
+HELP_HASH="hash: Generate the hashes of the unsigned payload and metadata used \
+for signing."
+HELP_SIGN="sign: Insert the signatures into the unsigned payload."
+
+usage() {
+  echo "Supported commands:"
+  echo
+  echo "${HELP_GENERATE}"
+  echo "${HELP_HASH}"
+  echo "${HELP_SIGN}"
+  echo
+  echo "Use: \"$0 <command> --help\" for more options."
+}
+
+# Check that a command is specified.
 if [[ $# -lt 1 ]]; then
   echo "Please specify a command [generate|hash|sign]"
   exit 1
 fi
 
-# Parse command
-case "$1" in
-  generate|hash|sign)
-    COMMAND=$1
+# Parse command.
+COMMAND="${1:-}"
+shift
+
+case "${COMMAND}" in
+  generate)
+    FLAGS_HELP="${HELP_GENERATE}"
+    ;;
+
+  hash)
+    FLAGS_HELP="${HELP_HASH}"
+    ;;
+
+  sign)
+    FLAGS_HELP="${HELP_SIGN}"
     ;;
   *)
-    echo "Unrecognized command:" $1
+    echo "Unrecognized command: \"${COMMAND}\"" >&2
+    usage >&2
     exit 1
     ;;
 esac
 
-shift
-
 # Flags
-DEFINE_string payload "" "Path to output the generated payload file."
-DEFINE_string target_image "" \
-  "Path to the target image that should be sent to clients."
-DEFINE_string source_image "" \
-  "Optional: Path to a source image. If specified, this makes\
- a delta update."
-DEFINE_string unsigned_payload "" "Path to the generated unsigned payload."
-DEFINE_string signature_size "" \
-  "Signature sizes in bytes in the following format: size1:size2[:...]"
-DEFINE_string payload_hash_file "" "Optional: Path to output payload hash file."
-DEFINE_string metadata_hash_file "" \
-  "Optional: Path to output metadata hash file."
-DEFINE_string payload_signature_file "" \
-  "The payload signatures in the following format:\
- payload_signature1:payload_signature2[:...]"
-DEFINE_string metadata_signature_file "" \
-  "The metatada signatures in the following format:\
- metadata_signature1:metadata_signature2[:...]"
+FLAGS_HELP="Usage: $0 ${COMMAND} [flags]
+${FLAGS_HELP}"
+
+if [[ "${COMMAND}" == "generate" ]]; then
+  DEFINE_string payload "" \
+    "Path to output the generated unsigned payload file."
+  DEFINE_string target_image "" \
+    "Path to the target image that should be sent to clients."
+  DEFINE_string source_image "" \
+    "Optional: Path to a source image. If specified, this makes a delta update."
+fi
+if [[ "${COMMAND}" == "hash" || "${COMMAND}" == "sign" ]]; then
+  DEFINE_string unsigned_payload "" "Path to the input unsigned payload."
+  DEFINE_string signature_size "" \
+    "Signature sizes in bytes in the following format: size1:size2[:...]"
+fi
+if [[ "${COMMAND}" == "hash" ]]; then
+  DEFINE_string metadata_hash_file "" \
+    "Optional: Path to output metadata hash file."
+  DEFINE_string payload_hash_file "" \
+    "Optional: Path to output payload hash file."
+fi
+if [[ "${COMMAND}" == "sign" ]]; then
+  DEFINE_string payload "" \
+    "Path to output the generated unsigned payload file."
+  DEFINE_string metadata_signature_file "" \
+    "The metatada signatures in the following format: \
+metadata_signature1:metadata_signature2[:...]"
+  DEFINE_string payload_signature_file "" \
+    "The payload signatures in the following format: \
+payload_signature1:payload_signature2[:...]"
+fi
 DEFINE_string work_dir "/tmp" "Where to dump temporary files."
 
 # Parse command line flag arguments