From 831e68c7b70ab9b7d37a4993fd072c42a138ca6f Mon Sep 17 00:00:00 2001 From: Roberto Hidalgo Date: Fri, 30 Dec 2022 01:00:26 -0600 Subject: [PATCH] sigh --- .editorconfig | 1 + README.md | 58 +++++++++++++++++++++++++++++++++++ internal/constants/help.md | 46 +++++++++++++-------------- internal/registry/registry.go | 4 ++- 4 files changed, 85 insertions(+), 24 deletions(-) diff --git a/.editorconfig b/.editorconfig index 6992289..8359760 100644 --- a/.editorconfig +++ b/.editorconfig @@ -11,3 +11,4 @@ trim_trailing_whitespace = true indent_style = space indent_size = 2 max_line_length = 120 + diff --git a/README.md b/README.md index 3948c89..37c5274 100644 --- a/README.md +++ b/README.md @@ -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) + } +} +``` diff --git a/internal/constants/help.md b/internal/constants/help.md index 6590dd8..1aaed5b 100644 --- a/internal/constants/help.md +++ b/internal/constants/help.md @@ -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}} diff --git a/internal/registry/registry.go b/internal/registry/registry.go index 2030a89..47e678f 100644 --- a/internal/registry/registry.go +++ b/internal/registry/registry.go @@ -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)