Commit Graph

872 Commits

Author SHA1 Message Date
Eric Paris 1ac08d9d94 UsageTemple: remove useless variable assignment 2015-09-11 18:52:47 -04:00
Eric Paris 85bb343e9c Merge pull request #151 from eparis/no-newline-without-short
No leading newline if Short and Long both unset
2015-09-11 17:46:37 -05:00
Eric Paris 0d9b07baf9 Merge pull request #150 from eparis/help-not-available
Do not count "help" as "Available"
2015-09-11 17:46:27 -05: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
Eric Paris 717619095e No leading newline if Short and Long both unset
Before --help would start
```

Usage:
  command [flags]
  [...]
```

After --help will show
```
Usage:
  command [flags]
  [...]
```
2015-09-11 18:28:40 -04:00
Eric Paris 24b4b01ffa Do not cound "help" as "Available" 2015-09-11 18:25:28 -04:00
Eric Paris 42498ec777 Merge pull request #148 from tamird/trailing-spaces
trim trailing whitespace
2015-09-11 17:24:06 -05:00
Tamir Duberstein 5f83d08ae0 trim trailing whitespace 2015-09-11 13:25:15 -04:00
Eric Paris 4b86c66ef2 Merge pull request #146 from eparis/is-available-rework
Update IsAvailableCommand checks
2015-09-09 14:20:07 -05:00
Eric Paris 3eac1434ef Update IsAvailableCommand checks
This slightly changes IsAvailableCommand in that a non-runnable command
with a runnable subcommand is now 'Available'

We also use IsAvailableCommand in the rest of the codebase instead of
half kinda sorta doing it incorrectly other places.
2015-09-08 22:48:12 -04:00
Eric Paris 75d96d75c3 Merge pull request #145 from eparis/man-cleanups
Enhance Man Page Generation
2015-09-08 18:19:27 -05:00
Eric Paris b625a4db24 Merge pull request #143 from sdomino/feature/hidden-commands
Feature/hidden commands
2015-09-08 18:19:22 -05:00
Steve Domino 6d2f1d2fa7 Ability to hide commands from usage/help text
Added the ability to have hidden commands that cobra will still run as intended, however they won't show up in any usage/help text

adding internal field to command

private is a better name

hiding private commands in default help/usage

opting for 'hidden' over 'private'

updating all 'help command' checks to exclude hidden commands

updating how commands are displayed in usage/help text by updating/adding some methods. added tests for hidden/deprecated commands

making command hidden when testing hidden command execution

test now leverage the included suite and are much less custom. also removed deprecation tests, once I discovered them in cobra_test.go

updating hidden command test to be more reliable

removing unnecessary () when checking len(c.Deprecated)

updating command comments to be godoc friendly
2015-09-08 17:01:13 -06:00
Eric Paris edcf765d9f Mangen: allow callers to specify header information
We previously had this weak argument called projectName which let you
set a single part of a man page header. Instead do the best we can if
the caller doesn't pass us anything, but let the caller specify anything
they want.
2015-09-08 18:52:09 -04:00
Eric Paris cfecf1379c manpage generation: Make sure parent in SEE ALSO has dashes
The CommandPath() for a parent might have a space (if .Parent() != .Root())
so we need to replace those spaces with `-`
2015-09-08 12:32:15 -04:00
Eric Paris 68f5a81a72 Merge pull request #141 from eparis/mac-completions
Enable bash completions on Macs
2015-09-04 09:11:03 -05:00
Eric Paris 32e87e3c8e Enable bash completions on Macs
If they installed bash-completions from homebrew, this should make our
completions work.
2015-09-03 17:00:18 -04:00
Eric Paris 16b4170663 Merge pull request #138 from eparis/cleanup-helpfunc
Allow SetHelpFunc to overwrite the --help flag instead of just the help subcommand
2015-09-03 15:53:20 -05:00
Bjørn Erik Pedersen 1ba88c48dd Merge pull request #139 from alexanderguy/RunE-is-runnable
RunE commands should be considered runnable.
2015-09-02 20:19:19 +02:00
Alexander Guy 823f6890b7 RunE commands should be considered runnable. 2015-09-02 10:59:31 -07:00
Eric Paris c2d19e9c43 Use the HelpFunc for the --help flag
We were just calling Help() when a user set the --help flag. You could
overwrite how the help subcommand worked with SetHelpFunc, but not now
the --help flag worked.
2015-09-01 14:25:38 -04:00
Eric Paris 27bf24802c Redo HelpFunc() to work things other than the help subcommand
Today the HelpFunc() seemed to be tailor built for the `help`
subcommand. Which has a rather weird purpose as its `Run` needs to
find the actual command we want to get help about.

Instead make the HelpFunc() for a command be about that command,
rather than having it search for some other command...
2015-09-01 14:25:32 -04:00
Eric Paris 1f92281ffc Merge pull request #135 from akutz/feature/custom-templ-funcs
Template Function Injection
2015-09-01 11:55:58 -05:00
Eric Paris e15d674de6 Merge pull request #134 from apriendeau/master
#67 creates RunE functions to allow for errors to populate to the top
2015-09-01 11:51:29 -05:00
Eric Paris 99d2c51143 Merge pull request #137 from eparis/fix-help-template
Remove spurious } from the help template
2015-09-01 11:51:12 -05:00
Eric Paris f7b419fbe1 Merge pull request #136 from eparis/late-init-help-flag-fix
Fix the late init help flag
2015-09-01 11:51:03 -05:00
Eric Paris 8f9b8c7cba Remove spurious } from the help template 2015-09-01 12:03:37 -04:00
Eric Paris ae28810f0e Fix the late init help flag 2015-09-01 11:31:23 -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
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