Release v0.2.2
This commit is contained in:
commit
81a1f6d97c
15
command.go
15
command.go
|
@ -71,6 +71,13 @@ func (c *Command) Execute(args []string) {
|
||||||
if c.isRoot {
|
if c.isRoot {
|
||||||
args = args[1:]
|
args = args[1:]
|
||||||
}
|
}
|
||||||
|
if len(args) > 0 {
|
||||||
|
sc, ok := c.Subcommands.Get(args[0])
|
||||||
|
if ok {
|
||||||
|
sc.Execute(args[1:])
|
||||||
|
return
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
parser := opt.NewParser(args, c.Opts, false)
|
parser := opt.NewParser(args, c.Opts, false)
|
||||||
restArgs, err := parser.Parse()
|
restArgs, err := parser.Parse()
|
||||||
|
@ -80,14 +87,6 @@ func (c *Command) Execute(args []string) {
|
||||||
os.Exit(1)
|
os.Exit(1)
|
||||||
}
|
}
|
||||||
|
|
||||||
if len(restArgs) > 0 {
|
|
||||||
sc, ok := c.Subcommands.Get(restArgs[0])
|
|
||||||
if ok {
|
|
||||||
sc.Execute(restArgs[1:])
|
|
||||||
return
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
helpOpt, ok := opt.Globals().GetBool("help")
|
helpOpt, ok := opt.Globals().GetBool("help")
|
||||||
if ok && helpOpt.Value() {
|
if ok && helpOpt.Value() {
|
||||||
c.ShowHelp()
|
c.ShowHelp()
|
||||||
|
|
|
@ -9,11 +9,49 @@ import (
|
||||||
|
|
||||||
func WriteBashCompletions(out io.Writer, rootCmd *Command) error {
|
func WriteBashCompletions(out io.Writer, rootCmd *Command) error {
|
||||||
return bashTpl.Execute(out, map[string]any{
|
return bashTpl.Execute(out, map[string]any{
|
||||||
"rootCmd": rootCmd,
|
"RootCmd": rootCmd,
|
||||||
"globalOpts": opt.Globals(),
|
"GlobalOpts": opt.Globals(),
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
var bashTpl = template.Must(template.New("bash").Parse(`
|
// TODO: Add --option|--other-option...) to return nothing for options that require a value
|
||||||
|
var bashTpl = template.Must(template.New("bash").Funcs(tplFuncs).Parse(`
|
||||||
|
{{- $rootCmd := .RootCmd -}}
|
||||||
|
{{- $progName := $rootCmd.Name -}}
|
||||||
|
{{- $varName := under $rootCmd.Name -}}
|
||||||
|
|
||||||
|
{{- define "cmd" }}
|
||||||
|
{{ $currIdx := .Index -}}
|
||||||
|
{{- $nextIdx := inc .Index -}}
|
||||||
|
{{- $indentSize := mult $currIdx 2 -}}
|
||||||
|
{{ indent $indentSize }}"{{ .Cmd.Name }}")
|
||||||
|
{{ indent $indentSize }} case ${COMP_WORDS[{{ $currIdx }}]} in
|
||||||
|
{{ indent $indentSize }} {{ range .Cmd.Subcommands -}}
|
||||||
|
{{ indent $indentSize }} {{ template "cmd" (map "Cmd" . "Index" $nextIdx) -}}
|
||||||
|
{{ indent $indentSize }} {{ end }}
|
||||||
|
{{ indent $indentSize }} *)
|
||||||
|
{{ indent $indentSize }} COMPREPLY=($(compgen -W "{{ join .Cmd.Subcommands.Names " " }} {{ join .Cmd.Opts.Names " " }}" -- $curr))
|
||||||
|
{{ indent $indentSize }} ;;
|
||||||
|
{{ indent $indentSize }} esac
|
||||||
|
{{ indent $indentSize }};; # {{ .Cmd.Name }}
|
||||||
|
{{- end -}}
|
||||||
|
|
||||||
|
_{{$varName}}_completions()
|
||||||
|
{
|
||||||
|
COMPREPLY=()
|
||||||
|
|
||||||
|
case ${COMP_WORDS[1]} in
|
||||||
|
{{- range .RootCmd.Subcommands -}}
|
||||||
|
{{ template "cmd" (map "Cmd" . "Index" 2) -}}
|
||||||
|
{{ end }}
|
||||||
|
*)
|
||||||
|
COMPREPLY=($(compgen -W "{{ join .RootCmd.Subcommands.Names " " }} {{ join .RootCmd.Opts.Names " " }}" -- $curr))
|
||||||
|
;;
|
||||||
|
esac
|
||||||
|
|
||||||
|
# Global Options
|
||||||
|
COMPREPLY+=($(compgen -W "{{ join .GlobalOpts.Names " " }}" -- $curr))
|
||||||
|
}
|
||||||
|
|
||||||
|
complete -F _{{$varName}}_completions {{$progName}}
|
||||||
`))
|
`))
|
||||||
|
|
|
@ -14,11 +14,12 @@ func WriteFishCompletions(out io.Writer, rootCmd *Command) error {
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// TODO: Fix fish infinitely completing last command at the end of the command chain
|
||||||
var fishTpl = template.Must(template.New("fish").Funcs(tplFuncs).Parse(`
|
var fishTpl = template.Must(template.New("fish").Funcs(tplFuncs).Parse(`
|
||||||
{{- $rootCmd := .RootCmd -}}
|
{{- $rootCmd := .RootCmd -}}
|
||||||
{{- $progName := $rootCmd.Name -}}
|
{{- $progName := $rootCmd.Name -}}
|
||||||
{{- $varName := under $rootCmd.Name -}}
|
{{- $varName := under $rootCmd.Name -}}
|
||||||
set -l commands {{- range $rootCmd.Subcommands }} {{ .Name }}{{ end }}
|
set -l commands {{ join $rootCmd.Subcommands.Names " " }}
|
||||||
|
|
||||||
function __fish_{{ $varName }}_needs_command
|
function __fish_{{ $varName }}_needs_command
|
||||||
set -l cmd (commandline -opc)
|
set -l cmd (commandline -opc)
|
||||||
|
@ -74,7 +75,7 @@ complete -f -c {{ $progName }} -n "__fish_{{ $varName }}_using_command {{.Cmd.Pa
|
||||||
{{ end -}}
|
{{ end -}}
|
||||||
|
|
||||||
{{ if gt (len .Cmd.Subcommands) 0 }}
|
{{ if gt (len .Cmd.Subcommands) 0 }}
|
||||||
set -l {{ $varPrefix }}commands {{- range .Cmd.Subcommands }} {{ .Name }}{{ end -}}
|
set -l {{ $varPrefix }}commands {{ join .Cmd.Subcommands.Names " " }}
|
||||||
{{ $cmdName := .Cmd.Name }}
|
{{ $cmdName := .Cmd.Name }}
|
||||||
{{- range .Cmd.Subcommands }}
|
{{- range .Cmd.Subcommands }}
|
||||||
{{- template "cmd" (map "Cmd" . "ProgName" $progName "VarName" $varName) -}}
|
{{- template "cmd" (map "Cmd" . "ProgName" $progName "VarName" $varName) -}}
|
||||||
|
|
10
opt/set.go
10
opt/set.go
|
@ -33,6 +33,16 @@ func (s Set) Get(name string) (Option, bool) {
|
||||||
return nil, false
|
return nil, false
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (s Set) Names() []string {
|
||||||
|
names := []string{}
|
||||||
|
for _, o := range s {
|
||||||
|
names = append(names, "--"+o.Name())
|
||||||
|
names = append(names, "-"+o.ShortName())
|
||||||
|
}
|
||||||
|
return names
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
func (s Set) GetByLongName(longName string) (Option, bool) {
|
func (s Set) GetByLongName(longName string) (Option, bool) {
|
||||||
for _, o := range s {
|
for _, o := range s {
|
||||||
if o.Name() == longName {
|
if o.Name() == longName {
|
||||||
|
|
9
set.go
9
set.go
|
@ -35,6 +35,15 @@ func (s Set) MaxNameWidth() int {
|
||||||
return max
|
return max
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (s Set) Names() []string {
|
||||||
|
names := make([]string, 0, len(s))
|
||||||
|
for _, c := range s {
|
||||||
|
names = append(names, c.Name)
|
||||||
|
names = append(names, c.Aliases...)
|
||||||
|
}
|
||||||
|
return names
|
||||||
|
}
|
||||||
|
|
||||||
func (s Set) Len() int {
|
func (s Set) Len() int {
|
||||||
return len(s)
|
return len(s)
|
||||||
}
|
}
|
||||||
|
|
42
tpl_funcs.go
42
tpl_funcs.go
|
@ -8,9 +8,17 @@ import (
|
||||||
|
|
||||||
var tplFuncs = template.FuncMap{
|
var tplFuncs = template.FuncMap{
|
||||||
"map": tplMap,
|
"map": tplMap,
|
||||||
|
"cat": tplCat,
|
||||||
"join": tplJoin,
|
"join": tplJoin,
|
||||||
"under": tplUnder,
|
"under": tplUnder,
|
||||||
"varPrefix": tplVarPrefix,
|
"varPrefix": tplVarPrefix,
|
||||||
|
"repeat": tplRepeat,
|
||||||
|
"indent": tplIndent,
|
||||||
|
"add": tplAdd,
|
||||||
|
"inc": tplInc,
|
||||||
|
"sub": tplSub,
|
||||||
|
"dec": tplDec,
|
||||||
|
"mult": tplMult,
|
||||||
}
|
}
|
||||||
|
|
||||||
func tplMap(vals ...any) (map[string]any, error) {
|
func tplMap(vals ...any) (map[string]any, error) {
|
||||||
|
@ -25,10 +33,14 @@ func tplMap(vals ...any) (map[string]any, error) {
|
||||||
return m, nil
|
return m, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func tplJoin(strs ...string) string {
|
func tplCat(strs ...string) string {
|
||||||
return strings.Join(strs, "")
|
return strings.Join(strs, "")
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func tplJoin(strs []string, sep string) string {
|
||||||
|
return strings.Join(strs, sep)
|
||||||
|
}
|
||||||
|
|
||||||
func tplUnder(s string) string {
|
func tplUnder(s string) string {
|
||||||
return strings.ToLower(strings.ReplaceAll(strings.ReplaceAll(s, " ", "_"), "-", "_"))
|
return strings.ToLower(strings.ReplaceAll(strings.ReplaceAll(s, " ", "_"), "-", "_"))
|
||||||
}
|
}
|
||||||
|
@ -40,3 +52,31 @@ func tplVarPrefix(s string) string {
|
||||||
|
|
||||||
return tplUnder(s) + "_"
|
return tplUnder(s) + "_"
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func tplRepeat(s string, n int) string {
|
||||||
|
return strings.Repeat(s, n)
|
||||||
|
}
|
||||||
|
|
||||||
|
func tplIndent(n int) string {
|
||||||
|
return tplRepeat(" ", n)
|
||||||
|
}
|
||||||
|
|
||||||
|
func tplAdd(a, b int) int {
|
||||||
|
return a + b
|
||||||
|
}
|
||||||
|
|
||||||
|
func tplInc(i int) int {
|
||||||
|
return tplAdd(i, 1)
|
||||||
|
}
|
||||||
|
|
||||||
|
func tplSub(a, b int) int {
|
||||||
|
return a - b
|
||||||
|
}
|
||||||
|
|
||||||
|
func tplDec(i int) int {
|
||||||
|
return tplSub(i, 1)
|
||||||
|
}
|
||||||
|
|
||||||
|
func tplMult(a, b int) int {
|
||||||
|
return a * b
|
||||||
|
}
|
||||||
|
|
Loading…
Reference in New Issue