Merged upstream
authorDmitriy Morozov <dmitriy@mrzv.org>
Tue, 14 Sep 2010 14:35:54 -0700
changeset 117 e17bf1d5c289
parent 108 8befe33c55b1 (current diff)
parent 116 48cda0ae3913 (diff)
child 122 76334dcc9ae4
Merged upstream
opster.py
--- a/.hgtags	Wed Sep 08 17:39:56 2010 -0700
+++ b/.hgtags	Tue Sep 14 14:35:54 2010 -0700
@@ -8,3 +8,4 @@
 d4bdbbf7a50098e61fb4f10e6841c098c22bc293 0.9.7
 cc24c392fd45ee296ea1535b8f49aa3a42df3b05 0.9.8
 472a33d66076d746d6c2dedd80fc7b40ac18e5f9 0.9.9
+95653a562a05f97e1a969d916d805bc6cf8a8b9b 0.9.10
--- a/docs/changelog.rst	Wed Sep 08 17:39:56 2010 -0700
+++ b/docs/changelog.rst	Tue Sep 14 14:35:54 2010 -0700
@@ -4,12 +4,11 @@
 0.9.10
 ~~~~~~
 
- - **backward incompatible change**: if you are calling function to parse
-   command line, earlier you could write ``main()`` to accomplish that
-   task. This syntax is unavailable now (this will try to actually call a
-   function), and you should use ``main(argv=sys.argv[1:])``.
  - if default value of an option is a fuction, always call it (None is passed in
    case when option is not supplied)
+ - always call a function if it's default argument for an option
+ - some cleanup with better support for python 3
+ - initial support for autocompletion (borrowed from PIP)
 
 0.9.9
 ~~~~~
--- a/docs/overview.rst	Wed Sep 08 17:39:56 2010 -0700
+++ b/docs/overview.rst	Tue Sep 14 14:35:54 2010 -0700
@@ -58,7 +58,7 @@
 After that you can simply call this function as an entry point to your program::
 
   if __name__ == '__main__':
-      main(argv=sys.argv[1:])
+      main()
 
 This will run command line parsing facility, using arguments from
 ``sys.argv``. ``%name`` in usage string will be replaced with ``sys.argv[0]``
--- a/opster.py	Wed Sep 08 17:39:56 2010 -0700
+++ b/opster.py	Tue Sep 14 14:35:54 2010 -0700
@@ -6,7 +6,7 @@
 from itertools import imap
 
 __all__ = ['command', 'dispatch']
-__version__ = '0.9.9'
+__version__ = '0.9.10'
 __author__ = 'Alexander Solovyov'
 __email__ = 'piranha@piranha.org.ua'
 
@@ -66,7 +66,7 @@
             except StopIteration:
                 options_.append(('h', 'help', False, 'show help'))
 
-            argv = opts.pop('argv', None)
+            argv = opts.pop('argv', sys.argv[1:])
             if opts.pop('help', False):
                 return help_func()
 
@@ -123,10 +123,6 @@
     cmdtable['help'] = (help_(cmdtable, globaloptions), [], '[TOPIC]')
     help_func = cmdtable['help'][0]
 
-    cmdtable['_completion'] = (completion_,
-                              [('b', 'bash', False, 'Output bash competion'), 
-                               ('z', 'zsh',  False, 'Output zsh completion')], 
-                              '--bash OR --zsh')
     autocomplete(cmdtable, args, middleware)
 
     try:
@@ -323,109 +319,18 @@
         if t is types.FunctionType:
             del funlist[funlist.index(name)]
             state[name] = defmap[name](val)
-        elif t is types.IntType:
-            state[name] = int(val)
-        elif t is types.FloatType:
-            state[name] = float(val)
-        elif t is types.StringType:
-            state[name] = val
         elif t is types.ListType:
             state[name].append(val)
         elif t in (types.NoneType, types.BooleanType):
             state[name] = not defmap[name]
+        else:
+            state[name] = t(val)
 
     for name in funlist:
         state[name] = defmap[name](None)
 
     return state, args
 
