Commit graph

88 commits

Author SHA1 Message Date
Eric Paris c2d19e9c43 Use the HelpFunc for the --help flag
We were just calling Help() when a user set the --help flag. You could
overwrite how the help subcommand worked with SetHelpFunc, but not now
the --help flag worked.
2015-09-01 14:25:38 -04:00
Eric Paris 27bf24802c Redo HelpFunc() to work things other than the help subcommand
Today the HelpFunc() seemed to be tailor built for the `help`
subcommand. Which has a rather weird purpose as its `Run` needs to
find the actual command we want to get help about.

Instead make the HelpFunc() for a command be about that command,
rather than having it search for some other command...
2015-09-01 14:25:32 -04:00
Eric Paris e15d674de6 Merge pull request #134 from apriendeau/master
#67 creates RunE functions to allow for errors to populate to the top
2015-09-01 11:51:29 -05:00
Eric Paris 99d2c51143 Merge pull request #137 from eparis/fix-help-template
Remove spurious } from the help template
2015-09-01 11:51:12 -05:00
Eric Paris 8f9b8c7cba Remove spurious } from the help template 2015-09-01 12:03:37 -04:00
Eric Paris ae28810f0e Fix the late init help flag 2015-09-01 11:31:23 -04:00
Austin Riendeau 9174039216 #67 creates RunE functions to allow for errors to populate to the top 2015-08-30 19:03:16 -06:00
Eric Paris bab6d45bc6 Initialize the --help flag as late as possible
This should let users overwrite it with something custom. Like the help
sub-command.
2015-08-30 12:10:14 -04:00
Eric Paris e8bd799c1c Display pflag.CommandLine flags as if they were declared on the parent
```go
package main

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

func main() {
	cmd := &cobra.Command{
		Use:   "min",
		Short: "minimal command",
		Run:   func(_ *cobra.Command, _ []string) {},
	}

	pflag.String("oncmdline", "oncmdline", "oncmdline")
	cmd.Execute()
}
```

Is a minimal cobra program.  When --help is displayed without this patch
you only get:

But with the patch --oncmdline is shows under flags.
2015-08-16 22:31:08 -07:00
Eric Paris f453e878d4 Update help template
The template had gotten out of control. It was basically unparsable.
This does a little more work in functions and a little less in the
template. Overall it should be basically the same. It might output the
'additional help topics' in a couple of fewer places, but I doubt people
complain too much...
2015-06-29 20:06:04 -04:00
Eric Paris 9a9d01c9ec Better error message
Calling `cobra-test echo times one two turkey` where `one` and `two` are
valid arguments but `turkey` is not now results in.

Error: invalid argument "turkey" for "cobra-test echo times"
Run 'cobra-test echo times --help' for usage.
2015-06-29 17:09:41 -04:00
Eric Paris 0a7a850026 Make error handling more obvious
Again, the code looks a little more like a middle-schooler's code. But
that just makes it easier to understand and maintain.
2015-06-29 15:45:01 -04:00
Eric Paris 6f735782e0 Remove unused ErrHelp check
Inside Command.Execute() we were checking for pflag.ErrHelp. But
Command.execute() never returns that value. It just complicates the code
and isn't used.
2015-06-29 15:45:01 -04:00
Eric Paris 0a1a8e2e92 Remove (unused) cmdErrorBuf
Nothing was using it any more...
2015-06-29 15:45:01 -04:00
Eric Paris d0bb3e33e6 rework Find() to make it more obvious what is happening
We had lots of quirky if statements like `commandFound.Name() ==
c.Name() && len(stripFlags(args, c)) > 0 && commandFound.Name() !=
args[0]` which embeed all sorts of artifacts which are hard to parse. So
in general, just try to simplify and make stuff readable.
2015-06-29 15:44:56 -04:00
Eric Paris 07ad27d239 Handle grand children with the same name as the root
This fixes a problem where if you had a root command and a grand child
with the same name, the parser would break and would not run the
grandchild. The code was special casing if the immediate child had the
same name, but didn't handle grand-children
2015-06-22 17:54:08 -04:00
Simone Gotti fb86c5c559 Correctly print the unknown command name
by now, if someone calls: `program --validflag unknowncommand` the
output will be:

```
Error: unknown command "--validflag"
Run 'program help' for usage.
```

This patch strips out flags so the unknown command is printed:

