This commit is contained in:
Roberto Hidalgo 2022-12-30 01:00:26 -06:00
parent 4eec8e55a1
commit 831e68c7b7
4 changed files with 85 additions and 24 deletions

View File

@ -11,3 +11,4 @@ trim_trailing_whitespace = true
indent_style = space
indent_size = 2
max_line_length = 120

View File

@ -1,3 +1,61 @@
# Chinampa
Like [`milpa`](https://milpa.dev) but for go programs only.
```go
package main
import (
"os"
"fmt",
"git.rob.mx/nidito/chinampa"
"git.rob.mx/nidito/chinampa/pkg/command"
"git.rob.mx/nidito/chinampa/pkg/runtime"
"github.com/sirupsen/logrus"
)
func main() {
chinampa.Register(&command.Command{
Path: []string{"something"},
Summary: "does something",
Description: "a longer description of how it does stuff",
Arguments: command.Arguments{
{
Name: "argument zero",
Description: "a help text for using argument zero",
Required: true,
},
},
Action: func(cmd *command.Command) error {
someArg := cmd.Arguments[0].ToValue().(string)
return fmt.Errorf("Don't know how to do stuff with %s", someArg)
},
})
logrus.SetFormatter(&logrus.TextFormatter{
DisableLevelTruncation: true,
DisableTimestamp: true,
ForceColors: runtime.ColorEnabled(),
})
if runtime.DebugEnabled() {
logrus.SetLevel(logrus.DebugLevel)
logrus.Debug("Debugging enabled")
}
cfg := chinampa.Config{
Name: "myProgram",
Version: "0.0.0",
Summary: "a short summary of my program",
Description: "a longer text describing what its for",
}
if err := chinampa.Execute(cfg); err != nil {
logrus.Errorf("total failure: %s", err)
os.Exit(2)
}
}
```

View File

@ -1,5 +1,5 @@
{{- if not .HTMLOutput }}
# {{ if and (not .Spec.IsRoot) (not (eq .Command.Name "help")) }}@chinampa@ {{ end }}{{ .Spec.FullName }}{{if eq .Command.Name "help"}} help{{end}}
# {{ .Spec.FullName }}{{if eq .Command.Name "help"}} help{{end}}
{{- else }}
---
description: {{ .Command.Short }}
@ -12,6 +12,22 @@ description: {{ .Command.Short }}
`{{ replace .Command.UseLine " [flags]" "" }}{{if .Command.HasAvailableSubCommands}} SUBCOMMAND{{end}}`
{{ if and .Spec.IsRoot (not (eq .Command.Name "help")) }}
## Description
{{ .Spec.Description }}
{{ end -}}
{{- if .Spec.HasAdditionalHelp }}
{{ .Spec.AdditionalHelp .HTMLOutput }}
{{ end -}}
{{- if (and (not .Spec.IsRoot) .Spec.Description) }}
## Description
{{ if not (eq .Command.Long "") }}{{ .Command.Long }}{{ else }}{{ .Spec.Description }}{{end}}
{{ end }}
{{ if .Command.HasAvailableSubCommands -}}
## Subcommands
@ -19,9 +35,9 @@ description: {{ .Command.Short }}
{{ range .Command.Commands -}}
{{- if (or .IsAvailableCommand (eq .Name "help")) -}}
- {{ if $hh -}}
[﹅{{ .Name }}﹅]({{.Name}})
[`{{ .Name }}`]({{.Name}})
{{- else -}}
﹅{{ .Name }}﹅
`{{ .Name }}`
{{- end }} - {{.Short}}
{{ end }}
{{- end -}}
@ -32,39 +48,23 @@ description: {{ .Command.Short }}
{{ range .Spec.Arguments -}}
- ﹅{{ .Name | toUpper }}{{ if .Variadic}}...{{ end }}﹅{{ if .Required }} _required_{{ end }} - {{ .Description }}
- `{{ .Name | toUpper }}{{ if .Variadic}}...{{ end }}`{{ if .Required }} _required_{{ end }} - {{ .Description }}
{{ end -}}
{{- end -}}
{{ if and .Spec.IsRoot (not (eq .Command.Name "help")) }}
## Description
{{ .Spec.Description }}
{{ end -}}
{{- if .Spec.HasAdditionalHelp }}
{{ .Spec.AdditionalHelp .HTMLOutput }}
{{ end -}}
{{- if .Command.HasAvailableLocalFlags}}
{{- if (and .Command.HasAvailableLocalFlags .Spec.Options) }}
## Options
{{ range $name, $opt := .Spec.Options -}}
- ﹅--{{ $name }}﹅ (_{{$opt.Type}}_): {{ trimSuffix $opt.Description "."}}.{{ if $opt.Default }} Default: _{{ $opt.Default }}_.{{ end }}
- `--{{ $name }}` (_{{$opt.Type}}_): {{ trimSuffix $opt.Description "."}}.{{ if $opt.Default }} Default: _{{ $opt.Default }}_.{{ end }}
{{ end -}}
{{- end -}}
{{- if not .Spec.IsRoot }}
## Description
{{ if not (eq .Command.Long "") }}{{ .Command.Long }}{{ else }}{{ .Spec.Description }}{{end}}
{{ end }}
{{- if .Command.HasAvailableInheritedFlags }}
## Global Options
{{ range $name, $opt := .GlobalOptions -}}
- ﹅--{{ $name }}﹅ (_{{$opt.Type}}_): {{$opt.Description}}.{{ if $opt.Default }} Default: _{{ $opt.Default }}_.{{ end }}
- `--{{ $name }}` (_{{$opt.Type}}_): {{$opt.Description}}.{{ if $opt.Default }} Default: _{{ $opt.Default }}_.{{ end }}
{{ end -}}
{{end}}

View File

@ -137,7 +137,7 @@ func Execute(version string) error {
ccRoot := newCobraRoot(command.Root)
ccRoot.Annotations["version"] = version
ccRoot.CompletionOptions.HiddenDefaultCmd = true
ccRoot.Flags().AddFlagSet(cmdRoot.FlagSet())
ccRoot.PersistentFlags().AddFlagSet(cmdRoot.FlagSet())
for name, opt := range cmdRoot.Options {
if err := ccRoot.RegisterFlagCompletionFunc(name, opt.CompletionFunction); err != nil {
@ -214,6 +214,8 @@ func Execute(version string) error {
container = cc
}
}
cmd.Path = append(cmdRoot.Path, cmd.Path...)
}
cmdRoot.SetCobra(ccRoot)