Commit Graph

35 Commits

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