Commit Graph

193 Commits

Author SHA1 Message Date
Tim Peoples
eb58983359 Add CalledAs method to cobra.Command (w/ tests) (#567)
* Add `CalledAs` method to Command (w/ tests)

The `CalledAs` method returns the name of the command or alias that
invoked the command -- as long as the command was actually invoked.
Otherwise, it returns the empty string.

The opens up possibilies for commands to behave differently based on
which alias invoked the command (in the same vein as Linux programs
which adjust their behavior based on the value of argv[0]).

* Fixed formatting
2018-02-04 08:58:53 -08:00
Shamus Taylor
3a7eb146ba fix grammatical error in command.go (#626) 2018-02-01 22:04:56 -08:00
Nick Miyake
ccaecb155a Ensure that '--version' flag works properly for root command (#595)
Make it so that, in the case that the root command is not runnable
but has subcommands, specifying a '--version' flag will still
run the "version" behavior.
2017-12-07 08:49:35 +01:00
Nick Miyake
b1ec2ce1ad Add support for --version flag (#584) 2017-12-01 22:37:16 +01:00
Nick Miyake
19e54c4a2b Update error message for missing required flags (#580)
Make it so that first letter is not capitalized and rephrase
to remove "have/has".
2017-11-19 10:22:51 +01:00
Daniel Nephin
34ceca591b Disable flags in UseLine (#552)
Signed-off-by: Daniel Nephin <dnephin@gmail.com>
2017-11-19 08:58:54 +01:00
Albert Nigmatzianov
d6a430541c Edit ResetFlags and ResetCommands descriptions 2017-11-09 07:56:43 +01:00
Albert Nigmatzianov
65c8acb228 Improve tests 2017-11-09 07:56:43 +01:00
Pavol Loffay
f634327172 Fix typo (#573) 2017-11-08 11:47:54 +01:00
Thomas Cyron
7b2c5ac9fc Create new buffer if not present yet (#549)
Fixes a nil dereference when TraverseChildren is used
with multiple subcommands.
2017-10-12 20:25:33 +02:00
Albert Nigmatzianov
40f18800b2 Delete name field from Command (#547)
Discussion: https://github.com/spf13/cobra/pull/422#discussion_r143918343
2017-10-11 10:15:37 -04:00
Di Xu
4d6af280c7 enforce required flags (#502) 2017-10-09 23:44:33 -04:00
Daniel Nephin
83b1f03962 Add a TraverseChildren option to allow for flags on each command in the hierarchy (#299)
Fix #277 
Fix #467
2017-10-07 23:29:11 +02:00
Diego Becciolini
0dacccfbaa Improve consistency of flags when using SetGlobalNormalizationFunc (#522)
Fix #521
2017-10-02 12:00:25 +02:00
Albert Nigmatzianov
1723331773 Update documentation 2017-08-23 12:32:09 +05:00
Daniel Nephin
f20b4e9c32 Refactor TakesArgs to use an interface for arg validation.
Fix some typos in README and comments.
Move arg validation to after flag validation so that the help flag is run first.
Pass the same args to ValidateArgs as the Run methods receive.
Update README.

Signed-off-by: Daniel Nephin <dnephin@gmail.com>
2017-07-23 09:57:27 +02:00
Eric Paris
d89c499964 Allow commands to explicitly state if they do, or do not take arbitrary arguments
Check that arguments are in ValidArgs

If a command defined cmd.ValidArgs check that the argument is actually
in ValidArgs and fail if it is not.
2017-07-23 09:57:27 +02:00
Albert Nigmatzianov
d994347eda Delete tab in the long description of help 2017-07-16 12:48:02 +02:00
Albert Nigmatzianov
8c6fa02d22 Fix InitDefaultHelpCmd when custom help command is set 2017-06-29 12:52:34 +02:00
Albert Nigmatzianov
99b5d838ca Show messages if deprecated flags are used
Fix #463
2017-06-19 23:05:29 +02:00
Albert Nigmatzianov
e606913c4e Fix typos 2017-05-28 12:56:30 +02:00
Albert Nigmatzianov
52ae6a1d02 Make initHelpCmd public (#436)
Useful for solving #424
2017-05-20 19:23:33 +02:00
Albert Nigmatzianov
51b7cf57e1 Fix tests so they give correct args (#445)
* Fix tests so they give correct args

Shell already deletes all quotes and unite args under quotes, so we
don't need to test it.

* Simplify stripFlags

* Fix 'unused' and 'gosimple' complaints
2017-05-14 08:51:04 -04:00
Albert Nigmatzianov
7d22e77cce Fix incorrect merge of pflag.CommandLine to cmd.Flags()
Fix #443
2017-05-12 20:22:26 +02:00
Albert Nigmatzianov
eceb483eb5 Divide fields of Command for readability 2017-05-09 11:33:56 +02:00
Albert Nigmatzianov
90d2fd84ad Group fields of Command 2017-05-09 11:25:41 +02:00
Albert Nigmatzianov
4db577d34d Fix docs for Command 2017-05-09 11:15:05 +02:00
Albert Nigmatzianov
4d2c4afa04 Improve template mechanisms
* Delete Eq, Gt, appendIfNotPresent and trim functions

* Add "[flags]" in UseLine

* Simplify other functions

* Simplify templates

Minor performance improvement.
Benchmark for command with 4 flags and one child command:
benchmark                   old ns/op     new ns/op     delta
BenchmarkCmdUsageFunc-4     335860        319290        -4.93%

benchmark                   old allocs     new allocs     delta
BenchmarkCmdUsageFunc-4     562            543            -3.38%

benchmark                   old bytes     new bytes     delta
BenchmarkCmdUsageFunc-4     21623         21037         -2.71%
2017-05-08 14:22:04 +02:00
Albert Nigmatzianov
d83a1d7ccd Add comment for InitDefaultHelpFlag 2017-05-07 10:52:40 +02:00
Albert Nigmatzianov
fe69f2e3a3 Make initHelpFlag public
Used for solving #424
2017-05-07 00:45:39 +02:00
Albert Nigmatzianov
4061f41c9a Use pflag.ShorthandLookup 2017-05-05 08:08:38 +02:00
Albert Nigmatzianov
3d7bff8a32 Revert "Get rid of flagErrorBuf (#417)"
This reverts commit b655df6ce8.

Reason for revert: duplicate errors from flag. More in #426

Fix #426
2017-04-27 15:10:57 +02:00
Albert Nigmatzianov
867d414548 Initialize help flag in initHelpCmd
Fix #302
2017-04-26 21:24:14 +02:00
Albert Nigmatzianov
f4f10f6873 Simplify stripFlags 2017-04-24 15:20:01 +02:00
Albert Nigmatzianov
9890b7b2e9 Simplify stripFlags function 2017-04-23 22:42:38 +02:00
Albert Nigmatzianov
681a777b18 Delete checkHelpFunc 2017-04-23 09:17:44 +02:00
Albert Nigmatzianov
e135867f96 Make parentsPflags more flexible
updateParentsPflags returns nothing, so you can use it independent of
mergePersistentFlags. A little performance impact.
2017-04-20 15:38:50 +02:00
Albert Nigmatzianov
3e61377cd5 Use AddFlagSet instead of self-written function 2017-04-20 15:38:50 +02:00
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