Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Set aliases using usage template #1045

Closed
navidshaikh opened this issue Oct 6, 2020 · 6 comments · Fixed by #1055
Closed

Set aliases using usage template #1045

navidshaikh opened this issue Oct 6, 2020 · 6 comments · Fixed by #1055
Labels
kind/feature New feature or request
Milestone

Comments

@navidshaikh
Copy link
Collaborator

Currently we've to manually document the aliases for the commands and flags, that should've been added by auto doc utility from cobra.
ref: #1041

@navidshaikh navidshaikh added the kind/feature New feature or request label Oct 6, 2020
@danielhelfand
Copy link
Contributor

danielhelfand commented Oct 7, 2020

Is the goal here for just docs or to include in help command as well?

Something like below could very easily be added for help:

List services (alias: 'ls')

Examples:

  # List all services
  kn service list

  # List all services in JSON output format
  kn service list -o json

  # List service 'web'
  kn service list web


Options:
  -A, --all-namespaces                If present, list the requested object(s) across all namespaces. Namespace in current context is ignored even if specified with --namespace.
      --allow-missing-template-keys   If true, ignore any errors in templates when a field or map key is missing in the template. Only applies to golang and jsonpath output formats. (default true)
  -n, --namespace string              Specify the namespace to operate in.
      --no-headers                    When using the default output format, don't print headers (default: print headers).
  -o, --output string                 Output format. One of: json|yaml|name|go-template|go-template-file|template|templatefile|jsonpath|jsonpath-file.
      --template string               Template string or path to template file to use when -o=go-template, -o=go-template-file. The template format is golang templates
                                      [http://golang.org/pkg/text/template/#pkg-overview].

Usage:
  kn service list [options]

Aliases: 
  list, ls

Use "kn options" for a list of global command-line options (applies to all commands).

While updating generate-docs.go might require relying less on "github.com/spf13/cobra/doc" default doc generation approaches and implementing a custom approach, such as what tkn has done.

@navidshaikh
Copy link
Collaborator Author

I think having

Aliases: 
  list, ls

section for help text should do, I'd remove the inline hint of alias from the short description, in favor of former.

@navidshaikh
Copy link
Collaborator Author

looks like tkn is controlling the help text using usage template https://github.com/tektoncd/cli/blob/master/pkg/cmd/root.go#L43

@danielhelfand
Copy link
Contributor

Yes, I put together something like below for kn:

	sectionAliases = `{{ if ne (len .Aliases) 0}}Aliases: 
  {{.NameAndAliases}}

{{end}}`

	// sectionTipsHelp is the help template section that displays the '--help' hint.
	sectionTipsHelp = `{{if .HasSubCommands}}Use "{{rootCmdName}} <command> --help" for more information about a given command.
{{end}}`

	// sectionTipsGlobalOptions is the help template section that displays the 'options' hint for displaying global flags.
	sectionTipsGlobalOptions = `Use "{{rootCmdName}} options" for a list of global command-line options (applies to all commands).`
)

// usageTemplate if the template for 'usage' used by most commands.
func usageTemplate() string {
	sections := []string{
		"\n\n",
		sectionExamples,
		sectionCommandGroups,
		sectionSubCommands,
		sectionPlugins,
		sectionFlags,
		sectionUsage,
		sectionAliases,
		sectionTipsHelp,
		sectionTipsGlobalOptions,
	}
	return strings.TrimRightFunc(strings.Join(sections, ""), unicode.IsSpace) + "\n"
}

Can put in a pr if it makes sense.

@navidshaikh
Copy link
Collaborator Author

nice! How about re-ordering of the subsections as:

"\n\n"
sectionUsage,
sectionAliases,
sectionExamples,
[..]

@danielhelfand
Copy link
Contributor

Sure, sounds good. Let me rearrange and I'll submit something.

@navidshaikh navidshaikh added this to the v0.19.0 milestone Oct 9, 2020
@navidshaikh navidshaikh changed the title Investigate auto doc for aliases Set aliases using usage template Oct 9, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
kind/feature New feature or request
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants