Commit graph

255 commits

Author SHA1 Message Date
Albert Nigmatzianov
458d79748e Add merging of CommandLine flags to Root command 2017-04-20 15:38:50 +02:00
Albert Nigmatzianov
3d89ed4908 Add only missing persistent flags of parents in mergePersistentFlags
As persistent flags of parents can only be added, we don't need to always
check them every time, so make updateParentsPflags return only added flags.

Performance improvement:
benchmark                     old ns/op     new ns/op     delta
BenchmarkInheritedFlags-4     5595          4412          -21.14%
BenchmarkLocalFlags-4         3235          2667          -17.56%

benchmark                     old allocs     new allocs     delta
BenchmarkInheritedFlags-4     39             24             -38.46%
BenchmarkLocalFlags-4         21             15             -28.57%

benchmark                     old bytes     new bytes     delta
BenchmarkInheritedFlags-4     1000          600           -40.00%
BenchmarkLocalFlags-4         544           408           -25.00%
2017-04-20 15:38:50 +02:00
Albert Nigmatzianov
6202b5942b Refactor flags mechanisms
I think It's more obvious now to understand the inheritance of flags.

Fix #403
Fix #404

Performance improvements:
benchmark                     old ns/op     new ns/op     delta
BenchmarkInheritedFlags-4     6536          5595          -14.40%
BenchmarkLocalFlags-4         3193          3235          +1.32%

benchmark                     old allocs     new allocs     delta
BenchmarkInheritedFlags-4     49             39             -20.41%
BenchmarkLocalFlags-4         23             21             -8.70%

