Commit Graph

49 Commits

Author SHA1 Message Date
Ville Skyttä
3d8ac432bd
Micro-optimizations (#1957)
* Avoid redundant string splits

There likely isn't actually more than once to split in the source
strings in these cases, but avoid doing so anyway as we're only
interested in the first.

* Avoid redundant completion output target evaluations

The target is not to be changed while outputting completions, so resolve
it only once.

* Avoid redundant active help enablement evaluations

The enablement state is not to be changed during completion output, so
evaluate it only once.

* Preallocate some slices and maps with known size

* Avoid some unnecessary looping

* Use strings.Builder to construct suggestions
2023-11-23 12:24:33 -05:00
vkhoroz
4cafa37bc4
Allow running persistent run hooks of all parents (#2044)
Currently, only one of the persistent pre-runs and post-runs is executed.
It is always the first one found in the parents chain, starting at this command.
Expected behavior is to execute all parents' persistent pre-runs and post-runs.

Dependent projects implemented various workarounds for this:
- manually building persistent hook chains (in every hook).
- applying some kind of monkey-patching on top of Cobra.

This change eliminates the necessity for such workarounds
by allowing to set a global variable EnableTraverseRunHooks.

Tickets:
- https://github.com/spf13/cobra/issues/216
- https://github.com/spf13/cobra/issues/252

Signed-off-by: Volodymyr Khoroz <volodymyr.khoroz@foundries.io>
2023-10-21 20:36:12 -04:00
Taavi Väänänen
2246fa82e9
Fix grammar: 'allows to' (#1978)
The use in generated bash completion files is getting flagged by
Lintian (the Debian package linting tool).

Signed-off-by: Taavi Väänänen <hi@taavi.wtf>
2023-06-13 11:12:49 -04:00
Unai Martinez-Corral
9e6b58afc7
update copyright year (#1927) 2023-03-05 21:28:31 -05:00
Yash Ladha
f25a3c6e0b
fix: conflict import name with variable (#1879)
`template` is an import in `cobra.go` file and also used as a variable
name, which masks the library in the scope of that function.
2022-12-15 05:39:50 -05:00
Yann Soubeyrand
93d1913fb0
Add OnFinalize method (#1788)
This method is the OnInitialize counterpart. Like OnInitialize which allows
loading the configuration before each command is executed, OnFinalize allows
saving the configuration after each command has been executed.
2022-10-03 08:52:50 -06:00
Unai Martinez-Corral
6d978a911e
add missing license headers (#1809) 2022-09-16 07:55:56 -04:00
Yuval Goldberg
d689184a42
Support for case-insensitive command names (#1802)
Add a global `EnableCaseInsensitive` variable to allow
case-insensitive command names.

The variable supports commands names and aliases globally.

Resolves #1382
2022-09-11 08:25:22 -04:00
Unai Martinez-Corral
652c755d37
Use golangci-lint (#1044)
Use golangci-lint. Repair warnings and errors resulting from linting.
2021-02-07 17:08:50 -07:00
Bruce Downs
51f06c7dd1 Correct all complaints from golint
* i.e.
* go get golang.org/x/lint/golint
* go list ./... | xargs golint
2019-08-02 01:25:21 +05:00
Willi Eggeling
67fc4837d2 added variable to allow configuration of mousetrap message duration (#809)
new variable MousetrapDisplayDuration allows to modify the default
display duration of 5s, or to completely disable the timeout and wait
for the user to press the return key.
2019-03-20 20:05:52 -04:00
Jonathan Hall
c156af3984 Explain OnInitialize better in the GoDoc (#594) 2017-12-06 19:18:45 +01:00
Albert Nigmatzianov
65c8acb228 Improve tests 2017-11-09 07:56:43 +01:00
Albert Nigmatzianov
c46add8a65 Add ability to disable mousetrap
Fix #486
Close #383 (no progress there)
2017-07-11 14:08:33 +02:00
Eric Paris
347767f8bd Partial revert of 4d2c4af 'Improve template mechanism' (#439)
There were template functions which we defined and others started using.
Although we no longer want those functions, since others use them,
deleting them breaks our API.  Putting those (unused) functions back.
2017-05-09 15:07:05 -04: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
Théotime Lévêque
b6cb395893 Fix typo in docstring (#360) 2017-04-17 19:03:07 +02: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
5da98880f3 Edit documentation 2016-08-30 22:14:27 +05:00
bogem
101d434d44 Simplify OnInitialize func 2016-08-30 21:58:52 +05: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
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
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
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
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
akutz
5b121bc9fb Template Function Injection
This patch enables developers to add one to many template functions that
can be used by custom Usage and Help templates. Here is an example that
is included in the file cobra_test.go as the test function named
TestAddTemplateFunctions:

    AddTemplateFunc("t", func() bool { return true })
    AddTemplateFuncs(template.FuncMap{
        "f": func() bool { return false },
        "h": func() string { return "Hello," },
        "w": func() string { return "world." }})

    const usage = "Hello, world."

    c := &Command{}
    c.SetUsageTemplate(`{{if t}}{{h}}{{end}}{{if f}}{{h}}{{end}} {{w}}`)

    if us := c.UsageString(); us != usage {
        t.Errorf("c.UsageString() != \"%s\", is \"%s\"", usage, us)
    }

In the above example four functions are added to the template function
map used when the Usage and Help text is generated from the templates
that enable custom logic as well as data injection during template
execution.
2015-08-31 22:36:55 -05:00
bep
beda1945ad Add mousetrap for Windows users
Fixes #80
2015-04-01 21:14:40 +02:00
Ahsanul Haque
e1e66f7b4e Code commentary 2014-12-18 23:10:25 -05:00
spf13
b1e90a7943 Making prefix matching opt in. 2014-10-07 16:15:19 -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
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
62fb674a93 Organize the files a bit better 2013-09-24 12:47:29 -04:00
spf13
57fc2cb534 Support and test for custom output. 2013-09-24 12:12:32 -04:00
spf13
b0c5461629 Much improved usage message 2013-09-13 00:09:28 -04:00
spf13
8858462331 Add decent usage message 2013-09-12 10:37:35 -04:00
spf13
6067837866 Adding support for a custom output, default to stderr 2013-09-11 09:52:15 -04:00
spf13
8d87882095 Documentation & housecleaning 2013-09-11 09:52:15 -04:00
spf13
7ba2df2eff Detect and panic if command is setup as it's own child. 2013-09-10 18:28:58 -04:00
spf13
90b975fd8e Rewrote DebugFlags() function 2013-09-10 18:27:31 -04:00
spf13
061ba30a84 Create and utilize mergePersistentFlags method 2013-09-10 18:26:17 -04:00
spf13
ccbe1b2359 Adding debug flags method to a command & commander 2013-09-04 11:32:51 -04:00
spf13
c568b575aa Better testing support. Easy to reset to blank slate in tests. 2013-09-04 11:32:51 -04:00
spf13
a618569eea Better error handling 2013-09-04 11:32:36 -04:00
spf13
36e08ffd5b Parsing persistentFlags properly (mostly) 2013-09-04 00:15:18 -04:00
spf13
ce6447a965 Properly swallowing flag error messages 2013-09-03 23:28:34 -04:00
spf13
bbb8e6e199 initial commit of cobra w/test suite 2013-09-03 18:54:51 -04:00