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

Documenting a positional argument and suppressing [flags] when appropriate #395

Closed
kofalt opened this issue Feb 27, 2017 · 12 comments
Closed

Comments

@kofalt
Copy link

kofalt commented Feb 27, 2017

The readme references an example command that has a positional argument:

git clone URL --bare

To document that in Hugo, here's what I would start with:

cloneCmd := &cobra.Command{
	Use:   "clone",
	Short: "Clone a repository into a new directory",
	Run: func(cmd *cobra.Command, args []string) {
		// stub
	},
}

Which produces:

$ example clone --help
Clone a repository into a new directory

Usage:
  example clone [flags]

This stub could use a few improvements:

  1. The positional parameter URL is not specified in the help.
  2. I'd have to manually check the args array for URL, invent some error messages, etc.
  3. The documentation specifies [flags], but there are no flags!

Any ideas on how I could tackle these improvements?

The first one could probably be fixed by setting Use to "clone [URL]", but the third one is the real pickle. As far as I can tell I'd have to copy and modify the default template, at which point I'm detached from the official version and I'd have to maintain my "fork" of the template, which I'd rather avoid.

@eparis
Copy link
Collaborator

eparis commented Feb 27, 2017

Fixing the default template to not say [flags] if there are no flags fixes the second problem for everyone, no? It doesn't seem like something that wouldn't be universal...

@kofalt
Copy link
Author

kofalt commented Feb 27, 2017

I think you meant the third problem, but yes! That would work.

Looking at the default template I linked, my guess is using HasAvailableLocalFlags instead of HasAvailableFlags would work. Might cause other issues though, I'm not sure.

@eparis
Copy link
Collaborator

eparis commented Feb 27, 2017

Yes, the third problem.

@dnephin
Copy link
Contributor

dnephin commented Feb 27, 2017

#284 is one approach to the second problem

@kofalt
Copy link
Author

kofalt commented Feb 27, 2017

That looks great! I would prefer to use the main source, but it looks like that PR has been hanging out for awhile. Would you mind merging upstream master into your fork again? If not, no worries, I can maintain my own fork if necessary.

This branch is 16 commits ahead, 6 commits behind spf13:master.

@dnephin
Copy link
Contributor

dnephin commented Feb 28, 2017

I've rebased it with master

@n10v
Copy link
Collaborator

n10v commented Apr 12, 2017

  1. You can specify it in Use like in my project.
  2. There is a help flag, but there is a bug with showing help flag in usage. Refactor flags mechanisms #414 fixes it.

@kofalt
Copy link
Author

kofalt commented Apr 12, 2017

Nice. It sounds like the current solutions to the three problems mentioned in the ticket are:

  1. Write the positional argument in the Use field, or use the fork from Support validation of positional arguments #284
  2. Manually parse + validate the positional argument, or wait for / use Support validation of positional arguments #284
  3. Wait for Refactor flags mechanisms #414 and see how that affects things, or possibly develop the fix that @eparis mentioned.

I think once the behavior on master satisfies these points, I'll add a comment with a new cobra.Command example and close the ticket 🙂

@n10v
Copy link
Collaborator

n10v commented Apr 12, 2017

#284 will not solve first problem. Only second

@n10v
Copy link
Collaborator

n10v commented Oct 30, 2017

@kofalt, is your problem solved?

@n10v n10v mentioned this issue Oct 30, 2017
@kofalt
Copy link
Author

kofalt commented Oct 30, 2017

Yes! Give or take. Thank you for reminding me.

The first problem is handled by manually typing a positional argument in the usage string, the second problem is handled by #284 landing, and the third problem is small enough to be ignored.

Thanks to everyone who helped. Here is a complete solution for anyone who was following along:

package main

import (
	. "fmt"
	"os"

	"github.com/spf13/cobra"
)

func main() {
	RootCmd := &cobra.Command{
		Use:   "example",
		Short: "An example cobra command",
	}

	var bare bool
	CloneCmd := &cobra.Command{
		Use:   "clone [URL]",
		Short: "Clone a repository into a new directory",

		Args:  cobra.ExactArgs(1),

		Run: func(cmd *cobra.Command, args []string) {

			// This is guaranteed to work, due to ExactArgs above
			url := args[0]

			Println("Cloning from", url, "with bare =", bare)
		},
	}
	CloneCmd.Flags().BoolVar(&bare, "bare", false, "Make a bare Git repository.")

	RootCmd.AddCommand(CloneCmd)

	err := RootCmd.Execute()
	if err != nil {
		Println(err)
		os.Exit(1)
	}
}

In action:

$ example clone --help
Clone a repository into a new directory

Usage:
  example clone [URL] [flags]

Flags:
      --bare   Make a bare Git repository.
  -h, --help   help for clone


$ example clone github --bare
Cloning from github with bare = true

@kofalt kofalt closed this as completed Oct 30, 2017
@Dominik-K
Copy link

To 1.) Documentation of positional arguments in an automated manner is tracked in #378

lukehoban pushed a commit to pulumi/pulumi that referenced this issue Mar 26, 2019
Documenting positional arguments must be done manually, still: spf13/cobra#395
Samyak2 added a commit to Samyak2/guntainer that referenced this issue Sep 17, 2021
 - updated README getting started section to work with new cobra CLI
 - updated subcommand use section, short descriptions and long
   descriptions to provide more information
    - particularly, they now show the positional arguments in "Usage"
    - added descriptions of the positional arguments in the long description
    - these are kind of inelegant, see these issues in cobra for why
      this had to be done
        - spf13/cobra#395
        - spf13/cobra#378
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

5 participants