[tor-commits] [stem/master] Balk if an unrecognized argument is provided

atagar at torproject.org atagar at torproject.org
Mon Feb 9 16:30:24 UTC 2015


commit 7753976dc7477a86fa8bc923292af3a09da7d660
Author: Damian Johnson <atagar at torproject.org>
Date:   Mon Feb 9 08:18:22 2015 -0800

    Balk if an unrecognized argument is provided
    
    Interesting, thought getopt did this. For both run_tests.py and the tor-prompt
    providing an error when unrecognized arguments are provided. Caught by
    Sebastian on...
    
      https://trac.torproject.org/projects/tor/ticket/14804
---
 run_tests.py                  |   11 ++++++++++-
 stem/interpreter/arguments.py |    8 ++++++--
 2 files changed, 16 insertions(+), 3 deletions(-)

diff --git a/run_tests.py b/run_tests.py
index dbead2d..63102a4 100755
--- a/run_tests.py
+++ b/run_tests.py
@@ -343,7 +343,16 @@ def _get_args(argv):
 
   args = dict(ARGS)
 
-  for opt, arg in getopt.getopt(argv, OPT, OPT_EXPANDED)[0]:
+  try:
+    recognized_args, unrecognized_args = getopt.getopt(argv, OPT, OPT_EXPANDED)
+
+    if unrecognized_args:
+      error_msg = "aren't recognized arguments" if len(unrecognized_args) > 1 else "isn't a recognized argument"
+      raise ValueError("'%s' %s" % ("', '".join(unrecognized_args), error_msg))
+  except getopt.GetoptError as exc:
+    raise ValueError('%s (for usage provide --help)' % exc)
+
+  for opt, arg in recognized_args:
     if opt in ('-a', '--all'):
       args['run_unit'] = True
       args['run_integ'] = True
diff --git a/stem/interpreter/arguments.py b/stem/interpreter/arguments.py
index d62a386..acf6d42 100644
--- a/stem/interpreter/arguments.py
+++ b/stem/interpreter/arguments.py
@@ -45,11 +45,15 @@ def parse(argv):
   args = dict(DEFAULT_ARGS)
 
   try:
-    getopt_results = getopt.getopt(argv, OPT, OPT_EXPANDED)[0]
+    recognized_args, unrecognized_args = getopt.getopt(argv, OPT, OPT_EXPANDED)
+
+    if unrecognized_args:
+      error_msg = "aren't recognized arguments" if len(unrecognized_args) > 1 else "isn't a recognized argument"
+      raise ValueError("'%s' %s" % ("', '".join(unrecognized_args), error_msg))
   except getopt.GetoptError as exc:
     raise ValueError('%s (for usage provide --help)' % exc)
 
-  for opt, arg in getopt_results:
+  for opt, arg in recognized_args:
     if opt in ('-i', '--interface'):
       if ':' in arg:
         address, port = arg.split(':', 1)





More information about the tor-commits mailing list