merge completer improvements
authorAlexander Solovyov <piranha@piranha.org.ua>
Sat, 11 Sep 2010 10:39:15 +0300
changeset 116 48cda0ae3913
parent 115 b0d1129c76bc (diff)
parent 105 1ce5c137b3e5 (current diff)
child 117 e17bf1d5c289
child 118 51f4b89d6c8d
merge completer improvements
opster.py
--- a/.hgtags	Sat May 29 15:46:31 2010 -0700
+++ b/.hgtags	Sat Sep 11 10:39:15 2010 +0300
@@ -8,3 +8,4 @@
 d4bdbbf7a50098e61fb4f10e6841c098c22bc293 0.9.7
 cc24c392fd45ee296ea1535b8f49aa3a42df3b05 0.9.8
 472a33d66076d746d6c2dedd80fc7b40ac18e5f9 0.9.9
+95653a562a05f97e1a969d916d805bc6cf8a8b9b 0.9.10
--- a/docs/changelog.rst	Sat May 29 15:46:31 2010 -0700
+++ b/docs/changelog.rst	Sat Sep 11 10:39:15 2010 +0300
@@ -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	Sat May 29 15:46:31 2010 -0700
+++ b/docs/overview.rst	Sat Sep 11 10:39:15 2010 +0300
@@ -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	Sat May 29 15:46:31 2010 -0700
+++ b/opster.py	Sat Sep 11 10:39:15 2010 +0300
@@ -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:
@@ -242,7 +238,8 @@
 def help_options(options):
     yield 'options:\n\n'
     output = []
-    for short, name, default, desc in options:
+    for o in options:
+        short, name, default, desc = o[:4]
         if hasattr(default, '__call__'):
             default = default(None)
         default = default and ' (default: %s)' % default or ''
@@ -322,104 +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
-    elif cwords[0] in commands:
-        idx = -2 if current else -1
-        options = []
-        aliases, (cmd, opts, usage) = findcmd(cwords[0], cmdtable)
-
-        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 ) )
-}
-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
 
 
 # --------
@@ -609,6 +520,92 @@
         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
+    elif cwords[0] in commands:
+        idx = -2 if current else -1
+        options = []
+        aliases, (cmd, opts, usage) = findcmd(cwords[0], cmdtable)
+
+        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 ) )
+}
+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	Sat May 29 15:46:31 2010 -0700
+++ b/tests/test2	Sat Sep 11 10:39:15 2010 +0300
@@ -16,5 +16,5 @@
     a = 42
 
 if __name__ == '__main__':
-    main(argv=[])
+    main()
     assert a == 42, "WTF???"
--- a/tests/test_opts.py	Sat May 29 15:46:31 2010 -0700
+++ b/tests/test_opts.py	Sat Sep 11 10:39:15 2010 +0300
@@ -31,5 +31,5 @@
     print locals()
 
 if __name__ == '__main__':
-    #main(argv=sys.argv[1:])
-    another(argv=sys.argv[1:])
+    #main()
+    another()