Patchwork D5452: tests: update printenv.py argument parsing

login
register
mail settings
Submitter phabricator
Date Dec. 20, 2018, 1:14 p.m.
Message ID <1fe267e0a6a43944dacbea178e4e2b35@localhost.localdomain>
Download mbox | patch
Permalink /patch/37277/
State Not Applicable
Headers show

Comments

phabricator - Dec. 20, 2018, 1:14 p.m.
This revision was automatically updated to reflect the committed changes.
Closed by commit rHG07b15f64e560: tests: update printenv.py argument parsing (authored by lothiraldan, committed by ).

REPOSITORY
  rHG Mercurial

CHANGES SINCE LAST UPDATE
  https://phab.mercurial-scm.org/D5452?vs=12899&id=12927

REVISION DETAIL
  https://phab.mercurial-scm.org/D5452

AFFECTED FILES
  tests/printenv.py

CHANGE DETAILS




To: lothiraldan, #hg-reviewers
Cc: mercurial-devel

Patch

diff --git a/tests/printenv.py b/tests/printenv.py
--- a/tests/printenv.py
+++ b/tests/printenv.py
@@ -13,6 +13,7 @@ 
 #              the file will be opened in append mode.
 #
 from __future__ import absolute_import
+import argparse
 import os
 import sys
 
@@ -24,23 +25,33 @@ 
 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.
 env = [(k, v) for k, v in os.environ.items()
        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)