```
Error: unknown command "unknowncommand"
Run 'program help' for usage.
```
2015-06-22 11:02:06 +02:00
Anastasis Andronidis d910a04b50 Add Global Normalization Function
[close #110]
2015-05-19 10:23:16 -04:00
Sam Ghods e0f326dabc Fix root command without subcommands but with arguments
[close #108]
2015-05-19 10:22:07 -04:00
Anastasis Andronidis 42e6ce397f Fixed Persistent-Run function propagation 2015-05-04 03:42:55 +02:00
Eric Paris fad5931693 Add docs, tests, and bit of rearrainging... 2015-04-30 14:08:47 -04:00
Alexander Thaller fbce60cc56 added persistent pre/post commands. 2015-04-30 12:45:23 -04:00
Alexander Thaller 2df64026ba added pre and post run hooks. 2015-04-30 12:44:01 -04:00
Eric Paris 9cf0f3737d Merge pull request #90 from eparis/deprecated-subcommands
Deprecated subcommands
2015-04-30 10:22:48 -05:00
Eric Paris 7c7837e882 Merge pull request #85 from eparis/more-simplification
More simplification
2015-04-30 10:21:12 -05:00
Eric Paris 2d5fab043c Merge pull request #75 from eparis/CommandLine
Make flags declared outside spf13.pflag persistent flags on the parent
2015-04-30 10:20:35 -05:00
Eric Paris c3e48f996d Deprecated subcommands
They should still work, but shouldn't show up in help or usage output
2015-04-29 13:08:20 -04:00
deads2k 36aee64abe prevent removal of valid arguments 2015-04-28 07:58:26 -04:00
Eric Paris c746d30ef0 Merge pull request #69 from eparis/command-annotations
Bash Autocompletion Generator
2015-04-10 23:20:17 -05:00
Eric Paris 9b2e6822e5 Add bash autocompletion generator
Given a (potentially annotated) cobra command you can generate a bash
completion script.
2015-04-07 20:13:49 -04:00
Eric Paris 86a16864ea Add tests about additional help topics 2015-04-07 16:17:22 -04:00
Eric Paris f576d29563 Merge pull request #82 from bep/mousetrap
Add mousetrap for Windows users
2015-04-06 15:49:03 -05:00
Eric Paris bbc51773c3 Merge pull request #77 from eparis/additional-help-topics
Additional help topics
2015-04-06 15:04:48 -05:00
Eric Paris 794a362808 Remove specail casing in Execute()
We had some stuff that created a new empty []string if args was already
and empty string (why?)

We had some stuff that called Help() if it wasn't runnable (but
.execute() already does that)

Just remove the special case stuff.
2015-04-06 16:02:09 -04:00
Eric Paris b9e25fa4a0 Slight formatting change, to make next commit more readable 2015-04-06 16:02:08 -04:00
Eric Paris bd0f8a846e Stop special casing runnable root commands
The special case code to handle a runnable root command had some
problems.  It was noticed that if you created a runnable root and a
subcommand.  And the subcommand was then executed with both a valid and
invalid flag, the error message was about the valid flag being invalid.
For example

./command subcommand --goodflag=10 --badflag=10

Would fail and tell you that --goodflag was an invalid flag. Instead if
we just do away with the special Command.execute() for the root command
the parser for subcommand is what prints the error and it gets it
right...
2015-04-06 15:42:03 -04:00
Adam Mckaig 2c370cd936 Fix redundant error for unknown root command 2015-04-03 01:07:34 -04:00
bep beda1945ad Add mousetrap for Windows users
Fixes #80
2015-04-01 21:14:40 +02:00
Eric Paris fd10548830 Fix additional help topics template
The additional help topics were really hard to ever get to show.  The
required conditionals were difficult to meet and did not seem to really
be logical.

Problems I see:
1) the top level command could never have additional topics.
2) you must have at least one sibling command AND one subcommand
3) we had the AND above, but then test both conditionals a second time
4) if the sub command was runnable we wouldn't print anything
5) if the sibling commands were not runnable we wouldn't print anything
4+5) it's possible that we printed "Additional help topics:" with nothing following it
6) We always printed ourselves as a sibling in the additional info

Whew, I think I fixed all of those!  Again, using
https://github.com/eparis/readable-golang-template
I'm actually able to visualize the template and see this craziness.

The conditionals BEFORE this change:

{{if .HasParent}}
        {{if and (gt .Commands 0) (gt .Parent.Commands 1) }}
                Additional help topics:
                {{if gt .Commands 0 }}
                        {{range .Commands}}
                                {{if not .Runnable}}
                                        {{rpad .CommandPath .CommandPathPadding}} {{.Short}}
                                {{end}}
                        {{end}}
                {{end}}
                {{if gt .Parent.Commands 1 }}
                        {{range .Parent.Commands}}
                                {{if .Runnable}}
                                        {{if not (eq .Name $cmd.Name) }}
                                        {{end}}
                                        {{rpad .CommandPath .CommandPathPadding}} {{.Short}}
                                {{end}}
                        {{end}}
                {{end}}
        {{end}}
{{end}}

The conditionals AFTER this change:

{{if or (.HasHelpSubCommands) (.HasRunnableSiblings)}}
        Additional help topics:
        {{if .HasHelpSubCommands}}
                {{range .Commands}}
                        {{if not .Runnable}}
                                {{rpad .CommandPath .CommandPathPadding}} {{.Short}}
                        {{end}}
                {{end}}
        {{end}}
        {{if .HasRunnableSiblings }}
                {{range .Parent.Commands}}
                        {{if .Runnable}}
                                {{if not (eq .Name $cmd.Name) }}
                                        {{rpad .CommandPath .CommandPathPadding}} {{.Short}}
                                {{end}}
                        {{end}}
                {{end}}
        {{end}}
{{end}}
2015-03-20 14:55:05 -04:00
Eric Paris c64442a487 fix usage template conditional
I wrote https://github.com/eparis/readable-golang-template which
converts golang templates into something structured around the
conditionals.  Obviously you can't just USE the output, but you can SEE
the problems.  In this case the output shows something like:

{{if .HasParent}}
        {{if and (gt .Commands 0) (gt .Parent.Commands 1) }}
                Additional help topics:
                {{if gt .Commands 0 }}
                        {{range .Commands}}
                                {{if not .Runnable}}
                                        {{rpad .CommandPath .CommandPathPadding}} {{.Short}}
                                {{end}}
                        {{end}}
                {{end}}
                {{if gt .Parent.Commands 1 }}
                        {{range .Parent.Commands}}
                                {{if .Runnable}}
                                        {{if not (eq .Name $cmd.Name) }}
                                        {{end}}
                                        {{rpad .CommandPath .CommandPathPadding}} {{.Short}}
                                {{end}}
                        {{end}}
                {{end}}
        {{end}}
{{end}}

We have a completely unused "{{if not (eq .Name $cmd.Name) }}"

Move the {{end}} after the {{rpad...}}
2015-03-20 13:59:32 -04:00
Edgard Castro 384c059f4b Help uses Short message if Long is not available 2015-03-19 20:29:37 +01:00
Eric Paris f81b784738 Make flags declared outside spf13.pflag persistent flags on the parent
Some projects pick up flags from other projects they include.  A great
example would be projects that use glog and thus get all of those flags.
Kubernetes, for example, merges those flags manually into its commands.

This was reported in https://github.com/spf13/cobra/issues/44

What this patch does is merge those flags into the PersistentFlags on
the highest parent.  This allows kubernetes to stop having to merge
things themselves...
2015-03-19 15:08:12 -04:00
Jason Moiron e784a59910 fix missing newline in help output introduced in bf480fe628 2015-03-18 21:11:55 +01:00
Eric Paris 6de96b849c Default usage output to stdout
If the command has not set an output explicitly everything will go to
stderr.  This makes a lot of sense, but is a huge PITA for people who
want to be able to grep the help output.  It is very common for users to
want to do

command --help | grep flag

This patch fixes that by default help output (but not error output like
an invalid command) to stdout instead of defaulting to stderr.
2015-03-18 16:43:00 +01:00
Brendan Burns 93278e2f35 Add mergePersistentFlags in strip flags since we now look at the flag set. 2015-03-16 16:15:32 -07:00
Brendan Burns 9e7273d546 Remove some wonky error handling, as upstream seems correct now.
Also, it's buggy for nested commands.
2015-03-13 13:36:21 -04:00
Étienne Vallette d'Osia f479c924b8 Add Command's RemoveCommand method
This method removes children commands of an existing command.

This allows to build CLI clients that can be extended by 3rd party tools,
for instance by adding commands _and replacing the “version” command_.

For now the 1st defined command will be executed, so it is not possible
to override an existing command. But anyway, deleting old command then
adding a new one is the ultimate way to be certain there is no
confusion.
2015-03-12 22:44:11 -04:00
Brendan Burns 2cb625eda3 Remove some wonky error handling, as upstream seems correct now.
Also, it's buggy for nested commands.
2015-03-12 22:37:48 -04:00
Brendan Burns bbdea35c49 Fix stripFlags to be more intelligent about what it parses. 2015-03-12 22:36:49 -04:00
Jeff Lowdermilk 5c9146990b Explicitly support local flags overwriting persistent/inherited flags
The current (desired) behavior when a Command specifies a flag that
has the same name as a persistent/inherited flag, is that the local
definition takes precedence. This change updates the various
Flag subset functions to respect that behavior:
* LocalFlags: now returns only the set of flags and persistent flags
  attached to the Command itself.
* InheritedFlags: now returns only the set of persistent flags inherited
  from the Command's parent(s), excluding any that are overwritten by a
  local flag.
* NonInheritedFlags: changed to an alias of LocalFlags.
* AllPersistentFlags: removed as not very useful; it returned the set
  of all persistent flags attached to the Command and its parent(s).

Default UsageTemplate updated to use LocalFlags and InheritedFlags
2015-03-12 16:41:00 -07:00