Created
May 19, 2014 22:32
-
-
Save zeekay/46a85b3f4dce0c4e5d69 to your computer and use it in GitHub Desktop.
Flask-script completions for zsh.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#compdef managepy | |
typeset -A opt_args | |
_arguments -C \ | |
'1:cmds:->cmds' \ | |
'2:subcmds:->subcmds' \ | |
'*:: :->args' \ | |
&& ret=0 | |
case $state in | |
cmds) | |
local commands | |
commands=(${(f)"$(python manage.py complete)"}) | |
_describe -t commands 'command' commands && ret=0 | |
;; | |
subcmds) | |
local subcommands | |
subcommands=(${(f)"$(python manage.py complete $words[2])"}) | |
_describe -t subcommands 'subcommand' subcommands && ret=0 | |
;; | |
args) | |
_files | |
;; | |
esac | |
return 1 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from flask.ext.script import Manager | |
from myapp import create_app | |
manager = Manager(create_app) | |
@manager.command | |
def foo(): | |
print 'foo' | |
def complete(argv, manager): | |
''' | |
Return completions for zsh. | |
''' | |
if not argv[1:]: | |
return | |
if not argv[1] == 'complete': | |
return | |
# Get commands for sub manager | |
if argv[2:]: | |
manager = manager._commands[argv[2]] | |
# Generate list of command:descriptions | |
completions = [] | |
for name, command in manager._commands.items(): | |
if hasattr(command, 'help'): | |
completions.append(name + ':' + command.help) | |
elif hasattr(command, 'description'): | |
completions.append(name + ':' + command.description) | |
else: | |
completions.append(name + ':') | |
return '\n'.join(completions) | |
if __name__ == "__main__": | |
completions = complete(sys.argv, manager) | |
if completions: | |
print completions | |
else: | |
manager.run() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Note you need to tell zsh to use
_managepy
for manage.py files still, in your zshrc: