Commit Graph

294 Commits

Author SHA1 Message Date
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
Eric Paris 4c4f2d9417 Merge pull request #133 from eparis/late-init-help-flag
Initialize the --help flag as late as possible
2015-08-31 09:26:37 -05:00
Austin Riendeau 9174039216 #67 creates RunE functions to allow for errors to populate to the top 2015-08-30 19:03:16 -06:00
Eric Paris bab6d45bc6 Initialize the --help flag as late as possible
This should let users overwrite it with something custom. Like the help
sub-command.
2015-08-30 12:10:14 -04:00
Eric Paris 7cd6c9c000 Merge pull request #132 from AntonioMeireles/unbrand-man
turn generated man pages non k8s specific.
2015-08-28 12:09:21 -05:00
António Meireles f5d2d93abf turn generated man pages non k8s specific.
Signed-off-by: António Meireles <antonio.meireles@reformi.st>
2015-08-28 17:34:11 +01:00
Eric Paris e4993076d8 Merge pull request #128 from eparis/genManPages
Auto generation of a man pages
2015-08-21 01:11:45 -07:00
Eric Paris 9e7788657f Auto generation of a man page 2015-08-21 01:10:13 -07:00
Eric Paris db05184446 Merge pull request #127 from eparis/show-commandline-on-parent
Display pflag.CommandLine flags as if they were declared on the parent
2015-08-16 22:42:27 -07:00
Eric Paris e8bd799c1c Display pflag.CommandLine flags as if they were declared on the parent
```go
package main

import (
	"github.com/spf13/cobra"
	"github.com/spf13/pflag"
)

func main() {
	cmd := &cobra.Command{
		Use:   "min",
		Short: "minimal command",
		Run:   func(_ *cobra.Command, _ []string) {},
	}

	pflag.String("oncmdline", "oncmdline", "oncmdline")
	cmd.Execute()
}
```

Is a minimal cobra program.  When --help is displayed without this patch
you only get:

But with the patch --oncmdline is shows under flags.
2015-08-16 22:31:08 -07:00
Eric Paris c55cdf3385 Merge pull request #126 from anthonyfok/bash-completion/subdirs-in-dir
Add new BashCompSubdirsInDir annotation
2015-08-09 17:25:49 -05:00
Anthony Fok 1e6fdf608f Add new BashCompSubdirsInDir annotation
This first `cd` to a specified directory, then
lists the subdirectories therein with `_filedir -d`.

This can be used by e.g. `hugo --theme=[Tab][Tab]`, which would
give a list of subdirectories under the `themes` directory.
2015-08-09 13:30:58 -06:00
Eric Paris 385fc87e43 Merge pull request #122 from eparis/cleanups
Update help template
2015-06-29 19:11:20 -05:00
Eric Paris f453e878d4 Update help template
The template had gotten out of control. It was basically unparsable.
This does a little more work in functions and a little less in the
template. Overall it should be basically the same. It might output the
'additional help topics' in a couple of fewer places, but I doubt people
complain too much...
2015-06-29 20:06:04 -04:00
Eric Paris 8e127101ec Merge pull request #121 from eparis/cleanups
Cleanups
2015-06-29 17:30:09 -05:00
Eric Paris 9a9d01c9ec Better error message
Calling `cobra-test echo times one two turkey` where `one` and `two` are
valid arguments but `turkey` is not now results in.

Error: invalid argument "turkey" for "cobra-test echo times"
Run 'cobra-test echo times --help' for usage.
2015-06-29 17:09:41 -04:00
Eric Paris 0a7a850026 Make error handling more obvious
Again, the code looks a little more like a middle-schooler's code. But
that just makes it easier to understand and maintain.
2015-06-29 15:45:01 -04:00
Eric Paris 6f735782e0 Remove unused ErrHelp check
Inside Command.Execute() we were checking for pflag.ErrHelp. But
Command.execute() never returns that value. It just complicates the code
and isn't used.
2015-06-29 15:45:01 -04:00
Eric Paris 0a1a8e2e92 Remove (unused) cmdErrorBuf
Nothing was using it any more...
2015-06-29 15:45:01 -04:00
Eric Paris d0bb3e33e6 rework Find() to make it more obvious what is happening
We had lots of quirky if statements like `commandFound.Name() ==
c.Name() && len(stripFlags(args, c)) > 0 && commandFound.Name() !=
args[0]` which embeed all sorts of artifacts which are hard to parse. So
in general, just try to simplify and make stuff readable.
2015-06-29 15:44:56 -04:00
Eric Paris 66816bcd03 Merge pull request #119 from eparis/bogus-bad-command
Handle grand children with the same name as the root
2015-06-22 17:06:05 -05:00
Eric Paris 07ad27d239 Handle grand children with the same name as the root
This fixes a problem where if you had a root command and a grand child
with the same name, the parser would break and would not run the
grandchild. The code was special casing if the immediate child had the
same name, but didn't handle grand-children
2015-06-22 17:54:08 -04:00
Eric Paris ddd4c82b82 Merge pull request #116 from sgotti/fix_flag_as_unknown_command_in_output
Correctly print the unknown command name
2015-06-22 16:52:47 -05:00
Eric Paris a8f7f3dc25 Merge pull request #118 from liggitt/file_autocomplete_helper
Simplify setting file/extension annotations on a flag
2015-06-22 14:56:13 -05:00
Jordan Liggitt 6119fc993e Simplify setting file/extension annotations on a flag 2015-06-22 15:16:00 -04:00
Simone Gotti fb86c5c559 Correctly print the unknown command name
by now, if someone calls: `program --validflag unknowncommand` the
output will be:

