Commit graph

739 commits

Author SHA1 Message Date
Clayton Coleman
af6b555418 Merge pull request #95 from deads2k/deads-prevent-extra-removal
prevent removal of valid arguments
2015-04-28 10:06:27 -04:00
deads2k
36aee64abe prevent removal of valid arguments 2015-04-28 07:58:26 -04:00
Bjørn Erik Pedersen
ebb2d55f56 Merge pull request #97 from sdboyer/patch-1
Fix misspelling typo
2015-04-26 16:23:45 +02:00
Sam Boyer
476e73f093 Fix misspelling typo 2015-04-26 08:53:29 -04:00
Bjørn Erik Pedersen
d697cc29c1 Merge pull request #96 from commonlisp/master
Fixed typo in md output
2015-04-25 22:31:21 +02:00
George Kuan
c4667c9e63 Fixed typo in md output 2015-04-25 10:59:26 -07: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
kballou
f491ee284b Fix minor wording issue in README 2015-04-16 15:15:33 -06: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
Peter Dave Hello
a40354f0bd Use svg instead of png to get better image quality 2015-04-10 07:15:21 +08: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
Adam Mckaig
2c370cd936 Fix redundant error for unknown root command 2015-04-03 01:07:34 -04:00
bep
beda1945ad Add mousetrap for Windows users
Fixes #80
2015-04-01 21:14:40 +02:00
bep
be3cf39f80 Set Go version to 1.4.2 in Travis config
See #81
2015-04-01 19:20:02 +02:00
bep
be1667c3f0 Add Go 1.4 to Travis config
See #81
2015-04-01 19:14:28 +02:00
bep
bea8ba9ce6 Update Go versions in Travis config
See #81
2015-04-01 19:04:25 +02:00
Jeff Lowdermilk
79bd93d369 Add test for persistent bool flag on subcommand 2015-03-23 14:42:59 -07: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
Edgard Castro
384c059f4b Help uses Short message if Long is not available 2015-03-19 20:29:37 +01: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
Jason Moiron
e784a59910 fix missing newline in help output introduced in bf480fe628 2015-03-18 21:11:55 +01: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
Bjørn Erik Pedersen
6264dc67e1 Merge pull request #70 from brendandburns/master
Add mergePersistentFlags in strip flags since we now look at the flag set.
2015-03-17 00:30:08 +01:00
Brendan Burns
93278e2f35 Add mergePersistentFlags in strip flags since we now look at the flag set. 2015-03-16 16:15:32 -07:00
Brendan Burns
9e7273d546 Remove some wonky error handling, as upstream seems correct now.
Also, it's buggy for nested commands.
2015-03-13 13:36:21 -04:00
Étienne Vallette d'Osia
5b56110fb0 Add tests for RemoveCommand 2015-03-13 16:30:33 +09:00
Étienne Vallette d'Osia
0e7e122c81 Add README section for RemoveCommand 2015-03-13 12:40:00 +09:00
Étienne Vallette d'Osia
f479c924b8 Add Command's RemoveCommand method
This method removes children commands of an existing command.

This allows to build CLI clients that can be extended by 3rd party tools,
for instance by adding commands _and replacing the “version” command_.

For now the 1st defined command will be executed, so it is not possible
to override an existing command. But anyway, deleting old command then
adding a new one is the ultimate way to be certain there is no
confusion.
2015-03-12 22:44:11 -04:00
Brendan Burns
2cb625eda3 Remove some wonky error handling, as upstream seems correct now.
Also, it's buggy for nested commands.
2015-03-12 22:37:48 -04:00
Brendan Burns
bbdea35c49 Fix stripFlags to be more intelligent about what it parses. 2015-03-12 22:36:49 -04:00