Commit graph

170 commits

Author SHA1 Message Date
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
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