```
Error: unknown command "--validflag"
Run 'program help' for usage.
```

This patch strips out flags so the unknown command is printed:

```
Error: unknown command "unknowncommand"
Run 'program help' for usage.
```
2015-06-22 11:02:06 +02:00
Bjørn Erik Pedersen 312092086b Merge pull request #115 from skonzem/fix_typos
Fix typos in docs
2015-06-05 20:08:24 +02:00
Scott Konzem 39e648c2b0 Fix typos in docs 2015-06-05 13:02:54 -04:00
Eric Paris 8f5946caae Merge pull request #112 from jlowdermilk/fix-genmarkdowntree
Don't prepend filename in default GenMarkdownTree
2015-05-20 22:43:41 -05:00
Jeff Lowdermilk 7b4b4aaac9 Don't prepend filename in default GenMarkdownTree 2015-05-20 17:21:00 -07:00
Anastasis Andronidis d910a04b50 Add Global Normalization Function
[close #110]
2015-05-19 10:23:16 -04:00
Sam Ghods e0f326dabc Fix root command without subcommands but with arguments
[close #108]
2015-05-19 10:22:07 -04:00
Eric Paris c11766b405 slight typo in md_docs.md 2015-05-13 08:38:33 -05:00
Bjørn Erik Pedersen e659faf634 Merge pull request #109 from bep/markdowncustom
genmarkdown: add optional frontmatter- and linkadjustment-funcs
2015-05-13 15:30:04 +02:00
bep 1d99c8ff6d genmarkdown: add optional frontmatter- and linkadjustment-funcs
The automatic Markdown generator works great!

But to use it to render the documentation in Hugo, we need front matter and slightly different links.

This commit adds optional callback funcs to add that.
2015-05-13 15:22:00 +02:00
bep be18870136 Fix two failing tests
The error message has changed ever so slightly.
2015-05-13 11:54:47 +02:00
Eric Paris bba56042cf Merge pull request #105 from eparis/minor-bash-cleanup
make filename extension handling a bash function
2015-05-04 22:51:16 -05:00
Eric Paris dff410ab56 make filename extension handling a bash function
We were trying to call a bash function with bash stuff like @ () from a
variable.  Stop that.  Just call a function with an arg from a variable
instead of trying to pass around the bash.

Should fix https://github.com/spf13/cobra/pull/103
2015-05-04 18:41:53 -04:00
Eric Paris 743fa31b46 Merge pull request #106 from eparis/bash-comp-docs
Make the filename extension bit of docs easier to read.
2015-05-04 15:20:46 -05:00
Eric Paris 48b95d0594 Make the filename extension bit of docs easier to read. 2015-05-04 16:20:06 -04:00
Eric Paris 787b737b48 Merge pull request #104 from eparis/minor-bash-cleanup
Clean up minor bash complaints from shellcheck.net
2015-05-04 13:56:05 -05:00
Eric Paris f4b3401f9e Clean up minor bash complaints from shellcheck.net 2015-05-04 14:40:27 -04:00
Eric Paris 7fc9f148dd Merge pull request #102 from andronat/master
Fixed Persistent-Run function propagation
2015-05-04 11:57:28 -05:00
Anastasis Andronidis 799a8ef863 Test for Persistent-Run propagation 2015-05-04 17:57:46 +02:00
Anastasis Andronidis 42e6ce397f Fixed Persistent-Run function propagation 2015-05-04 03:42:55 +02:00
Eric Paris 3ee9552eeb Merge pull request #100 from eparis/pre-post-run
Pre post run fucntions
2015-04-30 13:26:01 -05:00
Eric Paris fad5931693 Add docs, tests, and bit of rearrainging... 2015-04-30 14:08:47 -04:00
Alexander Thaller fbce60cc56 added persistent pre/post commands. 2015-04-30 12:45:23 -04:00
Alexander Thaller 2df64026ba added pre and post run hooks. 2015-04-30 12:44:01 -04:00
Eric Paris 9cf0f3737d Merge pull request #90 from eparis/deprecated-subcommands
Deprecated subcommands
2015-04-30 10:22:48 -05:00