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

How to add a required argument? #346

Closed
carolynvs opened this issue Sep 23, 2016 · 2 comments
Closed

How to add a required argument? #346

carolynvs opened this issue Sep 23, 2016 · 2 comments

Comments

@carolynvs
Copy link

I am migrating my app over from another cli library (kingpin), and one of the things they let me do is document positional arguments, e.g. cmd.Arg("name", "name of the thing").Required().StringVar(&name). I am having trouble understanding how to migrate that over. So far the best I have come up with is this:

var createCmd = &cobra.Command{
    Use:   "create <name>",
    Short: "Create a thing",
    Long:  "Create a thing",
    RunE: func(cmd *cobra.Command, args []string) error {
        if len(args) < 1 {
            return errors.New("The name argument is required")
        }
        name = args[0]
        // create a thing
    }

Basically I am documenting the arg in the Use string and then parsing the arg in RunE. This feels a bit awkward, is there something built-in that I am overlooking?

Thanks!

@eparis
Copy link
Collaborator

eparis commented Sep 23, 2016

Doing it manually in a (Pre)RunE is sadly the best you can likely do. Sorry.

@carolynvs
Copy link
Author

No need to apologize! I was just looking for the recommended way to get stuff done. 😄 Thank you for the quick reply!

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

2 participants