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

If no subcommand, how to get args? #42

Closed
kk44 opened this issue Dec 27, 2014 · 8 comments
Closed

If no subcommand, how to get args? #42

kk44 opened this issue Dec 27, 2014 · 8 comments
Labels
area/lib Methods and functions that exist in the cobra library and consumed by users kind/bug A bug in cobra; unintended behavior

Comments

@kk44
Copy link

kk44 commented Dec 27, 2014

example:

package main

import (
    "fmt"
    "github.com/spf13/cobra"
)

func main() {
    var echo = &cobra.Command{
        Use:    "echo [string...]",
        Short:  "echo all strings",
        Long:   "echo string string string string...",
        Run:    func(cmd *cobra.Command, args []string) {
            fmt.Printf("%v\n", args)
        },
    }

    echo.Execute()
}
[bash]# go run echo.go aa bb cc dd

Usage:
  echo [string...] [flags]

 Available Flags:
  -h, --help=false: help for echo
Error: unknown command "aa"
Run 'help' for usage.

echo: invalid command [`aa` `bb` `cc` `dd`]
Run 'echo help' for usage
[bash]# go run echo.go echo aa bb cc dd
[echo aa bb cc dd]
[bash]# go run echo.go
[]

Thanks!

@stephen304
Copy link

I'm having a similar problem. My program is run like so:
./myapp /home/stephen/test.txt

But instead of running the root command with the path as an argument, it complains that the path is an invalid command.I tried using flags, but it seems that would require adding a flag (-p /home/test.txt) to the invocation.

@ghodss
Copy link
Contributor

ghodss commented May 13, 2015

I'm also having this issue. Is having zero subcommands supported? It doesn't seem to be working.

@eparis
Copy link
Collaborator

eparis commented May 13, 2015

It isn't possible. The code treats the root command "special". I don't really love it.

Root commands may only take flags and subcommands. Validation for subcommands is only done for root commands.

Subcommands will be called with an argument instead of failing if an invalid sub-sub-command was intended, but typod.

I'm thinking about what to do here in #86

But any kind of consistency is going to break previous behavior. Personally I'm leaning towards a 'takesNoArgs' flag which defaults to false. Meaning the root command, and all subcommands, would get unknown args.

You would only get subcommand validation and complain if you set takesNoArgs=true

@viktorbenei
Copy link

We recently started to migrate from codegangsta / urfave CLI, and we love Cobra so far! But we have a couple of tools which are primarily designed to run other programs (think about something /usr/bin/env or /usr/bin/ssh, but with a couple of utility commands like init built into the tool), and we can't figure out how we could do that with Cobra.

IMO what @ghodss described (and what's essentially how this works in codegangsta / urfave CLI) at #120 (comment) / #86 (comment) makes sense, or be able to specify a "default" command in any other way.

Is there a workaround for this use case for now?

@viktorbenei
Copy link

viktorbenei commented Jul 1, 2016

I think this issue is also pretty much the same as #15 (as in, solving either would provide a solution / possible workaround for the other one).

@dnephin
Copy link
Contributor

dnephin commented Jul 1, 2016

If you use my PR #284 and use cobra.ArbitraryArgs as the Args value, you get this behaviour.

@viktorbenei
Copy link

@dnephin checked the PR and it's not exactly clear for me: does this work for the Root command too?

@dnephin
Copy link
Contributor

dnephin commented Jul 1, 2016

Yes, it will work for the root command. The old args behaviour is defined in legacyArgs (https://github.com/spf13/cobra/pull/284/files#diff-07ee57a49d1c0b692bd79676cc31dd6fR9). If you set any other Args it wont use the legacyArgs() behaviour.

@n10v n10v added area/lib Methods and functions that exist in the cobra library and consumed by users kind/bug A bug in cobra; unintended behavior labels Apr 22, 2017
@n10v n10v closed this as completed in #284 Jul 23, 2017
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
area/lib Methods and functions that exist in the cobra library and consumed by users kind/bug A bug in cobra; unintended behavior
Projects
None yet
Development

Successfully merging a pull request may close this issue.

7 participants