Re: shell completion support for optparse/optik
Martin Blais <[email protected]> Wed, 17 Sep 2003 00:49:15 -0400
| Newsgroups | gmane.comp.python.optik.user |
|---|---|
| Organization | Furious Vibe |
| Message-ID | <[email protected]> |
-----BEGIN PGP SIGNED MESSAGE----- Hash: SHA1 i think more and more that the idea could work, it's too easy, here is a tentative little hack i just fiddled: 1. in a bash shell, enter the following: complete -C '/tmp/completion-test --optparse-complete ' completion-test this will invoke the program itself when completion is asked on it. 2. copy the simple test program in attachment in /tmp/completion-test 3. type /tmp/completion-test<TAB> now there only remains to have a look at the current point and the line, and to print on stdout the completions, one per line, using information from the given OptionParser. issues: - - how do we setup the completion instructions above without having to list each individual program that we want to be able to complete this way? if at least a simple list of them can be enumerated, i guess i could create a shell function to do this and then invoke it for every program, e.g. install_optparse_completion(completion-test) install_optparse_completion(other-program) ... - - in my example, it means that a user cannot add --optparse-complete to his own set of options; what name to assign, any ideas on another way to do this? - - should individual options have possibility for individual completion methods? e.g. parser.add_option(... complete=<method>) i think so, this would mean that program --option=<TAB> could complete with the "--option"'s specific requirements, how cool is that? - - i'd like to find a good set of default completion methods to add to the library, so that adding completion in the most common cases doesn't require you to declare a class; -----BEGIN PGP SIGNATURE----- iD8DBQE/Z+fPq2PmC9F3Xx0RAlgbAJ4yFefghKMHHakG32XzWGABBDUpKwCfZyqJ IegNGKl6ja/BH4jhnSE1/AI= =vAU3 -----END PGP SIGNATURE-----
completion-test
(text/plain, 1022 B)
#!/usr/bin/env python
# This functionality should be implemented within the parse_args()
# function.
def parse_args_complete():
# this should be hidden behaviour within the optparse module
import sys
if len(sys.argv) > 1 and sys.argv[1] == '--optparse-complete':
# FIXME todo: compute possible completions, using parser object
# note that you're getting the following variables as input
# in the environment:
# COMP_CWORD
# COMP_LINE
# COMP_POINT
# COMP_WORDS
# import os
# print >> sys.stderr, os.environ['COMP_LINE']
print 'completion1'
print 'completion2'
print 'completion3'
sys.exit(1)
else:
return parser.parse_args()
import os, optparse
parser = optparse.OptionParser()
parser.add_option('--bla', help="Bla optino.")
# ...
# Note: this should be calling the normal parse_args() method.
opts, args = parse_args_complete()
# ...
print 'Executing command'
print 'opts', opts
print 'args', args