benchmark                     old bytes     new bytes     delta
BenchmarkInheritedFlags-4     2040          1000          -50.98%
BenchmarkLocalFlags-4         1008          544           -46.03%
2017-04-20 15:38:50 +02:00
Albert Nigmatzianov
b655df6ce8 Get rid of flagErrorBuf (#417)
Fix #27
2017-04-19 09:23:43 -04:00
Albert Nigmatzianov
9350054794 Keep only one newline between Aliases and Flags in help (#410)
Fix #409
2017-04-17 18:55:55 +02:00
Tamir Duberstein
7aeaa2cce6 Avoid storing pointer to nil (#411)
* Fix shellcheck

Before this change:

	In - line 204:
	    declare -F $next_command >/dev/null && $next_command
	               ^-- SC2086: Double quote to prevent globbing and word splitting.

	--- FAIL: TestBashCompletions (0.34s)
		bash_completions_test.go:138: shellcheck failed: exit status 1

* Avoid storing pointer to nil

Before this change, the new test fails with:

	--- FAIL: TestSetOutput (0.00s)
		command_test.go:198: expected setting output to nil to revert back to stdout, got <nil>
2017-04-02 10:14:34 -04:00
Tamir Duberstein
7be4beda01 remove help command before adding it (#399)
This fixes an issue where each Execute call grows the number of times
`help` appears in the help command by 1.
2017-03-14 12:12:53 -05:00
Albert Nigmatzianov
0960ff7fa9 Rename IsHelpCommand to IsAdditionalHelpTopicCommand (#398)
Fixes #393
2017-03-09 10:37:15 -05:00
Albert Nigmatzianov
fcd0c5a1df Some fixes related to help command (#394)
* Add dots in the end of docs

* Make UsageTemplate show help command

Fix #366

* Fix output of help if it's invalid command
2017-02-28 14:17:48 -05:00
Eric Paris
92ea23a837 Fix subcommand locations with NoOptDefValue flags (#391)
If one ran a command like

./root --boolFlag subcmd1 subcmd2

Thing worked fine. The code recognized that --boolFlag followed by a
space meant the next word was not the argument to --boolFlag. But other
flag types with a NoOptDefValue (like a Count flag) would not ignore the
"argument".  On a command like:

./root --countflag subcmd1 subcmd2

The processor, when looking for a subcommand, would first throw out the
`--countflag subcmd1` and then look for subcmd2 under root.

The fix is to ignore the next word after any NoOptDefVal flag, not just
boolean flags.
2017-02-23 08:12:51 -05:00
Di Xu
b5d8e8f46a cache command name (#390) 2017-02-09 11:54:17 -05:00
Daniel Nephin
99600f6874 Add a tags field for user defined tags on a Command. (#359)
Signed-off-by: Daniel Nephin <dnephin@gmail.com>
2017-02-07 16:20:48 -05:00
Oleg Atamanenko
0f056af21f Added godocs to public methods. (#386)
* Added godocs to public methods.

* Fix gofmt formatting.
2017-01-24 11:30:45 -05:00
bogem
011f19e8ec Fix condition in HelpFunc
I think, it's more obvious, what does this method
2016-10-14 20:34:35 +02:00
Eric Paris
ec2fe78599 Merge pull request #286 from dnephin/merge_flags_before_adding_help
Merge persistent flags before checking for a help flag
2016-10-10 11:14:18 -04:00
Daniel Nephin
67feb8173c Add support for setting a function to handle flag parsing errors.
The default pflag error is to only print the bad flag. This enables an application
to include a usage message or other details about the error.

Signed-off-by: Daniel Nephin <dnephin@gmail.com>
2016-10-10 10:56:48 -04:00
bogem
5da98880f3 Edit documentation 2016-08-30 22:14:27 +05:00
bogem
6e17f4e2c5 Fix typos 2016-08-20 12:04:53 +05:00
Fabiano Franz
ded646f978 Restore Help() and Usage() for backwards compatibility 2016-07-20 12:02:56 -03:00
Fabiano Franz
d6bf4ef243 Don't expose Usage() and Help() - our interfaces are UsageFunc, HelpFunc and UsageString 2016-07-15 17:18:59 -03:00
Fabiano Franz
20217d8f5e Expose OutOrStdout and OutOrStderr and don't make assumptions if output is not set 2016-07-15 17:18:47 -03:00
Fabiano Franz
77530f537e Help command must use Stdout instead of Stderr 2016-07-06 12:51:57 -03:00
Daniel Nephin
7faa7fcdd2 Merge persistent flags before checking for a help flag.
Signed-off-by: Daniel Nephin <dnephin@gmail.com>
2016-06-20 14:58:44 -04:00
Andrey Kurilin
ab97c7a63a Sort commands by their names
The slice of commands are sorted now automatically while Commands are called.
To turn off this feature, EnableCommandSorting variable is added.
2016-06-15 17:27:34 +03:00
Eric Paris
c21ee9da52 Merge pull request #288 from eparis/flags-vs-commands
Do not display bash commands if local flag specified
2016-06-07 08:37:00 -04:00
Mark Bates
dc6e9ece6f added a flag to disable flags 2016-06-03 20:25:52 -04:00
Eric Paris
7bf964e5b6 Do not show subcommands in bash completion if a local flag was specified
If a user specifies a flag to a command which doesn't make sense to a
subcommand do not show subcommands as a suggestion.

This also changes things to show both 'required flags' and 'commands'
instead of only 'required flags'
2016-06-03 12:44:58 -04:00
Kevin Peng
6096009e8d Fixed Small Comment Typo
small error in function description
2016-05-17 13:17:01 -04:00
John Schnake
e14e47b7a9 Issue #267: Ensure hidden flags/subcommands do not change help
If a command has one flag which is hidden, it should not, for
instance, show the `Flags: ` heading.  Likewise there are other
items in the help template which should respect hidden/deprecated
state.
2016-04-27 13:33:23 -04:00
Eric Paris
60267ae24d Merge pull request #257 from sttts/sttts-noun-aliases
Add noun aliases
2016-04-02 09:29:34 -05:00
Dr. Stefan Schimanski
1a0a490d76 Remove plural handling and add aliases for nouns in completion 2016-04-02 12:39:19 +02:00
Ian Walter
a0bd6c17b3 Fixing golint warnings
* Moving final return outside of if-else
* Removing type declarations that Go can infer from values
* Cleaning up some existing comments
* Changing snake_case variables to camelCase
2016-03-31 09:53:34 -04:00
Fabiano Franz
16b7ba55fb Must only add "[flags]" to the end of usage if not yet present 2016-03-15 18:22:20 -03:00
Jonathan Reyna
1ef0913976 Use nil instead of len for arg check.
This honors the SetArgs() call
2016-02-08 17:04:25 -05:00
Sascha Holzleiter
2bd8a730ae always show help if requested, even if SilenceErrors is enabled 2015-12-24 08:52:49 -05:00
Eric Paris
73665614cb Merge pull request #197 from magiconair/issue-195-mousetrap-win-only
Issue #195: Compile mousetrap only on Windows
2015-12-16 19:04:05 -05:00
Nan Xiao
d95d1b86bb Update command.go
Fix typo in AddCommand method.
2015-12-16 16:14:10 +08:00
Frank Schroeder
193b182195 Issue #195: Compile mousetrap only on Windows
* Create command_win.go and command_notwin.go for windows only code
* Move call to mousetrap hook into separate preExecHook() function
2015-11-24 01:52:16 +01:00
Austin Riendeau
3f605993a9 Creates the ExecuteC function while maintaining Execute signature 2015-11-17 13:01:58 -07:00
Austin Riendeau
5144a3aa19 add a flag to disable autogen tag in man and markdown generators
Inherits from parent commands all the way to root
2015-11-08 09:58:38 -05:00
Anthony Fok
e5762054c9 Limit the verbose test workaround to "cobra.test" only
so that full path to the executable or a renamed executable
parses command-line arguments correctly as before.

Special thanks to @apriendeau for discovering "go test -v" failing
and for providing the initial workaround, see #155 and subsequent
discussions.
2015-11-07 23:21:50 -07:00
Austin Riendeau
62e859a9ed fixes verbose tests by checking if the first arg belongs to cobra 2015-11-07 11:42:13 -07:00
James Bowes
871b0edae2 Ensure the usage command is printed only once
Reverse a swap in logic introduced in #169 that would cause the usage
output to be printed twice.

Fixes #171
2015-10-31 10:22:19 -03:00
Austin Riendeau
2244c3923a [silence-errors]: adds comments around code 2015-10-26 17:42:06 -06:00
Austin Riendeau
4729b374ae [silence-errors]: adds Silence Usage 2015-10-26 17:38:08 -06:00
Austin Riendeau
03aabcda72 adds inheritance for SilenceErrors 2015-10-25 21:17:39 -06:00
Austin Riendeau
f6e1a2df92 [silence-errors]: -m adds a way for errors to silenced down the stack 2015-10-25 19:58:53 -06:00
siddharthist
c4d6dbe059 Added documentation for ArgsLenAtDash 2015-10-13 15:41:57 -07:00
Viktor Stanchev
72ee37f33e fix bug with PersistentPreRunE 2015-09-30 00:09:17 -07:00
Fabiano Franz
a83a172122 Improve suggestions - add explicit SuggestFor attribute 2015-09-25 12:07:38 -03:00
Fabiano Franz
3afa4db2c6 Improve suggestions - prefix will match 2015-09-25 11:41:47 -03:00
Eric Paris
edde52e321 Provide the length of Args when a -- is found in commandline
This allows a program to know what args came before the -- and what args
came after.
2015-09-16 12:31:41 -04:00
Eric Paris
6d70311770 Merge pull request #149 from fabianofranz/siblings
Allows command suggestions along with "unknown command" errors
2015-09-13 22:44:46 -05:00
Fabiano Franz
b4087da7eb Allows command suggestions along with "unknown command" errors 2015-09-13 23:35:10 -03:00
Eric Paris
632e090805 If nothing after flags: sting print a newline 2015-09-12 00:21:31 -04:00
Eric Paris
eeec8e3ce3 Merge pull request #153 from eparis/remove-useless-assignment
UsageTemple: remove useless variable assignment
2015-09-11 18:00:59 -05:00
Eric Paris
d96b4f7741 Merge pull request #152 from eparis/no-newline-after-flags
No newline after Flags in usage
2015-09-11 18:00:50 -05:00
Eric Paris
1ac08d9d94 UsageTemple: remove useless variable assignment 2015-09-11 18:52:47 -04:00
Eric Paris
85bb343e9c Merge pull request #151 from eparis/no-newline-without-short
No leading newline if Short and Long both unset
2015-09-11 17:46:37 -05:00
Eric Paris
8af2b2b89f No newline after Flags in usage
The flags usage template from pflags has a trailing \n. We need to
include a newline in case there are no flags in our template. This will
trim the newline from the end of the flags from pflag and we can do it
right outselves.
2015-09-11 18:45:28 -04:00
Eric Paris
717619095e No leading newline if Short and Long both unset
Before --help would start
```

Usage:
  command [flags]
  [...]
```

After --help will show
```
Usage:
  command [flags]
  [...]
```
2015-09-11 18:28:40 -04:00
Eric Paris
24b4b01ffa Do not cound "help" as "Available" 2015-09-11 18:25:28 -04:00
Tamir Duberstein
5f83d08ae0 trim trailing whitespace 2015-09-11 13:25:15 -04:00
Eric Paris
3eac1434ef Update IsAvailableCommand checks
This slightly changes IsAvailableCommand in that a non-runnable command
with a runnable subcommand is now 'Available'

We also use IsAvailableCommand in the rest of the codebase instead of
half kinda sorta doing it incorrectly other places.
2015-09-08 22:48:12 -04:00
Steve Domino
6d2f1d2fa7 Ability to hide commands from usage/help text
Added the ability to have hidden commands that cobra will still run as intended, however they won't show up in any usage/help text

adding internal field to command

private is a better name

hiding private commands in default help/usage

opting for 'hidden' over 'private'

updating all 'help command' checks to exclude hidden commands

updating how commands are displayed in usage/help text by updating/adding some methods. added tests for hidden/deprecated commands

making command hidden when testing hidden command execution

test now leverage the included suite and are much less custom. also removed deprecation tests, once I discovered them in cobra_test.go

updating hidden command test to be more reliable

removing unnecessary () when checking len(c.Deprecated)

updating command comments to be godoc friendly
2015-09-08 17:01:13 -06:00
Eric Paris
16b4170663 Merge pull request #138 from eparis/cleanup-helpfunc
Allow SetHelpFunc to overwrite the --help flag instead of just the help subcommand
2015-09-03 15:53:20 -05:00
Alexander Guy
823f6890b7 RunE commands should be considered runnable. 2015-09-02 10:59:31 -07:00
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
Eric Paris
f8e1ec56bd new NonInheritedFlags() which give all flags which were not persisted from a parent command 2015-02-17 13:52:53 -05:00
Eric Paris
b96dd75141 new InheritedFlags() command to tell all flags which persisted from a parent 2015-02-17 13:52:53 -05:00
Eric Paris
bf480fe628 New "example" section of commands
We have a long and short description.  This adds an "Example" section.
Which can used to create better docs than putting it all in Long.
2015-02-17 13:50:52 -05:00
Masahiro Sano
a16cb24999 help displays command names instead of usage in Available Commands 2015-02-17 13:50:09 -05:00
Kartik Singhal
c36f627ba6 Minor English correction 2015-02-11 17:17:28 -05:00
fabianofranz
efb045ec60 Restores the ability to fetch local only flags 2015-02-11 17:12:48 -05:00
Ahsanul Haque
e1e66f7b4e Code commentary 2014-12-18 23:10:25 -05:00
Anthony Fok
4745f1fd64 In execute(), check if command is Runnable()
A corner case exists where c.Runnable() is not checked
before c.Run() is called, thus a nil c.Run is executed
leading to "panic: runtime error: invalid memory address
or nil pointer dereference".

This patch adds an extra c.Runnable() check in execute()
to catch that corner case.

Fixes #37.
2014-12-18 22:46:33 -05:00
Maciej Szulik
6e6b6a9c19 Subcommands flag parsing errors print subcommand usage and not root's command usage
Conflicts:
	command.go
2014-12-18 22:43:23 -05:00
Maciej Szulik
033c83bc5e Subcommands flag parsing errors print subcommand usage and not root's command usage 2014-12-09 08:46:16 -05:00
Clayton Coleman
9b6c92647a When no subcommands are registered, omit command help output
For a single root command with a Run method, the help output still
contains 'help [command]' as a subcommand (because Help is always
added). Since the only subcommand would be 'help', the help is better
off omitted.

This change allows a command to be used both as a subcommand
or a root command without having to define a custom help that elides
the help command when no subcommands are added.  Instead, the default
help command is only added when subcommands are present.
2014-11-11 23:43:27 -05:00
spf13
b1e90a7943 Making prefix matching opt in. 2014-10-07 16:15:19 -04:00
spf13
c2c23ac0bd adding support for prefix matching against aliases & names 2014-10-07 15:41:19 -04:00
Sam Ghods
881657297e Replace prefix matching with aliases 2014-10-07 15:22:14 -04:00
Joel Scoble
83b58a2f9b changed help flag setup to use PersistenFlags so -h will be supported 2014-10-07 15:18:02 -04:00
Sam Ghods
cdafa9f413 Print Long description when printing Help, fixes #20 2014-09-08 11:59:35 -04:00
spf13
864687ae68 Adding "OnInitialize()" method instead of directly setting a property. Now can have multiple initializers. 2014-07-11 10:57:53 -04:00
spf13
7cebca3761 Add support for an InitializeConfig method as defined on Cobra by the user 2014-06-27 15:45:51 -04:00
spf13
10a8494a87 Support for flags before commands 2014-06-17 12:32:27 -04:00
spf13
07be8145cc Adding support for --help 2014-06-17 12:28:42 -04:00
spf13
1a6c5d1d21 Remove "::" from output. Fixed #4 2014-06-13 20:06:09 -04:00
spf13
71bb1dfdcd Cobra behavior is now more consistent. Invalid flags cause Usage to be printed. 2014-06-13 20:00:56 -04:00
spf13
59bd285b5f Documentation update 2014-04-11 10:11:09 -04:00
tummychow
667c348dbd Test behavior for subcommand with same name as root command
If, for some reason, you have an application with some name "foo", and your
app has a subcommand "foo", cobra should behave properly when you call
"foo foo", and it should also behave if you call "foo f".

These changes verify both of these cases and ensure cobra responds properly.
2014-03-26 16:19:34 -04:00
tummychow
f4c075f8f8 Add command name prefix matching
A command can now be invoked with a prefix of its own name, assuming that
prefix is unambiguous (ie it isn't also a prefix of any sibling command's
name).
2014-03-26 05:12:44 -04:00
Kevin Darlington
e174a40cf5 Print root usage when help called with no args. 2014-02-03 21:52:51 -05:00
awilliams
2e73a96a48 Right aligns "Use" and "Additional help topics" even if they are longer than the min padding 2014-01-17 13:36:57 +01:00
spf13
fae133554d Fixing error messaging when upstream flag library is inconsistent. 2013-11-05 20:04:51 -05:00
spf13
79bdde5f6c Refactored code to eliminate need for a commander. Much simpler interface now. 2013-11-01 18:36:24 -04:00
spf13
a2845e7f7a fixed spf13/cobra#1 spf13/hugo#88 2013-09-30 21:54:46 -04:00
spf13
2d02c310f3 Help improvements 2013-09-29 02:02:49 -04:00
spf13
6b3d2f2e9a Shorter message when invalid flags provided 2013-09-29 02:01:56 -04:00
spf13
55ff474dee Finding command based on args cleaned up 2013-09-29 01:59:13 -04:00