Commit Graph

500 Commits

Author SHA1 Message Date
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
Bjørn Erik Pedersen
6264dc67e1 Merge pull request #70 from brendandburns/master
Add mergePersistentFlags in strip flags since we now look at the flag set.
2015-03-17 00:30:08 +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
5b56110fb0 Add tests for RemoveCommand 2015-03-13 16:30:33 +09:00
Étienne Vallette d'Osia
0e7e122c81 Add README section for RemoveCommand 2015-03-13 12:40:00 +09: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
Étienne Vallette d'Osia
fd2b84c4e2 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-03 16:38:01 +09: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
Clayton Coleman
07a9dc0024 Update README.md to describe help behavior without subcommands 2015-02-11 17:18:36 -05:00
Kartik Singhal
bd75ea1d19 Updated test corresponding to #49 2015-02-11 17:17:28 -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
spf13
1cb31604a1 small formatting change to readme 2014-12-18 22:41:49 -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
Steve Francia
b825817fc0 Merge pull request #32 from smarterclayton/hide_subcommand_help_for_root_only_command
When no subcommands are registered, omit command help output
2014-11-12 16:52: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
Brad Dickason
d8ec18ee74 Fixed typo :D 2014-09-08 12:00:28 -04:00
Sam Ghods
cdafa9f413 Print Long description when printing Help, fixes #20 2014-09-08 11:59:35 -04:00
Hugo Leonardo Costa e Silva
8d72c1e167 Fix typo mistake on README 2014-08-06 00:22:50 -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
3e874b31b7 Test confirming fixed #11 2014-06-13 20:45:55 -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
Alan Pearce
7ab4fd75e9 Fix grammar in readme 2014-05-27 14:54:43 -04:00
Ryan Cox
0330aef05c Fix small typo 2014-05-09 23:30:45 -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
4c29b190e0 Add basic test for prefix matching 2014-03-26 05:12:48 -04:00
tummychow
96d543cf2c Reset root command lists in testing
This fixes some issues that appear when testing prefix invocations. Since the
root command lists weren't being cleared, the list would persist between
tests, so there would be multiple instances of each command. Then, if you
tried to match a prefix of one of those commands, you'd get two matches (one
for each instance) and the command would fail.

Resetting the root command lists prevents them from persisting between tests,
resolving this issue.
2014-03-26 05:12:48 -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