Commit graph

1116 commits

Author SHA1 Message Date
Jack Wright
9f06afcca6
Merge ae37156f63 into ceb39aba25 2025-03-13 07:13:23 -05:00
John McBride
ceb39aba25
chore: Rm GitHub Action PR size labeler (#2256)
Signed-off-by: John McBirde <jpmmcbride@gmail.com>
2025-03-11 06:56:36 -06:00
John McBride
c25b598345
feat: Add security policy (#2253)
Signed-off-by: John McBirde <jpmmcbride@gmail.com>
2025-03-10 16:46:30 -06:00
styee
4f9ef8cdbb
Update command.go (#2248)
Fix spelling in the help output.

topcis -> topics
2025-03-07 09:53:22 -05:00
Fraser Waters
1995054b00
Flow context to command in SetHelpFunc (#2241)
Fixes https://github.com/spf13/cobra/issues/2240
2025-02-21 07:46:14 -05:00
Yedaya Katsman
f98cf4216d
Remove traling curlybrace (#2237) 2025-02-17 14:16:17 -05:00
Chongyi Zheng
0d417d8f77
Test with Go 1.24 (#2236) 2025-02-17 13:59:07 -05:00
Marc Khouzam
40b5bc1437
Revert "Make detection for test-binary more universal (#2173)" (#2235)
This reverts commit d1e9d85fcf.

Some programs set os.Args in their unit tests and this change would
break those tests.

Ref: https://github.com/spf13/cobra/pull/2173#issuecomment-2661544195
2025-02-16 18:42:04 -05:00
ccoVeille
a97f9fd47b
fix CompletionFunc implementation (#2234)
The new type CompletionFunc could lead to a regression.
This commit make the new `CompletionFunc` type a type alias instead,
in case projects using Cobra have created their own similar type.

This commit also adds a test to ensure that the completion function
remains backwards-compatible.

Signed-off-by: ccoVeille <3875889+ccoVeille@users.noreply.github.com>
2025-02-16 13:08:14 -05:00
John McBride
5f9c40898e
chore: Upgrade dependencies for v1.9.0 (#2233)
Signed-off-by: John McBirde <jpmmcbride@gmail.com>
2025-02-15 11:17:48 -07:00
Marc Khouzam
24ada7fe71
Remove the default "completion" cmd if it is alone (#1559)
When a program has no sub-commands, its root command can accept
arguments.  If we add the default "completion" command to such programs
they will now have a sub-command and will no longer accept arguments.

What we do instead for this special case, is only add the "completion"
command if it is being called, or if it is being completed itself.

We want to have the "completion" command for such programs because it
will allow the completion of flags and of arguments (if provided
by the program).

Signed-off-by: Marc Khouzam <marc.khouzam@gmail.com>
2025-02-15 11:07:02 -07:00
Steve Francia
680936a220
New logo 2025-02-11 20:00:54 -05:00
ccoVeille
8cb30f9ca5
feat: add CompletionWithDesc helper (#2231)
The code has also been refactored to use a type alias for completion and a completion helper

Using a type alias is a non-breaking change and it makes the code more readable and easier to understand.

Signed-off-by: ccoVeille <3875889+ccoVeille@users.noreply.github.com>
Co-authored-by: Marc Khouzam <marc.khouzam@gmail.com>
2025-02-09 11:06:07 -05:00
Raúl Barroso
17b6dca2ff
doc: add Conduit (#2230)
We recently added a CLI to a our Conduit project, and we use Cobra to power it.

Here's a blog post mentioning it https://meroxa.com/blog/introducing-the-new-conduit-cli:-a-powerful-tool-for-managing-your-pipelines/
2025-02-06 15:35:25 -05:00
Marc Khouzam
ab5cadcc1b
Allow to reset the templates to the default (#2229)
Follow-up to #1956.

This commit allows a program to reset any of the tree templates to their
default behaviour, as it was possible to do before the change of #1956.

Signed-off-by: Marc Khouzam <marc.khouzam@gmail.com>
2025-02-06 08:25:44 -07:00
Ville Skyttä
4ba5566f57
fix(bash): nounset unbound file filter variable on empty extension (#2228)
Happens at least if a flag is marked as filename, with "" given as
extensions.
2025-02-01 19:35:34 -05:00
Marc Khouzam
41b26ec8bb
Print ActiveHelp for bash along other completions (#2076)
In the bash shell we used to print ActiveHelp messages on every
tab-press. In the example below, notice the "Command help" line which is
ActiveHelp:

bash-5.1$ tanzu context u[tab]
Command help: Configure and manage contexts for the Tanzu CLI

bash-5.1$ tanzu context u[tab]
Command help: Configure and manage contexts for the Tanzu CLI

bash-5.1$ tanzu context u
unset  (Unset the active context so that it is not used by default.)
use    (Set the context to be used by default)
bash-5.1$ tanzu context u

Above, on the first [tab] press, only the ActiveHelp is printed.
On the second [tab] press, the ActiveHelp is printed again, followed
by a re-print of the command-line, followed by the completions choices.

The separation between ActiveHelp and completion choices makes the
ActiveHelp harder to see. Furthermore, I find the double printing of the
ActiveHelp string to look bad.

Note that for zsh, the UX is different and that ActiveHelp messages are
printed at the same time as the completion choices.

This commit aligns the UX for ActiveHelp in bash with the one for zsh:
if there are other completions to be shown, the ActiveHelp messages are
printed at the same time.

New behaviour:
1- ActiveHelp is no longer printed on the first [tab] press. This is
   better aligned with bash's standard approach.
2- ActiveHelp is printed on the second [tab] press, above the completion
   choices, with a `--` delimiter.
3- If there are no completion choices, the `--` delimiter is omitted.

This behaviour is the same as what is done for zsh (except that for zsh
the first [tab] press immediately shows completion choices).

Below is the above example, but using this commit.
Notice the more concise and easier to read completion output:

bash-5.1$ tanzu context u[tab][tab]
Command help: Configure and manage contexts for the Tanzu CLI
--
unset  (Unset the active context so that it is not used by default.)
use    (Set the context to be used by default)
bash-5.1$ tanzu context u

Signed-off-by: Marc Khouzam <marc.khouzam@gmail.com>
2025-01-29 19:28:46 -05:00
Alessandro Arzilli
611e16c322
Allow linker to perform deadcode elimination for program using Cobra (#1956)
* Restructure code to let linker perform deadcode elimination step

Cobra, in its default configuration, will execute a template to generate
help, usage and version outputs. Text/template execution calls MethodByName
and MethodByName disables dead code elimination in the Go linker, therefore
all programs that make use of cobra will be linked with dead code
elimination disabled, even if they end up replacing the default usage, help
and version formatters with a custom function and no actual text/template
evaluations are ever made at runtime.

Dead code elimination in the linker helps reduce disk space and memory
utilization of programs. For example, for the simple example program used by
TestDeadcodeElimination 40% of the final executable size is dead code. For a
more realistic example, 12% of the size of Delve's executable is deadcode.

This PR changes Cobra so that, in its default configuration, it does not
automatically inhibit deadcode elimination by:

1. changing Cobra's default behavior to emit output for usage and help using
   simple Go functions instead of template execution
2. quarantining all calls to template execution into SetUsageTemplate,
   SetHelpTemplate and SetVersionTemplate so that the linker can statically
   determine if they are reachable

Co-authored-by: Marc Khouzam <marc.khouzam@gmail.com>
2025-01-27 09:43:43 -05:00
Toni Kangas
09d5664f34
Add similar whitespace escape logic to bash v2 completions than in other completions (#1743)
Signed-off-by: Toni Kangas <toni.kangas@upcloud.com>
Signed-off-by: Marc Khouzam <marc.khouzam@gmail.com>
Co-authored-by: Marc Khouzam <marc.khouzam@gmail.com>
Co-authored-by: Jeffrey Faer <jeffrey.faer@gmail.com>
2025-01-22 21:28:30 -05:00
ccoVeille
6c3c1163c6
feat: add CompletionFunc type to help with completions (#2220)
Also document that NoFilesCompletion and FixedCompletion can be used with RegisterFlagCompletionFunc.
2025-01-21 20:34:05 -05:00
ZhuHaiCheng
01ffff4eca
chore: fix function name in comment (#2216)
Signed-off-by: zhuhaicity <zhuhai@52it.net>
2025-01-12 07:49:28 -05:00
Jack Wright
ae37156f63 Removing unused includeDesc parameter 2024-12-29 13:23:29 -08:00
Sebastiaan van Stijn
0745e55762
completion: improve detection for flags that accept multiple values (#2210)
The completion code attempts to detect whether a flag can be specified
more than once, and therefore should provide completion even if already
set.

Currently, this code depends on conventions used in the pflag package,
which uses an "Array" or "Slice" suffix or for some types a "stringTo"
prefix.

Cobra allows custom value types to be used, which may not use the same
convention for naming, and therefore currently aren't detected to allow
multiple values.

The pflag module defines a [SliceValue] interface, which is implemented
by the Slice and Array value types it provides (unfortunately, it's not
currently implemented by the "stringTo" values).

This patch adds a reduced interface based on the [SliceValue] interface
mentioned above to allow detecting Value-types that accept multiple values.
Custom types can implement this interface to make completion work for
those values.

I deliberately used a reduced interface to keep the requirements for this
detection as low as possible, without enforcing the other methods defined
in the interface (Append, Replace) which may not apply to all custom types.

Future improvements can likely still be made, considering either implementing
the SliceValue interface for the "stringTo" values or defining a separate
"MapValue" interface for those types.

Possibly providing the reduced interface as part of the pflag module and
to export it.

[SliceValue]: d5e0c0615a/flag.go (L193-L203)

Signed-off-by: Sebastiaan van Stijn <github@gone.nl>
Signed-off-by: Marc Khouzam <marc.khouzam@gmail.com>
Co-authored-by: Marc Khouzam <marc.khouzam@gmail.com>
2024-12-28 17:41:43 -05:00
Sebastiaan van Stijn
d1e9d85fcf
Make detection for test-binary more universal (#2173)
When running tests in verbose mode (or other options), tests involving
Cobra may fail if the test does not explicitly set Command.args to an
empty slice; in this case, Cobra defaults to using `os.Args`, which
will contain arguments passed to the test (such as `-v` (verbose)).

Commits e5762054c9 and 1ef0913976
implemented a workaround for this when running (unit) tests for Cobra
itself, but this check is specifig to Cobra (checking for `cobra.test`),
and don't work on Windows (which will have a `.exe` extension),

This patch implements a more universal check, so that users of Cobra
as a module also benefit from this workaround.

go1.21 and up provides a `testing.Testing()` utility ([1]); as the Cobra
module still supports Go1.16 and up, an alternative implementation was
added for older versions, based on golang.org/x/mod/lazyregexp [2].

Before this patch:

    go test -c -o foo.test

    ./foo.test -test.run TestNoArgs
    --- FAIL: TestNoArgs (0.00s)
        args_test.go:37: Unexpected output: Error: unknown command "TestNoArgs" for "c"
            Usage:
              c [flags]

            Flags:
              -h, --help   help for c

        args_test.go:40: Unexpected error: unknown command "TestNoArgs" for "c"
    FAIL

After this patch:

    go test -c -o foo.test

    ./foo.test -test.run TestNoArgs
    PASS

[1]: https://pkg.go.dev/testing#Testing
[2]: https://cs.opensource.google/go/x/mod/+/refs/tags/v0.19.0:internal/lazyregexp/lazyre.go;l=66-78

Signed-off-by: Sebastiaan van Stijn <github@gone.nl>
Signed-off-by: Marc Khouzam <marc.khouzam@gmail.com>
Co-authored-by: Marc Khouzam <marc.khouzam@gmail.com>
2024-12-28 17:07:03 -05:00
Jack Wright
997ac04e61 Merge branch 'main' into nushell 2024-12-18 08:58:23 -08:00
Sebastiaan van Stijn
9f9056765c
build(deps): bump github.com/cpuguy83/go-md2man/v2 from 2.0.5 to 2.0.6 (#2206)
fix compatibility with go versions before go1.17

diff: https://github.com/cpuguy83/go-md2man/compare/v2.0.5...v2.0.6

Signed-off-by: Sebastiaan van Stijn <github@gone.nl>
2024-12-16 21:26:34 -05:00
Jack Wright
da1bab9757 Fixed the ShellCompDirectiveNoFileComp case 2024-12-11 17:22:41 -08:00
Mikel Olasagasti Uranga
8519630750
Update to latest go-md2man (#2201)
Since cpuguy83/go-md2man 2.0.5 no paraTag is written after "SEE ALSO".

With go-md2man 2.0.4:

.SH SEE ALSO
.PP
\fBroot-bbb(1)\fP, \fBroot-ccc(1)\fP

With go-md2man 2.0.5:

.SH SEE ALSO
\fBroot-bbb(1)\fP, \fBroot-ccc(1)\fP

See: https://github.com/cpuguy83/go-md2man/pull/122

Signed-off-by: Mikel Olasagasti Uranga <mikel@olasagasti.info>
2024-12-11 07:26:08 -05:00
Jack Wright
1df1dbd552 cobra logging command 2024-12-08 20:34:38 -08:00
Jack Wright
52185bed6e tweaking extra completion instructions 2024-12-08 19:44:45 -08:00
Jack Wright
d728bbb91e Nushell instructions tweak 2024-12-08 19:40:45 -08:00
Jack Wright
14e642ede9 fixed bad grammar mistake 2024-12-08 19:35:50 -08:00
Jack Wright
e0ac28fad8 fix fish reference 2024-12-08 19:33:48 -08:00
Jack Wright
93a41331ec tmpfile handling changes 2024-12-01 20:05:39 -08:00
Jack Wright
470ca0a628 Added a comment at the beginning to remain consistent with other shells. 2024-12-01 20:05:18 -08:00
Jack Wright
ddb39920b4 More documentation work for nushell 2024-12-01 18:44:56 -08:00
Jack Wright
1886f6baad minor formatting tweak 2024-12-01 18:27:49 -08:00
Jack Wright
2f80e0879f changed from being markdown, as I don't think the docs here are markdown 2024-11-30 15:01:06 -08:00
Jack Wright
18d7a29987 more documentation tweaks 2024-11-30 14:58:44 -08:00
Jack Wright
59726e2b0f fixed a typo 2024-11-30 14:06:56 -08:00
Jack Wright
982ba402cb fix tests 2024-11-30 13:59:41 -08:00
Jack Wright
7006719603 fixing formatting 2024-11-30 13:57:31 -08:00
Jack Wright
8b4aa590bb Added new directive to the list of directives for posterity. 2024-11-30 13:34:39 -08:00
Jack Wright
7eac0e11ae Format with spaces instead of tabs 2024-11-30 13:32:05 -08:00
Jack Wright
e7abbf39a3 rewriting nushell implementation 2024-11-29 12:30:16 -08:00
Jack Wright
29af015b57 "fixed whitespace" 2024-11-29 12:30:16 -08:00
Jack Wright
f963733c4e "Reverted to old version without formatting changes and readded nushell" 2024-11-29 12:30:16 -08:00
Jack Wright
1a69a83abc "Added nushell to list of shell autompletes" 2024-11-29 12:30:16 -08:00
Jack Wright
8cb9728476 Update completions.go
Co-authored-by: Marc Khouzam <marc.khouzam@gmail.com>
2024-11-29 12:30:16 -08:00
Jack Wright
ead0ff3e53 Update completions.go
Co-authored-by: Marc Khouzam <marc.khouzam@gmail.com>
2024-11-29 12:30:16 -08:00