Compare commits
No commits in common. "main" and "v0.2.2" have entirely different histories.
20
command.go
20
command.go
|
@ -9,7 +9,6 @@ import (
|
||||||
|
|
||||||
type Command struct {
|
type Command struct {
|
||||||
Name string
|
Name string
|
||||||
version string
|
|
||||||
ShortDescription string
|
ShortDescription string
|
||||||
LongDescription string
|
LongDescription string
|
||||||
Aliases []string
|
Aliases []string
|
||||||
|
@ -21,8 +20,8 @@ type Command struct {
|
||||||
isRoot bool
|
isRoot bool
|
||||||
}
|
}
|
||||||
|
|
||||||
func NewRoot(name string, version string, options ...Option) *Command {
|
func NewRoot(name string, options ...Option) *Command {
|
||||||
cmd := &Command{Name: name, version: version, isRoot: true}
|
cmd := &Command{Name: name, isRoot: true}
|
||||||
cmd.ApplyOptions(options...)
|
cmd.ApplyOptions(options...)
|
||||||
return cmd
|
return cmd
|
||||||
}
|
}
|
||||||
|
@ -47,10 +46,6 @@ func (c *Command) Root() *Command {
|
||||||
return c.Parent.Root()
|
return c.Parent.Root()
|
||||||
}
|
}
|
||||||
|
|
||||||
func (c *Command) IsRoot() bool {
|
|
||||||
return c.isRoot
|
|
||||||
}
|
|
||||||
|
|
||||||
func (c *Command) CommandPath() string {
|
func (c *Command) CommandPath() string {
|
||||||
if c.Parent == nil {
|
if c.Parent == nil {
|
||||||
return ""
|
return ""
|
||||||
|
@ -92,12 +87,6 @@ func (c *Command) Execute(args []string) {
|
||||||
os.Exit(1)
|
os.Exit(1)
|
||||||
}
|
}
|
||||||
|
|
||||||
versionOpt, ok := opt.Globals().GetBool("version")
|
|
||||||
if ok && versionOpt.Value() {
|
|
||||||
c.ShowVersion()
|
|
||||||
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()
|
||||||
|
@ -111,8 +100,3 @@ func (c *Command) Execute(args []string) {
|
||||||
|
|
||||||
c.ShowHelp()
|
c.ShowHelp()
|
||||||
}
|
}
|
||||||
|
|
||||||
func (c *Command) ShowVersion() {
|
|
||||||
rootName := c.Root().Name
|
|
||||||
fmt.Printf("%s %s\n", rootName, c.version)
|
|
||||||
}
|
|
||||||
|
|
|
@ -15,7 +15,6 @@ func WriteBashCompletions(out io.Writer, rootCmd *Command) error {
|
||||||
}
|
}
|
||||||
|
|
||||||
// TODO: Add --option|--other-option...) to return nothing for options that require a value
|
// 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(`
|
var bashTpl = template.Must(template.New("bash").Funcs(tplFuncs).Parse(`
|
||||||
{{- $rootCmd := .RootCmd -}}
|
{{- $rootCmd := .RootCmd -}}
|
||||||
{{- $progName := $rootCmd.Name -}}
|
{{- $progName := $rootCmd.Name -}}
|
||||||
|
|
|
@ -9,82 +9,11 @@ import (
|
||||||
|
|
||||||
func WritePowerShellCompletions(out io.Writer, rootCmd *Command) error {
|
func WritePowerShellCompletions(out io.Writer, rootCmd *Command) error {
|
||||||
return powerShellTpl.Execute(out, map[string]any{
|
return powerShellTpl.Execute(out, map[string]any{
|
||||||
"RootCmd": rootCmd,
|
"rootCmd": rootCmd,
|
||||||
"GlobalOpts": opt.Globals(),
|
"globalOpts": opt.Globals(),
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
var powerShellTpl = template.Must(template.New("PowerShell").Funcs(tplFuncs).Parse(`
|
var powerShellTpl = template.Must(template.New("PowerShell").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,77 +9,11 @@ import (
|
||||||
|
|
||||||
func WriteZshCompletions(out io.Writer, rootCmd *Command) error {
|
func WriteZshCompletions(out io.Writer, rootCmd *Command) error {
|
||||||
return zshTpl.Execute(out, map[string]any{
|
return zshTpl.Execute(out, map[string]any{
|
||||||
"RootCmd": rootCmd,
|
"rootCmd": rootCmd,
|
||||||
"GlobalOpts": opt.Globals(),
|
"globalOpts": opt.Globals(),
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
// TODO: Fix indentation and other spacing
|
var zshTpl = template.Must(template.New("zsh").Parse(`
|
||||||
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,5 +1,3 @@
|
||||||
module go.fifitido.net/cmd
|
module go.fifitido.net/cmd
|
||||||
|
|
||||||
go 1.21.3
|
go 1.21.3
|
||||||
|
|
||||||
require github.com/iancoleman/strcase v0.3.0
|
|
||||||
|
|
2
go.sum
2
go.sum
|
@ -1,2 +0,0 @@
|
||||||
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,7 +2,6 @@ package cmd
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"fmt"
|
"fmt"
|
||||||
"strings"
|
|
||||||
|
|
||||||
"go.fifitido.net/cmd/opt"
|
"go.fifitido.net/cmd/opt"
|
||||||
)
|
)
|
||||||
|
@ -44,10 +43,8 @@ func (c *Command) ShowHelp() {
|
||||||
fmt.Println("Available subcommands:")
|
fmt.Println("Available subcommands:")
|
||||||
}
|
}
|
||||||
|
|
||||||
paddedWidth := c.Subcommands.MaxWidth()
|
|
||||||
for _, s := range c.Subcommands {
|
for _, s := range c.Subcommands {
|
||||||
spaceSize := 2 + paddedWidth - len(s.Name)
|
fmt.Println(" " + s.Name + " " + s.ShortDescription)
|
||||||
fmt.Println(" " + s.Name + strings.Repeat(" ", spaceSize) + s.ShortDescription)
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
2
set.go
2
set.go
|
@ -25,7 +25,7 @@ func (s Set) Get(name string) (*Command, bool) {
|
||||||
return nil, false
|
return nil, false
|
||||||
}
|
}
|
||||||
|
|
||||||
func (s Set) MaxWidth() int {
|
func (s Set) MaxNameWidth() int {
|
||||||
max := 0
|
max := 0
|
||||||
for _, f := range s {
|
for _, f := range s {
|
||||||
if w := len(f.Name); w > max {
|
if w := len(f.Name); w > max {
|
||||||
|
|
19
tpl_funcs.go
19
tpl_funcs.go
|
@ -4,14 +4,11 @@ import (
|
||||||
"fmt"
|
"fmt"
|
||||||
"strings"
|
"strings"
|
||||||
"text/template"
|
"text/template"
|
||||||
|
|
||||||
"github.com/iancoleman/strcase"
|
|
||||||
)
|
)
|
||||||
|
|
||||||
var tplFuncs = template.FuncMap{
|
var tplFuncs = template.FuncMap{
|
||||||
"map": tplMap,
|
"map": tplMap,
|
||||||
"cat": tplCat,
|
"cat": tplCat,
|
||||||
"split": tplSplit,
|
|
||||||
"join": tplJoin,
|
"join": tplJoin,
|
||||||
"under": tplUnder,
|
"under": tplUnder,
|
||||||
"varPrefix": tplVarPrefix,
|
"varPrefix": tplVarPrefix,
|
||||||
|
@ -22,8 +19,6 @@ var tplFuncs = template.FuncMap{
|
||||||
"sub": tplSub,
|
"sub": tplSub,
|
||||||
"dec": tplDec,
|
"dec": tplDec,
|
||||||
"mult": tplMult,
|
"mult": tplMult,
|
||||||
"pascal": tplPascal,
|
|
||||||
"camel": tplCamel,
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func tplMap(vals ...any) (map[string]any, error) {
|
func tplMap(vals ...any) (map[string]any, error) {
|
||||||
|
@ -42,16 +37,12 @@ func tplCat(strs ...string) string {
|
||||||
return strings.Join(strs, "")
|
return strings.Join(strs, "")
|
||||||
}
|
}
|
||||||
|
|
||||||
func tplSplit(s string, sep string) []string {
|
|
||||||
return strings.Split(s, sep)
|
|
||||||
}
|
|
||||||
|
|
||||||
func tplJoin(strs []string, sep string) string {
|
func tplJoin(strs []string, sep string) string {
|
||||||
return strings.Join(strs, sep)
|
return strings.Join(strs, sep)
|
||||||
}
|
}
|
||||||
|
|
||||||
func tplUnder(s string) string {
|
func tplUnder(s string) string {
|
||||||
return strcase.ToSnake(s)
|
return strings.ToLower(strings.ReplaceAll(strings.ReplaceAll(s, " ", "_"), "-", "_"))
|
||||||
}
|
}
|
||||||
|
|
||||||
func tplVarPrefix(s string) string {
|
func tplVarPrefix(s string) string {
|
||||||
|
@ -89,11 +80,3 @@ func tplDec(i int) int {
|
||||||
func tplMult(a, b int) int {
|
func tplMult(a, b int) int {
|
||||||
return a * b
|
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