if len(sys.argv) == 1 or sys.argv[1] in ('--help', '-h'):
print cmdutil.help()
-elif sys.argv[1] == '--commands':
+elif sys.argv[1] == '--complete':
for command, module in cmdutil.iter_commands():
print command
-elif sys.argv[1] == '--options':
- print '\n'.join(cmdutil.options())
+ print '\n'.join(["--help","--complete","--options"])
else:
try:
try:
for option in [o.dest for o in parser.option_list if o.dest != None]:
value = getattr(options, option)
- if value == "--options":
+ if value == "--complete":
if option == "status":
raise cmdutil.GetCompletions(status_values)
raise cmdutil.GetCompletions()
- if "--options" in args:
+ if "--complete" in args:
raise cmdutil.GetCompletions() # no completions for arguments yet
if len(args) > 0:
# "An introduction to bash completion: part 2"
# http://www.debian-administration.org/articles/317
-# Support commands of the form:
-# be <command> [<long option>] [<long option>] ...
# Requires:
-# be --commands
-# to print a list of available commands
-# be command [<option> ...] --options
-# to print a list of available long options
+# be [X Y Z] --complete
+# to print a list of available completions at that point
_be()
{
local cur prev opts
if [ $COMP_CWORD -eq 1 ]; then
# no command yet, show all commands
- COMPREPLY=( $( compgen -W "$(be --commands)" -- $cur ) )
+ COMPREPLY=( $( compgen -W "$(be --complete)" -- $cur ) )
else
# remove the first word (should be "be") for security reasons
unset COMP_WORDS[0]
for i in `seq $COMP_CWORD ${#COMP_WORDS[@]}`; do
unset COMP_WORDS[$i];
done
- COMPREPLY=( $( compgen -W "$(be "${COMP_WORDS[@]}" --options)" -- $cur ) )
+ COMPREPLY=( $( compgen -W "$(be "${COMP_WORDS[@]}" --complete)" -- $cur ) )
fi
}
ret.append("be %s%*s %s" % (name, numExtraSpaces, "", desc))
return "\n".join(ret)
-def options(cmd=None):
- if cmd != None:
- parser = get_command(cmd).get_parser()
- longopts = []
- for opt in parser.option_list:
- longopts.append(opt.get_opt_string())
- return longopts
- else:
- # These probably shouldn't be hardcoded...
- return ["--help","--commands","--options"]
+def options(cmd):
+ parser = get_command(cmd).get_parser()
+ longopts = []
+ for opt in parser.option_list:
+ longopts.append(opt.get_opt_string())
+ return longopts
def raise_get_help(option, opt, value, parser):
raise GetHelp
self.remove_option("-h")
self.add_option("-h", "--help", action="callback",
callback=raise_get_help, help="Print a help message")
- self.add_option("--options", action="callback",
+ self.add_option("--complete", action="callback",
callback=raise_get_completions,
- help="Print a list of available long options")
+ help="Print a list of available completions")
def error(self, message):
raise UsageError(message)