Skip to content

Instantly share code, notes, and snippets.

@labocho
Created December 17, 2015 09:41
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save labocho/ebfb835a863b53df1ab6 to your computer and use it in GitHub Desktop.
Save labocho/ebfb835a863b53df1ab6 to your computer and use it in GitHub Desktop.
thor-zsh_completion example
#compdef example
local state
_example() {
__example
}
__example() {
readonly local DEPTH=2
case $CURRENT in
$DEPTH)
_arguments \
'*: :->subcommands'
case $state in
subcommands)
_values \
'subcommand' \
'foo[Description of foo]' \
'nest1[Description of nest1]' \
;
;;
esac
;;
*)
case $words[$DEPTH] in
foo)
__example_foo
;;
nest1)
__example_nest1
;;
*)
# if does not match any subcommand
# complete rest arguments
_files
;;
esac
;;
esac
}
__example_foo() {
_arguments \
{--global} \
'*: :->rest'
case $state in
rest)
# complete rest arguments
_files
;;
esac
}
__example_nest1() {
readonly local DEPTH=3
case $CURRENT in
$DEPTH)
_arguments \
{--global} \
'*: :->subcommands'
case $state in
subcommands)
_values \
'subcommand' \
'bar[Description of bar]' \
'nest2[Description of nest2]' \
'help[Describe subcommands or one specific subcommand]' \
;
;;
esac
;;
*)
case $words[$DEPTH] in
bar)
__example_nest1_bar
;;
nest2)
__example_nest1_nest2
;;
help)
__example_nest1_help
;;
*)
# if does not match any subcommand
# complete rest arguments
_files
;;
esac
;;
esac
}
__example_nest1_bar() {
_arguments \
'*: :->rest'
case $state in
rest)
# complete rest arguments
_files
;;
esac
}
__example_nest1_nest2() {
readonly local DEPTH=4
case $CURRENT in
$DEPTH)
_arguments \
'*: :->subcommands'
case $state in
subcommands)
_values \
'subcommand' \
'baz[Description of baz]' \
'help[Describe subcommands or one specific subcommand]' \
;
;;
esac
;;
*)
case $words[$DEPTH] in
baz)
__example_nest1_nest2_baz
;;
help)
__example_nest1_nest2_help
;;
*)
# if does not match any subcommand
# complete rest arguments
_files
;;
esac
;;
esac
}
__example_nest1_nest2_baz() {
_arguments \
'*: :->rest'
case $state in
rest)
# complete rest arguments
_files
;;
esac
}
__example_nest1_nest2_help() {
_arguments \
'*: :->rest'
case $state in
rest)
# complete rest arguments
_files
;;
esac
}
__example_nest1_help() {
_arguments \
'*: :->rest'
case $state in
rest)
# complete rest arguments
_files
;;
esac
}
compdef _example example
require "thor"
require "thor/zsh_completion"
class Nest2 < Thor
desc "baz", "Description of baz"
def baz
p options
puts "baz"
end
end
class Nest1 < Thor
desc "bar", "Description of bar"
def bar
p options
puts "bar"
end
desc "nest2", "Description of nest2"
subcommand "nest2", Nest2
end
class Example < Thor
class_option :global, description: "Global option"
desc "foo", "Description of foo"
def foo
p options
puts "foo"
end
desc "nest1", "Description of nest1"
subcommand "nest1", Nest1
end
puts Thor::ZshCompletion::Generator.new(Example, "example").generate
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment