diff cutils/treesum.py @ 147:ed35f3c9e2b5

Add also a "help" subcommand to "treesum". It can show the global general help or the help message for a given subcommand.
author Franz Glasner <fzglas.hg@dom66.de>
date Fri, 03 Jan 2025 16:51:26 +0100
parents 7d8df8311e3b
children 17d6e760143f
line wrap: on
line diff
--- a/cutils/treesum.py	Fri Jan 03 16:32:40 2025 +0100
+++ b/cutils/treesum.py	Fri Jan 03 16:51:26 2025 +0100
@@ -119,18 +119,39 @@
                     "This is an alias to \"generate\".")
     _populate_generate_arguments(genparser2)
 
-    subparsers.add_parser(
+    hparser = subparsers.add_parser(
+        "help",
+        help="Show this help message or a subcommand's help and exit",
+        description="Show this help message or a subcommand's help and exit.")
+    hparser.add_argument("help_command", nargs='?', metavar="COMMAND")
+
+    vparser = subparsers.add_parser(
         "version",
         help="Show the program's version number and exit",
         description="Show the program's version number and exit.")
 
-    # Parse leniently to just check for "version"
+    # Parse leniently to just check for "version" and/or help
     opts, _dummy = parser.parse_known_args(args=argv)
 
     if opts.subcommand == "version":
         print("%s (rv:%s)" % (__version__, __revision__),
               file=sys.stdout)
         sys.exit(0)
+    if opts.subcommand == "help":
+        if not opts.help_command:
+            parser.print_help()
+        else:
+            if opts.help_command == "generate":
+                genparser.print_help()
+            elif opts.help_command == "gen":
+                genparser2.print_help()
+            elif opts.help_command == "version":
+                vparser.print_help()
+            elif opts.help_command == "help":
+                hparser.print_help()
+            else:
+                parser.print_help()
+        sys.exit(0)
 
     # Reparse strictly
     opts = parser.parse_args(args=argv)