Compare commits
16 Commits
Author | SHA1 | Date |
---|---|---|
|
160b3321ef | |
|
73d7db2078 | |
|
b4759d5f08 | |
|
1fbc94aa49 | |
|
26bee58adc | |
|
ba90879ff0 | |
|
a573f722af | |
|
c5bd7662f2 | |
|
b4d610e5fd | |
|
384863fcdf | |
|
1ee91301f4 | |
|
81a1f6d97c | |
|
5979ea58a2 | |
|
71cddc5b20 | |
|
2c26eda5e2 | |
|
31c4264e4f |
31
command.go
31
command.go
|
@ -9,6 +9,7 @@ import (
|
|||
|
||||
type Command struct {
|
||||
Name string
|
||||
version string
|
||||
ShortDescription string
|
||||
LongDescription string
|
||||
Aliases []string
|
||||
|
@ -20,8 +21,8 @@ type Command struct {
|
|||
isRoot bool
|
||||
}
|
||||
|
||||
func NewRoot(name string, options ...Option) *Command {
|
||||
cmd := &Command{Name: name, isRoot: true}
|
||||
func NewRoot(name string, version string, options ...Option) *Command {
|
||||
cmd := &Command{Name: name, version: version, isRoot: true}
|
||||
cmd.ApplyOptions(options...)
|
||||
return cmd
|
||||
}
|
||||
|
@ -46,6 +47,10 @@ func (c *Command) Root() *Command {
|
|||
return c.Parent.Root()
|
||||
}
|
||||
|
||||
func (c *Command) IsRoot() bool {
|
||||
return c.isRoot
|
||||
}
|
||||
|
||||
func (c *Command) CommandPath() string {
|
||||
if c.Parent == nil {
|
||||
return ""
|
||||
|
@ -71,6 +76,13 @@ func (c *Command) Execute(args []string) {
|
|||
if c.isRoot {
|
||||
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)
|
||||
restArgs, err := parser.Parse()
|
||||
|
@ -80,12 +92,10 @@ func (c *Command) Execute(args []string) {
|
|||
os.Exit(1)
|
||||
}
|
||||
|
||||
if len(restArgs) > 0 {
|
||||
sc, ok := c.Subcommands.Get(restArgs[0])
|
||||
if ok {
|
||||
sc.Execute(restArgs[1:])
|
||||
return
|
||||
}
|
||||
versionOpt, ok := opt.Globals().GetBool("version")
|
||||
if ok && versionOpt.Value() {
|
||||
c.ShowVersion()
|
||||
return
|
||||
}
|
||||
|
||||
helpOpt, ok := opt.Globals().GetBool("help")
|
||||
|
@ -101,3 +111,8 @@ func (c *Command) Execute(args []string) {
|
|||
|
||||
c.ShowHelp()
|
||||
}
|
||||
|
||||
func (c *Command) ShowVersion() {
|
||||
rootName := c.Root().Name
|
||||
fmt.Printf("%s %s\n", rootName, c.version)
|
||||
}
|
||||
|
|
|
@ -9,11 +9,50 @@ import (
|
|||
|
||||
func WriteBashCompletions(out io.Writer, rootCmd *Command) error {
|
||||
return bashTpl.Execute(out, map[string]any{
|
||||
"rootCmd": rootCmd,
|
||||
"globalOpts": opt.Globals(),
|
||||
"RootCmd": rootCmd,
|
||||
"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
|
||||
// TODO: Add descriptions to completions using https://stackoverflow.com/a/10130007
|
||||
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(`
|
||||
{{- $rootCmd := .RootCmd -}}
|
||||
{{- $progName := $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
|
||||
set -l cmd (commandline -opc)
|
||||
|
@ -74,7 +75,7 @@ complete -f -c {{ $progName }} -n "__fish_{{ $varName }}_using_command {{.Cmd.Pa
|
|||
{{ end -}}
|
||||
|
||||
{{ 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 }}
|
||||
{{- range .Cmd.Subcommands }}
|
||||
{{- template "cmd" (map "Cmd" . "ProgName" $progName "VarName" $varName) -}}
|
||||
|
|
|
@ -9,11 +9,82 @@ import (
|
|||
|
||||
func WritePowerShellCompletions(out io.Writer, rootCmd *Command) error {
|
||||
return powerShellTpl.Execute(out, map[string]any{
|
||||
"rootCmd": rootCmd,
|
||||
"globalOpts": opt.Globals(),
|
||||
"RootCmd": rootCmd,
|
||||
"GlobalOpts": opt.Globals(),
|
||||
})
|
||||
}
|
||||
|
||||
var powerShellTpl = template.Must(template.New("PowerShell").Parse(`
|
||||
|
||||
var powerShellTpl = template.Must(template.New("PowerShell").Funcs(tplFuncs).Parse(`
|
||||
{{- $rootCmd := .RootCmd -}}
|
||||
{{- $progName := $rootCmd.Name -}}
|
||||
{{- $varName := $rootCmd.Name | camel -}}
|
||||
|
||||
{{- define "cmd" -}}
|
||||
{{- $progName := .ProgName -}}
|
||||
{{- if .Cmd.IsRoot }}
|
||||
{{- /**/}} '{{ $progName }}' {
|
||||
{{- else }}
|
||||
{{- /**/}} '{{ $progName }};{{ join (split .Cmd.CommandPath " ") ";" }}' {
|
||||
{{- end -}}
|
||||
{{- range .Cmd.Opts -}}
|
||||
{{ if ne .Name "" }}
|
||||
[CompletionResult]::new('--{{ .Name }}', '{{ .Name }}', [CompletionResultType]::ParameterName, '{{ .Description }}')
|
||||
{{- end -}}
|
||||
{{- if ne .ShortName "" }}
|
||||
[CompletionResult]::new('-{{ .ShortName }}', '{{ .ShortName }}', [CompletionResultType]::ParameterName, '{{ .Description }}')
|
||||
{{- end -}}
|
||||
{{- end }}
|
||||
{{- range .Cmd.Subcommands }}
|
||||
[CompletionResult]::new('{{ .Name }}', '{{ .Name }}', [CompletionResultType]::ParameterValue, '{{ .ShortDescription }}')
|
||||
{{- end }}
|
||||
break;
|
||||
}
|
||||
{{- range .Cmd.Subcommands }}
|
||||
{{ template "cmd" (map "Cmd" . "ProgName" $progName) }}
|
||||
{{- end -}}
|
||||
{{- end -}}
|
||||
using namespace System.Management.Automation
|
||||
using namespace System.Management.Automation.Language
|
||||
|
||||
[scriptblock]$__{{ $varName }}CompleterBlock = {
|
||||
param(
|
||||
$wordToComplete,
|
||||
$commandAst,
|
||||
$cursorPosition
|
||||
)
|
||||
|
||||
$commandElements
|
||||
$command = @(
|
||||
'{{ $progName }}'
|
||||
for ($i = 1; $i -lt $commandElements.Count; $i++) {
|
||||
$element = $commandElements[$i]
|
||||
if ($element -isnot [StringConstantExpressionAst] -or
|
||||
$element.StringConstantType -ne [StringConstantType]::BareWord -or
|
||||
$element.Value.StartsWith('-') -or
|
||||
$element.Value -eq $wordToComplete) {
|
||||
break
|
||||
}
|
||||
$element.Value
|
||||
}
|
||||
) -join ';'
|
||||
|
||||
$completions = @(
|
||||
switch ($command) {
|
||||
{{ template "cmd" (map "Cmd" $rootCmd "ProgName" $progName) }}
|
||||
}
|
||||
{{- range .GlobalOpts -}}
|
||||
{{- if ne .Name "" }}
|
||||
[CompletionResult]::new('--{{ .Name }}', '{{ .Name }}', [CompletionResultType]::ParameterName, '{{ .Description }}')
|
||||
{{- end -}}
|
||||
{{- if ne .ShortName "" }}
|
||||
[CompletionResult]::new('-{{ .ShortName }}', '{{ .ShortName }}', [CompletionResultType]::ParameterName, '{{ .Description }}')
|
||||
{{- end -}}
|
||||
{{- end -}}
|
||||
)
|
||||
|
||||
$completions.Where{ $_.CompletionText -like "$wordToComplete*" } |
|
||||
Sort-Object -Property ListItemText
|
||||
}
|
||||
|
||||
Register-ArgumentCompleter -CommandName {{ $progName }} -ScriptBlock $__{{ $varName }}CompleterBlock
|
||||
`))
|
||||
|
|
|
@ -9,11 +9,77 @@ import (
|
|||
|
||||
func WriteZshCompletions(out io.Writer, rootCmd *Command) error {
|
||||
return zshTpl.Execute(out, map[string]any{
|
||||
"rootCmd": rootCmd,
|
||||
"globalOpts": opt.Globals(),
|
||||
"RootCmd": rootCmd,
|
||||
"GlobalOpts": opt.Globals(),
|
||||
})
|
||||
}
|
||||
|
||||
var zshTpl = template.Must(template.New("zsh").Parse(`
|
||||
|
||||
// TODO: Fix indentation and other spacing
|
||||
var zshTpl = template.Must(template.New("zsh").Funcs(tplFuncs).Parse(`
|
||||
{{- $rootCmd := .RootCmd -}}
|
||||
{{- $progName := $rootCmd.Name -}}
|
||||
{{- $varName := under $rootCmd.Name -}}
|
||||
|
||||
{{- define "cmd" }}
|
||||
{{- $varPrefix := varPrefix .Cmd.CommandPath -}}
|
||||
{{- if not .Cmd.IsRoot}}
|
||||
{{ .Cmd.Name }})
|
||||
{{ end -}}
|
||||
{{ if gt (len .Cmd.Subcommands) 0 -}}
|
||||
local -a {{ $varPrefix }}commands
|
||||
{{ $varPrefix }}commands=(
|
||||
{{- range .Cmd.Subcommands}}
|
||||
'{{ .Name }}:{{ .ShortDescription }}'
|
||||
{{- end }}
|
||||
)
|
||||
{{ end }}
|
||||
|
||||
{{- if or (gt (len .Cmd.Opts) 0) (gt (len .Cmd.Subcommands) 0) -}}
|
||||
_arguments
|
||||
{{- end -}}
|
||||
{{- range .Cmd.Opts -}}
|
||||
{{- if ne .Name ""}} \
|
||||
'--{{ .Name }}[{{ .Description }}]'
|
||||
{{- end }}
|
||||
{{- if ne .ShortName ""}} \
|
||||
'-{{ .ShortName }}[{{ .Description }}]'
|
||||
{{- end }}
|
||||
{{- end }}
|
||||
{{- if gt (len .Cmd.Subcommands) 0 -}}
|
||||
{{ " " }}\
|
||||
'1: :{_describe 'command' {{ $varPrefix }}commands}' \
|
||||
'*:: :->args'
|
||||
|
||||
case $state in
|
||||
args)
|
||||
case $words[1] in
|
||||
{{- range .Cmd.Subcommands }}
|
||||
{{ template "cmd" (map "Cmd" .) }}
|
||||
{{- end }}
|
||||
esac
|
||||
;;
|
||||
esac
|
||||
{{- end }}
|
||||
{{- if not .Cmd.IsRoot}}
|
||||
;;
|
||||
{{ end }}
|
||||
{{ end -}}
|
||||
|
||||
function _{{ $varName }} {
|
||||
{{ template "cmd" (map "Cmd" .RootCmd) -}}
|
||||
|
||||
{{- if gt (len .GlobalOpts) 0 }}
|
||||
_arguments
|
||||
{{- range .GlobalOpts -}}
|
||||
{{- if ne .Name ""}} \
|
||||
'--{{ .Name }}[{{ .Description }}]'
|
||||
{{- end }}
|
||||
{{- if ne .ShortName ""}} \
|
||||
'-{{ .ShortName }}[{{ .Description }}]'
|
||||
{{- end }}
|
||||
{{- end }}
|
||||
{{- end }}
|
||||
}
|
||||
|
||||
compdef _{{ $varName }} {{ $progName }}
|
||||
`))
|
||||
|
|
2
go.mod
2
go.mod
|
@ -1,3 +1,5 @@
|
|||
module go.fifitido.net/cmd
|
||||
|
||||
go 1.21.3
|
||||
|
||||
require github.com/iancoleman/strcase v0.3.0
|
||||
|
|
2
go.sum
2
go.sum
|
@ -0,0 +1,2 @@
|
|||
github.com/iancoleman/strcase v0.3.0 h1:nTXanmYxhfFAMjZL34Ov6gkzEsSJZ5DbhxWjvSASxEI=
|
||||
github.com/iancoleman/strcase v0.3.0/go.mod h1:iwCmte+B7n89clKwxIoIXy/HfoL7AsD47ZCWhYzw7ho=
|
5
help.go
5
help.go
|
@ -2,6 +2,7 @@ package cmd
|
|||
|
||||
import (
|
||||
"fmt"
|
||||
"strings"
|
||||
|
||||
"go.fifitido.net/cmd/opt"
|
||||
)
|
||||
|
@ -43,8 +44,10 @@ func (c *Command) ShowHelp() {
|
|||
fmt.Println("Available subcommands:")
|
||||
}
|
||||
|
||||
paddedWidth := c.Subcommands.MaxWidth()
|
||||
for _, s := range c.Subcommands {
|
||||
fmt.Println(" " + s.Name + " " + s.ShortDescription)
|
||||
spaceSize := 2 + paddedWidth - len(s.Name)
|
||||
fmt.Println(" " + s.Name + strings.Repeat(" ", spaceSize) + s.ShortDescription)
|
||||
}
|
||||
}
|
||||
|
||||
|
|
10
opt/set.go
10
opt/set.go
|
@ -33,6 +33,16 @@ func (s Set) Get(name string) (Option, bool) {
|
|||
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) {
|
||||
for _, o := range s {
|
||||
if o.Name() == longName {
|
||||
|
|
11
set.go
11
set.go
|
@ -25,7 +25,7 @@ func (s Set) Get(name string) (*Command, bool) {
|
|||
return nil, false
|
||||
}
|
||||
|
||||
func (s Set) MaxNameWidth() int {
|
||||
func (s Set) MaxWidth() int {
|
||||
max := 0
|
||||
for _, f := range s {
|
||||
if w := len(f.Name); w > max {
|
||||
|
@ -35,6 +35,15 @@ func (s Set) MaxNameWidth() int {
|
|||
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 {
|
||||
return len(s)
|
||||
}
|
||||
|
|
61
tpl_funcs.go
61
tpl_funcs.go
|
@ -4,13 +4,26 @@ import (
|
|||
"fmt"
|
||||
"strings"
|
||||
"text/template"
|
||||
|
||||
"github.com/iancoleman/strcase"
|
||||
)
|
||||
|
||||
var tplFuncs = template.FuncMap{
|
||||
"map": tplMap,
|
||||
"cat": tplCat,
|
||||
"split": tplSplit,
|
||||
"join": tplJoin,
|
||||
"under": tplUnder,
|
||||
"varPrefix": tplVarPrefix,
|
||||
"repeat": tplRepeat,
|
||||
"indent": tplIndent,
|
||||
"add": tplAdd,
|
||||
"inc": tplInc,
|
||||
"sub": tplSub,
|
||||
"dec": tplDec,
|
||||
"mult": tplMult,
|
||||
"pascal": tplPascal,
|
||||
"camel": tplCamel,
|
||||
}
|
||||
|
||||
func tplMap(vals ...any) (map[string]any, error) {
|
||||
|
@ -25,12 +38,20 @@ func tplMap(vals ...any) (map[string]any, error) {
|
|||
return m, nil
|
||||
}
|
||||
|
||||
func tplJoin(strs ...string) string {
|
||||
func tplCat(strs ...string) string {
|
||||
return strings.Join(strs, "")
|
||||
}
|
||||
|
||||
func tplSplit(s string, sep string) []string {
|
||||
return strings.Split(s, sep)
|
||||
}
|
||||
|
||||
func tplJoin(strs []string, sep string) string {
|
||||
return strings.Join(strs, sep)
|
||||
}
|
||||
|
||||
func tplUnder(s string) string {
|
||||
return strings.ToLower(strings.ReplaceAll(strings.ReplaceAll(s, " ", "_"), "-", "_"))
|
||||
return strcase.ToSnake(s)
|
||||
}
|
||||
|
||||
func tplVarPrefix(s string) string {
|
||||
|
@ -40,3 +61,39 @@ func tplVarPrefix(s string) string {
|
|||
|
||||
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
|
||||
}
|
||||
|
||||
func tplPascal(s string) string {
|
||||
return strcase.ToCamel(s)
|
||||
}
|
||||
|
||||
func tplCamel(s string) string {
|
||||
return strcase.ToLowerCamel(s)
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue