Commit Graph
66 Commits
Author SHA1 Message Date
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
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
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
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
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
Eric Paris c11766b405 slight typo in md_docs.md 2015-05-13 08:38:33 -05: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
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
Eric Paris 9cf0f3737d Merge pull request #90 from eparis/deprecated-subcommands
Deprecated subcommands
2015-04-30 10:22:48 -05:00
Eric Paris 7c7837e882 Merge pull request #85 from eparis/more-simplification
More simplification
2015-04-30 10:21:12 -05:00
Eric Paris 2d5fab043c Merge pull request #75 from eparis/CommandLine
Make flags declared outside spf13.pflag persistent flags on the parent
2015-04-30 10:20:35 -05:00
Eric Paris c3e48f996d Deprecated subcommands
They should still work, but shouldn't show up in help or usage output
2015-04-29 13:08:20 -04:00
Eric Paris 69e5f196b5 Merge pull request #98 from eparis/markdown-quiet-test
quiet down to go test for markdown generation
2015-04-28 09:43:10 -05:00
Eric Paris a112838e26 quiet down to go test for markdown generation
We were printing the markdown, don't ask me why.
2015-04-28 10:42:03 -04:00
Eric Paris 3c187e9045 Merge pull request #93 from kennyballou/minor_grammar_fix
Fix minor wording issue in README
2015-04-16 16:19:32 -05:00
Eric Paris 9cb5e85029 Merge pull request #92 from eparis/sort-valid-args
Sort the valid args before output
2015-04-13 17:59:01 -05:00
Eric Paris 0fc734c3c5 Sort the valid args before output
We do this so we get stable repeatable output and can see if updates are
necessary or if it is just golang doing the same thing twice differently
2015-04-13 18:44:05 -04:00
Eric Paris c0da825198 Merge pull request #87 from eparis/gen-md-doc
Auto generation of markdown docs!
2015-04-12 13:45:33 -05:00
Eric Paris 8a18f25809 Auto generation of markdown docs!
An example from the kubernetes project, for the `kubectl config`
command, which as subcommands, and flags, and all sorts of stuff, it
will generate markdown like so:

config modifies .kubeconfig files

config modifies .kubeconfig files using subcommands like "kubectl config set current-context my-context"

```
kubectl config SUBCOMMAND
```

```
      --envvar=false: use the .kubeconfig from $KUBECONFIG
      --global=false: use the .kubeconfig from /home/username
  -h, --help=false: help for config
      --kubeconfig="": use a particular .kubeconfig file
      --local=false: use the .kubeconfig in the current directory
```

```
      --alsologtostderr=false: log to standard error as well as files
      --api-version="": The API version to use when talking to the server
  -a, --auth-path="": Path to the auth info file. If missing, prompt the user. Only used if using https.
      --certificate-authority="": Path to a cert. file for the certificate authority.
      --client-certificate="": Path to a client key file for TLS.
      --client-key="": Path to a client key file for TLS.
      --cluster="": The name of the kubeconfig cluster to use
      --context="": The name of the kubeconfig context to use
      --insecure-skip-tls-verify=false: If true, the server's certificate will not be checked for validity. This will make your HTTPS connections insecure.
      --log_backtrace_at=:0: when logging hits line file:N, emit a stack trace
      --log_dir=: If non-empty, write log files in this directory
      --log_flush_frequency=5s: Maximum number of seconds between log flushes
      --logtostderr=true: log to standard error instead of files
      --match-server-version=false: Require server version to match client version
      --namespace="": If present, the namespace scope for this CLI request.
      --password="": Password for basic authentication to the API server.
  -s, --server="": The address and port of the Kubernetes API server
      --stderrthreshold=2: logs at or above this threshold go to stderr
      --token="": Bearer token for authentication to the API server.
      --user="": The name of the kubeconfig user to use
      --username="": Username for basic authentication to the API server.
      --v=0: log level for V logs
      --validate=false: If true, use a schema to validate the input before sending it
      --vmodule=: comma-separated list of pattern=N settings for file-filtered logging
```

* [kubectl](kubectl.md)	 - kubectl controls the Kubernetes cluster manager
* [kubectl config set](kubectl_config_set.md)	 - Sets an individual value in a .kubeconfig file
* [kubectl config set-cluster](kubectl_config_set-cluster.md)	 - Sets a cluster entry in .kubeconfig
* [kubectl config set-context](kubectl_config_set-context.md)	 - Sets a context entry in .kubeconfig
* [kubectl config set-credentials](kubectl_config_set-credentials.md)	 - Sets a user entry in .kubeconfig
* [kubectl config unset](kubectl_config_unset.md)	 - Unsets an individual value in a .kubeconfig file
* [kubectl config use-context](kubectl_config_use-context.md)	 - Sets the current-context in a .kubeconfig file
* [kubectl config view](kubectl_config_view.md)	 - displays merged .kubeconfig settings or a specified .kubeconfig file.
2015-04-12 14:40:34 -04:00
Eric Paris c746d30ef0 Merge pull request #69 from eparis/command-annotations
Bash Autocompletion Generator
2015-04-10 23:20:17 -05:00
Eric Paris 9ea4cdf081 Merge pull request #91 from PeterDaveHello/patch-1
Use svg instead of png to get better image quality
2015-04-10 23:20:05 -05:00
Eric Paris a068307565 add a helper to annotate a flag as 'required' by a command 2015-04-07 20:14:18 -04:00
Eric Paris 9b2e6822e5 Add bash autocompletion generator
Given a (potentially annotated) cobra command you can generate a bash
completion script.
2015-04-07 20:13:49 -04:00
Eric Paris b78326bb16 Merge pull request #88 from eparis/subhelptest
Add tests about additional help topics
2015-04-07 18:26:15 -05:00
Eric Paris aab4e37189 Merge pull request #89 from eparis/line-in-go-test-error
print line number in go test failures
2015-04-07 17:05:46 -05:00
Eric Paris 86a16864ea Add tests about additional help topics 2015-04-07 16:17:22 -04:00
Eric Paris 9692856020 print line number in go test failures 2015-04-07 15:56:01 -04:00
Eric Paris f576d29563 Merge pull request #82 from bep/mousetrap
Add mousetrap for Windows users
2015-04-06 15:49:03 -05:00
Eric Paris 0ea881ce45 Merge pull request #60 from dohzya/remove-command
Add Command's RemoveCommand method
2015-04-06 15:18:42 -05:00
Eric Paris bbc51773c3 Merge pull request #77 from eparis/additional-help-topics
Additional help topics
2015-04-06 15:04:48 -05:00
Eric Paris 794a362808 Remove specail casing in Execute()
We had some stuff that created a new empty []string if args was already
and empty string (why?)

We had some stuff that called Help() if it wasn't runnable (but
.execute() already does that)

Just remove the special case stuff.
2015-04-06 16:02:09 -04:00
Eric Paris b9e25fa4a0 Slight formatting change, to make next commit more readable 2015-04-06 16:02:08 -04:00
Eric Paris 29e27b1649 Merge pull request #84 from eparis/subcommand-invalid-flag
Stop special casing runnable root commands
2015-04-06 14:57:12 -05:00
Eric Paris bd0f8a846e Stop special casing runnable root commands
The special case code to handle a runnable root command had some
problems.  It was noticed that if you created a runnable root and a
subcommand.  And the subcommand was then executed with both a valid and
invalid flag, the error message was about the valid flag being invalid.
For example

./command subcommand --goodflag=10 --badflag=10

Would fail and tell you that --goodflag was an invalid flag. Instead if
we just do away with the special Command.execute() for the root command
the parser for subcommand is what prints the error and it gets it
right...
2015-04-06 15:42:03 -04:00
Eric Paris fe581720ad New tests (they don't all pass) 2015-04-06 15:42:01 -04:00
Eric Paris 09b49c329c Merge pull request #83 from adammck/fix_double_output_on_unknown_command
Fix redundant error for unknown root commands
2015-04-03 15:28:12 -05:00
Eric Paris fd10548830 Fix additional help topics template
The additional help topics were really hard to ever get to show.  The
required conditionals were difficult to meet and did not seem to really
be logical.

Problems I see:
1) the top level command could never have additional topics.
2) you must have at least one sibling command AND one subcommand
3) we had the AND above, but then test both conditionals a second time
4) if the sub command was runnable we wouldn't print anything
5) if the sibling commands were not runnable we wouldn't print anything
4+5) it's possible that we printed "Additional help topics:" with nothing following it
6) We always printed ourselves as a sibling in the additional info

Whew, I think I fixed all of those!  Again, using
https://github.com/eparis/readable-golang-template
I'm actually able to visualize the template and see this craziness.

The conditionals BEFORE this change:

{{if .HasParent}}
        {{if and (gt .Commands 0) (gt .Parent.Commands 1) }}
                Additional help topics:
                {{if gt .Commands 0 }}
                        {{range .Commands}}
                                {{if not .Runnable}}
                                        {{rpad .CommandPath .CommandPathPadding}} {{.Short}}
                                {{end}}
                        {{end}}
                {{end}}
                {{if gt .Parent.Commands 1 }}
                        {{range .Parent.Commands}}
                                {{if .Runnable}}
                                        {{if not (eq .Name $cmd.Name) }}
                                        {{end}}
                                        {{rpad .CommandPath .CommandPathPadding}} {{.Short}}
                                {{end}}
                        {{end}}
                {{end}}
        {{end}}
{{end}}

The conditionals AFTER this change:

{{if or (.HasHelpSubCommands) (.HasRunnableSiblings)}}
        Additional help topics:
        {{if .HasHelpSubCommands}}
                {{range .Commands}}
                        {{if not .Runnable}}
                                {{rpad .CommandPath .CommandPathPadding}} {{.Short}}
                        {{end}}
                {{end}}
        {{end}}
        {{if .HasRunnableSiblings }}
                {{range .Parent.Commands}}
                        {{if .Runnable}}
                                {{if not (eq .Name $cmd.Name) }}
                                        {{rpad .CommandPath .CommandPathPadding}} {{.Short}}
                                {{end}}
                        {{end}}
                {{end}}
        {{end}}
{{end}}
2015-03-20 14:55:05 -04:00
Eric Paris c64442a487 fix usage template conditional
I wrote https://github.com/eparis/readable-golang-template which
converts golang templates into something structured around the
conditionals.  Obviously you can't just USE the output, but you can SEE
the problems.  In this case the output shows something like:

{{if .HasParent}}
        {{if and (gt .Commands 0) (gt .Parent.Commands 1) }}
                Additional help topics:
                {{if gt .Commands 0 }}
                        {{range .Commands}}
                                {{if not .Runnable}}
                                        {{rpad .CommandPath .CommandPathPadding}} {{.Short}}
                                {{end}}
                        {{end}}
                {{end}}
                {{if gt .Parent.Commands 1 }}
                        {{range .Parent.Commands}}
                                {{if .Runnable}}
                                        {{if not (eq .Name $cmd.Name) }}
                                        {{end}}
                                        {{rpad .CommandPath .CommandPathPadding}} {{.Short}}
                                {{end}}
                        {{end}}
                {{end}}
        {{end}}
{{end}}

We have a completely unused "{{if not (eq .Name $cmd.Name) }}"

Move the {{end}} after the {{rpad...}}
2015-03-20 13:59:32 -04:00
Eric Paris f81b784738 Make flags declared outside spf13.pflag persistent flags on the parent
Some projects pick up flags from other projects they include.  A great
example would be projects that use glog and thus get all of those flags.
Kubernetes, for example, merges those flags manually into its commands.

This was reported in https://github.com/spf13/cobra/issues/44

What this patch does is merge those flags into the PersistentFlags on
the highest parent.  This allows kubernetes to stop having to merge
things themselves...
2015-03-19 15:08:12 -04:00
Eric Paris 6de96b849c Default usage output to stdout
If the command has not set an output explicitly everything will go to
stderr.  This makes a lot of sense, but is a huge PITA for people who
want to be able to grep the help output.  It is very common for users to
want to do

command --help | grep flag

This patch fixes that by default help output (but not error output like
an invalid command) to stdout instead of defaulting to stderr.
2015-03-18 16:43:00 +01:00
Eric Paris f8e1ec56bd new NonInheritedFlags() which give all flags which were not persisted from a parent command 2015-02-17 13:52:53 -05:00
Eric Paris b96dd75141 new InheritedFlags() command to tell all flags which persisted from a parent 2015-02-17 13:52:53 -05:00
Eric Paris bf480fe628 New "example" section of commands
We have a long and short description.  This adds an "Example" section.
Which can used to create better docs than putting it all in Long.
2015-02-17 13:50:52 -05:00