-# --------
-# Autocomplete system
-# --------
-
-# Borrowed from PIP
-def autocomplete(cmdtable, args, middleware):
-    """Command and option completion.
-
-    Enable by sourcing one of the completion shell scripts (bash or zsh).
-    """
-    
-    # Don't complete if user hasn't sourced bash_completion file.
-    if not os.environ.has_key('OPSTER_AUTO_COMPLETE'):
-        return
-    cwords = os.environ['COMP_WORDS'].split()[1:]
-    cword = int(os.environ['COMP_CWORD'])
-
-    try:
-        current = cwords[cword-1]
-    except IndexError:
-        current = ''
-
-    commands = []
-    for k in cmdtable.keys():
-        commands += aliases_(k)
-
-    # command
-    if cword == 1:
-        print ' '.join(filter(lambda x: x.startswith(current), commands))
-    
-    # command options
-    else:
-        try:
-            aliases, (cmd, opts, usage) = findcmd(cwords[0], cmdtable)
-        except AmbiguousCommand:
-            sys.exit(1) 
-
-        idx = -2 if current else -1
-        options = []
-
-        for o in opts:
-            short, long, default, help = o[:4]
-            completer = o[4] if len(o) > 4 else None
-            short, long = '-%s' % short, '--%s' % long
-            options += [short,long]
-
-            if (cwords[idx] in [short,long]) and completer:
-                args = middleware(completer)(current)
-                print ' '.join(args),
-        
-        print ' '.join((o for o in options if o.startswith(current)))
-    
-    sys.exit(1)
-
-def completion_(**opts):
-    """Outputs the completion script for bash or zsh."""
-
-    (head, prog_name) = os.path.split(sys.argv[0])
-
-    if opts['bash']:
-        print """
-# opster bash completion start
-_opster_completion()
-{
-    COMPREPLY=( $( COMP_WORDS="${COMP_WORDS[*]}" \\
-                   COMP_CWORD=$COMP_CWORD \\
-                   OPSTER_AUTO_COMPLETE=1 $1 ) )
-    COMP_WORDBREAKS=${COMP_WORDBREAKS//:}
-}
-complete -o default -F _opster_completion %s
-# opster bash completion end
-              """ % prog_name
-
-    if opts['zsh']:
-        print """
-# opster zsh completion start
-function _opster_completion {
-  local words cword
-  read -Ac words
-  read -cn cword
-  reply=( $( COMP_WORDS="$words[*]" \\ 
-             COMP_CWORD=$(( cword-1 )) \\
-             OPSTER_AUTO_COMPLETE=1 $words[1] ) )
-}
-compctl -K _opster_completion %s
-# opster zsh completion end
-              """ % prog_name
 
 
 # --------
@@ -615,6 +520,97 @@
         return inner
 
 # --------
+# Autocomplete system
+# --------
+
+# Borrowed from PIP
+def autocomplete(cmdtable, args, middleware):
+    """Command and option completion.
+
+    Enable by sourcing one of the completion shell scripts (bash or zsh).
+    """
+
+    # Don't complete if user hasn't sourced bash_completion file.
+    if not os.environ.has_key('OPSTER_AUTO_COMPLETE'):
+        return
+    cwords = os.environ['COMP_WORDS'].split()[1:]
+    cword = int(os.environ['COMP_CWORD'])
+
+    try:
+        current = cwords[cword-1]
+    except IndexError:
+        current = ''
+
+    commands = []
+    for k in cmdtable.keys():
+        commands += aliases_(k)
+
+    # command
+    if cword == 1:
+        print ' '.join(filter(lambda x: x.startswith(current), commands))
+
+    # command options
+    else:
+        try:
+            aliases, (cmd, opts, usage) = findcmd(cwords[0], cmdtable)
+        except AmbiguousCommand:
+            sys.exit(1) 
+
+        idx = -2 if current else -1
+        options = []
+
+        for o in opts:
+            short, long, default, help = o[:4]
+            completer = o[4] if len(o) > 4 else None
+            short, long = '-%s' % short, '--%s' % long
+            options += [short,long]
+
+            if cwords[idx] in (short, long) and completer:
+                args = middleware(completer)(current)
+                print ' '.join(args),
+
+        print ' '.join((o for o in options if o.startswith(current)))
+
+    sys.exit(1)
+
+COMPLETIONS = {
+    'bash':
+        """
+# opster bash completion start
+_opster_completion()
+{
+    COMPREPLY=( $( COMP_WORDS="${COMP_WORDS[*]}" \\
+                   COMP_CWORD=$COMP_CWORD \\
+                   OPSTER_AUTO_COMPLETE=1 $1 ) )
+    COMP_WORDBREAKS=${COMP_WORDBREAKS//:}
+}
+complete -o default -F _opster_completion %s
+# opster bash completion end
+""",
+    'zsh':
+            """
+# opster zsh completion start
+function _opster_completion {
+  local words cword
+  read -Ac words
+  read -cn cword
+  reply=( $( COMP_WORDS="$words[*]" \\
+             COMP_CWORD=$(( cword-1 )) \\
+             OPSTER_AUTO_COMPLETE=1 $words[1] ) )
+}
+compctl -K _opster_completion %s
+# opster zsh completion end
+"""
+    }
+
+@command(name='_completion', hide=True)
+def completion(type=('t', 'bash', 'Completion type (bash or zsh)')):
+    """Outputs completion script for bash or zsh."""
+
+    prog_name = os.path.split(sys.argv[0])[1]
+    print COMPLETIONS[type] % prog_name
+
+# --------
 # Exceptions
 # --------
 
--- a/tests/test2	Wed Sep 08 17:39:56 2010 -0700
+++ b/tests/test2	Tue Sep 14 14:35:54 2010 -0700
@@ -16,5 +16,5 @@
     a = 42
 
 if __name__ == '__main__':
-    main(argv=[])
+    main()
     assert a == 42, "WTF???"
--- a/tests/test_opts.py	Wed Sep 08 17:39:56 2010 -0700
+++ b/tests/test_opts.py	Tue Sep 14 14:35:54 2010 -0700
@@ -31,5 +31,5 @@
     print locals()
 
 if __name__ == '__main__':
-    #main(argv=sys.argv[1:])
-    another(argv=sys.argv[1:])
+    #main()
+    another()