changeset 41025:3c5aaea9638f

tests: update printenv.py argument parsing We are about to introduce a new flag for printing the HG environment variables one per line and it's easier to do when using the argparse module for argument parsing. Differential Revision: https://phab.mercurial-scm.org/D5452
author Boris Feld <boris.feld@octobus.net>
date Wed, 19 Dec 2018 15:42:20 +0100
parents 6a951f535fee
children c236a491ab7b
files tests/printenv.py
diffstat 1 files changed, 21 insertions(+), 10 deletions(-) [+]
line wrap: on
line diff
--- a/tests/printenv.py	Thu Dec 20 01:22:58 2018 -0500
+++ b/tests/printenv.py	Wed Dec 19 15:42:20 2018 +0100
@@ -13,6 +13,7 @@
 #              the file will be opened in append mode.
 #
 from __future__ import absolute_import
+import argparse
 import os
 import sys
 
@@ -24,15 +25,25 @@
 except ImportError:
     pass
 
-exitcode = 0
-out = sys.stdout
-out = getattr(out, 'buffer', out)
+parser = argparse.ArgumentParser()
+parser.add_argument("name", help="the hook name, used for display")
+parser.add_argument(
+    "exitcode",
+    nargs="?",
+    default=0,
+    type=int,
+    help="the exit code for the hook",
+)
+parser.add_argument(
+    "out", nargs="?", default=None, help="where to write the output"
+)
+args = parser.parse_args()
 
-name = sys.argv[1]
-if len(sys.argv) > 2:
-    exitcode = int(sys.argv[2])
-    if len(sys.argv) > 3:
-        out = open(sys.argv[3], "ab")
+if args.out is None:
+    out = sys.stdout
+    out = getattr(out, "buffer", out)
+else:
+    out = open(args.out, "ab")
 
 # variables with empty values may not exist on all platforms, filter
 # them now for portability sake.
@@ -40,7 +51,7 @@
        if k.startswith("HG_") and v]
 env.sort()
 
-out.write(b"%s hook: " % name.encode('ascii'))
+out.write(b"%s hook: " % args.name.encode('ascii'))
 if os.name == 'nt':
     filter = lambda x: x.replace('\\', '/')
 else:
@@ -51,4 +62,4 @@
 out.write(b"\n")
 out.close()
 
-sys.exit(exitcode)
+sys.exit(args.exitcode)