fix behavior when adding --help option 0.9.1
authorAlexander Solovyov <piranha@piranha.org.ua>
Sat, 18 Jul 2009 00:09:50 +0300
changeset 46 188325c309d8
parent 45 5ee1ba53050e
child 47 631f37c1f906
fix behavior when adding --help option
finaloption.py
--- a/finaloption.py	Sat Jul 18 00:03:35 2009 +0300
+++ b/finaloption.py	Sat Jul 18 00:09:50 2009 +0300
@@ -6,7 +6,7 @@
 from itertools import imap
 
 __all__ = ['command', 'dispatch']
-__version__ = '0.9'
+__version__ = '0.9.1'
 __author__ = 'Alexander Solovyov'
 __email__ = 'piranha@piranha.org.ua'
 
@@ -35,8 +35,8 @@
        only for multiple subcommands
     '''
     def wrapper(func):
-        options_ = options or list(guess_options(func))
-        options_.append(('h', 'help', False, 'show help'))
+        # copy option list
+        options_ = list(options or guess_options(func))
 
         name_ = name or func.__name__
         CMDTABLE[(shortlist and '^' or '') + name_] = (
@@ -49,6 +49,11 @@
             return help_cmd(func, replace_name(usage, name_), options_)
 
         def inner(args=None):
+            try:
+                (True for option in reversed(options_)
+                 if option[1] == 'help').next()
+            except StopIteration:
+                options_.append(('h', 'help', False, 'show help'))
 
             args = args or sys.argv[1:]
             if not args:
@@ -151,7 +156,8 @@
             return helplist()
 
         aliases, (cmd, options, usage) = findcmd(name, cmdtable)
-        return help_cmd(cmd, replace_name(usage, aliases[0]), options)
+        return help_cmd(cmd, replace_name(usage, aliases[0]),
+                        options + globalopts)
     return inner
 
 def help_cmd(func, usage, options):