V2EX = way to explore
V2EX 是一个关于分享和探索的地方
现在注册
已注册用户请  登录
dalang
V2EX  ›  问与答

如何写oh-my-zsh的plugin,有教程吗?

  •  
  •   dalang · 2013-10-09 13:59:52 +08:00 · 4569 次点击
    这是一个创建于 4150 天前的主题,其中的信息可能已经有所发展或是发生改变。
    之前参照oh-my-zsh自带的brew插件写了一个:

    #oh-my-zsh razor plugin Enable Razor from puppetlabs capability of command autocomplete

    Ensure you have installed oh-my-zsh ###How to use this razor plugin:

    1. mkdir a new folder named razor in your oh-my-zsh plugin folder: ~/.oh-my-zsh/plugins/razor
    2. put this file in ~/.oh-my-zsh/plugins/razor
    3. config your ~/.zshrc to enable razor plugin: add razor in the bracket of the line starts with plugins=(
    4. Try type razor in terminal and test command autocomplete with tab
    view raw README.md hosted with ❤ by GitHub
    #compdef razor
    #autoload
    local -a _1st_arguments
    _1st_arguments=(
    'active_model:sed to view active models or active model logs, and to remove active models.'
    'bmc:used to view the current list of BMCs; also used by to register new BMCs with Razor.'
    'broker:used to add, view, update, and remove Broker Targets.'
    'image:used to add, view, and remove Images.'
    'log:used to view logs'
    'model:used to add, view, update, and remove models.'
    'node:used to view the current list of nodes (or node details)'
    'policy:used to view, create, update, and remove policies.'
    'tag:used to view, create, update, and remove tags.'
    'vmodel:used to add, view, update, and remove vmodels.'
    )
    local -a _active_model_arguments
    _active_model_arguments=(
    'get:View active models'
    'serial:View specific active model by serial number'
    'logview:Prints an aggregate active model log view'
    'remove:Remove existing (or all) active model(s)'
    )
    local -a _image_arguments
    _image_arguments=(
    'get:View images'
    'add:Add a new image to the system'
    'remove:Remove existing image from the system'
    )
    local -a _model_arguments
    _model_arguments=(
    'get:View model'
    'add:Create a new model instance'
    'update:Update a specific model instance'
    'remove:Remove existing model(s)'
    )
    local -a _node_arguments
    _node_arguments=(
    'get:View node'
    )
    local -a _policy_arguments
    _policy_arguments=(
    'get:View available policy templates'
    'add:Create a new policy'
    'update:Update an existing policy'
    'remove:Remove existing policy(s)'
    'callback:Invoke a callback for an existing policy(s)'
    'vcallback:Invoke a vmodel callback for an existing policy(s)'
    )
    local -a _tag_arguments
    _tag_arguments=(
    'get:View details of a Tag'
    'add:Create a new Tag'
    'update:Update an existing Tag'
    'remove:Remove existing Tag(s)'
    'matcher:Tag Matcher'
    )
    local -a _vmodel_arguments
    _vmodel_arguments=(
    'get:View vmodel'
    'add:Create a new vmodel instance'
    'update:Update a specific vmodel instance'
    'remove:Remove existing vmodel(s)'
    )
    local expl
    local -a active_model_formulae
    _arguments \
    '(-v --verbose)'{-v,--verbose}'[Enables verbose object printing]' \
    '(-d --debug)'{-d,--debug}'[Enables printing proper Ruby stacktrace]' \
    '(-w --webcommand)'{-w,--webcommand}'[Accepts web commands.]' \
    '(-j --jsoncommand)'{-j,--jsoncommand}'[Same as -w but not exposed in web UI.]' \
    '(-n --no-color)'{-n,--no-color}'[Disables console color. Useful for script wrapping.]' \
    '(-V --version)'{-V,--version}'[Display the version of Razor]' \
    '(-h --help)'{-h,--help}'[Display help info]' \
    '*:: :->subcmds' && return 0
    if (( CURRENT == 1 )); then
    _describe -t commands "razor subcommand" _1st_arguments
    return
    fi
    case "$words[1]" in
    active_model)
    _arguments \
    '(-h --help)'{-h,--help}'[Dipslay help info]' \
    '1: :->actions' && return 0
    if [[ "$state" == actions ]]; then
    _describe -t commands "razor active_model" _active_model_arguments
    fi ;;
    image)
    _arguments \
    '(-h --help)'{-h,--help}'[Dipslay help info]' \
    '1: :->actions' \
    '*::options:->options' && ret=0
    case $state in
    actions)
    _describe -t commands "razor image" _image_arguments
    ;;
    options)
    if [[ "$words[1]" == 'add' ]]; then
    _arguments \
    '(-t --type)'{-t,--type}'[The type of image (mk, os, win, or esxi)]' \
    '(-p --path)'{-p,--path}'[The local path to the image ISO]' \
    '(-n --name)'{-n,--name}'[The logical name to use (os/win images only)]' \
    '(-v --version)'{-v,--version}'[The version to use (os/win images only)]' \
    '(-h --help)'{-h,--help}'[Display image add help info.]' && ret=0
    else
    _arguments '(-h --help)'{-h,--help}'[Dipslay help info]' && ret=0
    fi
    ;;
    esac
    ;;
    model)
    _arguments \
    '(-h --help)'{-h,--help}'[Dipslay help info]' \
    '1: :->actions' \
    '*::options:->options' && ret=0
    case $state in
    actions)
    _describe -t commands "razor model" _model_arguments
    ;;
    options)
    if [[ "$words[1]" == 'add' ]]; then
    _arguments \
    '(-t --template)'{-t,--template}"[The model template to use for the new model.]" \
    '(-l --label)'{-l,--label}'[The label to use for the new model.]' \
    '(-i --image-uuid)'{-i,--image-uuid}'[The image UUID to use for the new model.]' \
    '(-h --help)'{-h,--help}'[Display model add help info.]' && ret=0
    #elif [[ "$words[1]" == 'update' ]]; then
    #_arguments \
    #'(-l --label)'{-l,--label}'[The new label to use for the model.]' \
    #'(-i --image-uuid)'{-i,--image-uuid}'[The new image UUID to use for the model.]' \
    #'(-c --change-metadata)'{-c,--change-metadata}"[Used to trigger a change in the model's meta-data]" \
    #'(-h --help)'{-h,--help}'[Display this screen.]' && ret=0
    else
    _arguments '(-h --help)'{-h,--help}'[Dipslay help info]' && ret=0
    fi
    ;;
    esac
    ;;
    node)
    _arguments \
    '(-h --help)'{-h,--help}'[Dipslay help info]' \
    '1: :->actions' && ret=0
    if [[ "$state" == actions ]]; then
    _describe -t commands "razor node" _node_arguments
    fi ;;
    policy)
    _arguments \
    '(-h --help)'{-h,--help}'[Dipslay help info]' \
    '1: :->actions' \
    '*::options:->options' && ret=0
    case $state in
    actions)
    _describe -t commands "razor policy" _policy_arguments
    ;;
    options)
    if [[ "$words[1]" == 'add' ]]; then
    _arguments \
    '(-p --template)'{-p,--template}'[The policy template name to use.]' \
    '(-l --label)'{-l,--label}'[A label to name this policy.]' \
    '(-m --model-uuid)'{-m,--model-uuid}'[The model to attach to the policy.]' \
    '(-v --vmodel-uuid)'{-v,--vmodel-uuid}'[The vmodel to attach to the policy.]' \
    '(-b --broker-uuid)'{-b,--broker-uuid}"[The broker to attach to the policy \[default: none\].]" \
    '(-e --enabled)'{-e,--enabled}"[Should policy be enabled (true|false) \[default: false\]?]" \
    '(-x --maximum)'{-x,--maximum}"[Sets the policy maximum count for nodes \[default: 0\].]" \
    '(-h, --help)'{-h,--help}'[Display policy add help info.]' && ret=0
    #elif [[ "$words[1]" == 'update' ]]; then
    #_arguments \
    #'(-l --label)'{-l --label}'[A label to name this policy.]' \
    #'(-m --model-uuid)'{-m,--model-uuid}'[The model to attached to the policy.]' \
    #'(-v --vmodel-uuid)'{-v,--vmodel-uuid}'[The vmodel attached to the policy [defualt: none].]' \
    #'(-b --broker-uuid)'{-b,--broker-uuid}'[The broker attached to the policy [default: none].]' \
    #'(-e --enabled)'{-e,--enabled}'[Should policy be enabled (true|false) [default: false]?]' \
    #'(-x --maximum)'{-x,--maximum}'[Sets the policy maximum count for nodes [default: 0].]' \
    #'(-n --new-line-number)'{-n,--new-line-number}'[Change policy rule number.]' \
    #'(-h --help)'{-h,--help}'[Display this screen.]' && ret=0
    else
    _arguments '(-h --help)'{-h,--help}'[Dipslay help info]' && ret=0
    fi
    ;;
    esac
    ;;
    tag)
    _arguments \
    '(-h --help)'{-h,--help}'[Dipslay help info]' \
    '1: :->actions' \
    '*::options:->options' && ret=0
    case $state in
    actions)
    _describe -t commands "razor tag" _tag_arguments
    ;;
    options)
    if [[ "$words[1]" == 'add' ]]; then
    _arguments \
    '(-n --name)'{-n,--name}'[Name for the tagrule being created]' \
    '(-t --tag)'{-t,--tag}'[Tag for the tagrule being created]' \
    '(-h --help)'{-h,--help}'[ Display tage add help info.]' && ret=0
    #elif [[ "$words[1]" == 'update' ]]; then
    #_arguments \
    #'(-n --name)'{-n,--name}'[Name for the tagrule being created]' \
    #'(-t --tag)'{-t,--tag}'[Tag for the tagrule being created]' \
    #'(-h --help)'{-h,--help}'[Display this screen.]' && ret=0
    #elif [[ "$words[1]" == 'matcher' ]]; then
    #_arguments \
    #'(-h --help)'{-h,--help}'[Dipslay help info]' \
    #'1: :->matcher_actions' && ret=0
    #'*::options:->matcher_options' && ret=0
    #case $state in
    #matcher_actions)
    #declare -a matcher_commands
    #matcher_commands=()
    #_describe -t commands 'razor tag matcher' matcher_commands && ret=0
    #;;
    #matcher_options)
    #case $words[1] in
    #add)
    #;;
    #update)
    #;;
    #esac
    #;;
    #esac
    #;;
    else
    _arguments '(-h --help)'{-h,--help}'[Dipslay help info]' && ret=0
    fi
    ;;
    esac
    ;;
    vmodel)
    _arguments \
    '(-h --help)'{-h,--help}'[Dipslay help info]' \
    '1: :->actions' \
    '*::options:->options' && ret=0
    case $state in
    actions)
    _describe -t commands "razor vmodel" _vmodel_arguments
    ;;
    options)
    if [[ "$words[1]" == 'add' ]]; then
    _arguments \
    '(-t --template)'{-t,--template}'[The vmodel template to use for the new vmodel.]' \
    '(-l --label)'{-l,--label}'[The label to use for the new vmodel.]' \
    '(-h --help)'{-h,--help}'[Display vmodel add help info.]' && ret=0
    #elif [[ "$words[1]" == 'update' ]]; then
    #_arguments \
    #'(-l --label)'{-l,--label}'[The new label to use for the vmodel.]' \
    #'(-c --change-metadata)'{-c,--change-metadata}"[Used to trigger a change in the vmodel's meta-data]" \
    #'(-h --help)'{-h,--help}'[Display this screen.]' && ret=0
    else
    _arguments '(-h --help)'{-h,--help}'[Dipslay help info]' && ret=0
    fi
    ;;
    esac
    ;;
    esac
    view raw _razor hosted with ❤ by GitHub

    现在发现自己实现的命令补全会导致原来的Path路径补全失效,应该是plugin实现有问题。
    所以想请教下有没有关于oh-my-zsh插件的指导文章,好让我fix这个bug。
    1 条回复    1970-01-01 08:00:00 +08:00
    dalang
        1
    dalang  
    OP
       2013-10-09 14:01:23 +08:00
    oh oh ~ 刚发现v2ex会自动把gist的链接展开
    关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   实用小工具   ·   5751 人在线   最高记录 6679   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 33ms · UTC 06:27 · PVG 14:27 · LAX 22:27 · JFK 01:27
    Developed with CodeLauncher
    ♥ Do have faith in what you're doing.