Skip to content

Commit

Permalink
Merge branch 'spf13:main' into one-required-flaggroup
Browse files Browse the repository at this point in the history
  • Loading branch information
marevers committed Jul 15, 2023
2 parents 06d37a0 + dcb405a commit 6f3b983
Show file tree
Hide file tree
Showing 24 changed files with 55 additions and 59 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ jobs:

- uses: actions/checkout@v3

- uses: golangci/golangci-lint-action@v3.4.0
- uses: golangci/golangci-lint-action@v3.6.0
with:
version: latest
args: --verbose
Expand Down
8 changes: 4 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ Cobra is a library for creating powerful modern CLI applications.

Cobra is used in many Go projects such as [Kubernetes](https://kubernetes.io/),
[Hugo](https://gohugo.io), and [GitHub CLI](https://github.com/cli/cli) to
name a few. [This list](./projects_using_cobra.md) contains a more extensive list of projects using Cobra.
name a few. [This list](site/content/projects_using_cobra.md) contains a more extensive list of projects using Cobra.

[![](https://img.shields.io/github/actions/workflow/status/spf13/cobra/test.yml?branch=main&longCache=true&label=Test&logo=github%20actions&logoColor=fff)](https://github.com/spf13/cobra/actions?query=workflow%3ATest)
[![Go Reference](https://pkg.go.dev/badge/github.com/spf13/cobra.svg)](https://pkg.go.dev/github.com/spf13/cobra)
Expand Down Expand Up @@ -80,7 +80,7 @@ which maintains the same interface while adding POSIX compliance.

# Installing
Using Cobra is easy. First, use `go get` to install the latest version
of the library.
of the library.

```
go get -u github.com/spf13/cobra@latest
Expand All @@ -105,8 +105,8 @@ go install github.com/spf13/cobra-cli@latest

For complete details on using the Cobra-CLI generator, please read [The Cobra Generator README](https://github.com/spf13/cobra-cli/blob/main/README.md)

For complete details on using the Cobra library, please read the [The Cobra User Guide](user_guide.md).
For complete details on using the Cobra library, please read the [The Cobra User Guide](site/content/user_guide.md).

# License

Cobra is released under the Apache 2.0 license. See [LICENSE.txt](https://github.com/spf13/cobra/blob/master/LICENSE.txt)
Cobra is released under the Apache 2.0 license. See [LICENSE.txt](LICENSE.txt)
2 changes: 1 addition & 1 deletion bash_completions.go
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@ __%[1]s_handle_go_custom_completion()
local out requestComp lastParam lastChar comp directive args
# Prepare the command to request completions for the program.
# Calling ${words[0]} instead of directly %[1]s allows to handle aliases
# Calling ${words[0]} instead of directly %[1]s allows handling aliases
args=("${words[@]:1}")
# Disable ActiveHelp which is not supported for bash completion v1
requestComp="%[8]s=0 ${words[0]} %[2]s ${args[*]}"
Expand Down
2 changes: 1 addition & 1 deletion bash_completionsV2.go
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ __%[1]s_get_completion_results() {
local requestComp lastParam lastChar args
# Prepare the command to request completions for the program.
# Calling ${words[0]} instead of directly %[1]s allows to handle aliases
# Calling ${words[0]} instead of directly %[1]s allows handling aliases
args=("${words[@]:1}")
requestComp="${words[0]} %[2]s ${args[*]}"
Expand Down
2 changes: 1 addition & 1 deletion cobra.go
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ const (
defaultCaseInsensitive = false
)

// EnablePrefixMatching allows to set automatic prefix matching. Automatic prefix matching can be a dangerous thing
// EnablePrefixMatching allows setting automatic prefix matching. Automatic prefix matching can be a dangerous thing
// to automatically enable in CLI tools.
// Set this to true to enable it.
var EnablePrefixMatching = defaultPrefixMatching
Expand Down
2 changes: 1 addition & 1 deletion fish_completions.go
Original file line number Diff line number Diff line change
Expand Up @@ -113,7 +113,7 @@ function __%[1]s_clear_perform_completion_once_result
__%[1]s_debug ""
__%[1]s_debug "========= clearing previously set __%[1]s_perform_completion_once_result variable =========="
set --erase __%[1]s_perform_completion_once_result
__%[1]s_debug "Succesfully erased the variable __%[1]s_perform_completion_once_result"
__%[1]s_debug "Successfully erased the variable __%[1]s_perform_completion_once_result"
end
function __%[1]s_requires_order_preservation
Expand Down
4 changes: 0 additions & 4 deletions fish_completions.md

This file was deleted.

35 changes: 13 additions & 22 deletions fish_completions_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,10 @@ package cobra

import (
"bytes"
"errors"
"fmt"
"log"
"os"
"path/filepath"
"testing"
)

Expand Down Expand Up @@ -98,12 +99,12 @@ func TestFishCompletionNoActiveHelp(t *testing.T) {
}

func TestGenFishCompletionFile(t *testing.T) {
err := os.Mkdir("./tmp", 0755)
tmpFile, err := os.CreateTemp("", "cobra-test")
if err != nil {
log.Fatal(err.Error())
t.Fatal(err.Error())
}

defer os.RemoveAll("./tmp")
defer os.Remove(tmpFile.Name())

rootCmd := &Command{Use: "root", Args: NoArgs, Run: emptyRun}
child := &Command{
Expand All @@ -113,18 +114,18 @@ func TestGenFishCompletionFile(t *testing.T) {
}
rootCmd.AddCommand(child)

assertNoErr(t, rootCmd.GenFishCompletionFile("./tmp/test", false))
assertNoErr(t, rootCmd.GenFishCompletionFile(tmpFile.Name(), false))
}

func TestFailGenFishCompletionFile(t *testing.T) {
err := os.Mkdir("./tmp", 0755)
tmpDir, err := os.MkdirTemp("", "cobra-test")
if err != nil {
log.Fatal(err.Error())
t.Fatal(err.Error())
}

defer os.RemoveAll("./tmp")
defer os.RemoveAll(tmpDir)

f, _ := os.OpenFile("./tmp/test", os.O_CREATE, 0400)
f, _ := os.OpenFile(filepath.Join(tmpDir, "test"), os.O_CREATE, 0400)
defer f.Close()

rootCmd := &Command{Use: "root", Args: NoArgs, Run: emptyRun}
Expand All @@ -135,18 +136,8 @@ func TestFailGenFishCompletionFile(t *testing.T) {
}
rootCmd.AddCommand(child)

got := rootCmd.GenFishCompletionFile("./tmp/test", false)
if got == nil {
t.Error("should raise permission denied error")
}

if os.Getenv("MSYSTEM") == "MINGW64" {
if got.Error() != "open ./tmp/test: Access is denied." {
t.Errorf("got: %s, want: %s", got.Error(), "open ./tmp/test: Access is denied.")
}
} else {
if got.Error() != "open ./tmp/test: permission denied" {
t.Errorf("got: %s, want: %s", got.Error(), "open ./tmp/test: permission denied")
}
got := rootCmd.GenFishCompletionFile(f.Name(), false)
if !errors.Is(got, os.ErrPermission) {
t.Errorf("got: %s, want: %s", got.Error(), os.ErrPermission.Error())
}
}
6 changes: 3 additions & 3 deletions powershell_completions.go
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ filter __%[1]s_escapeStringWithSpecialChars {
`+" $_ -replace '\\s|#|@|\\$|;|,|''|\\{|\\}|\\(|\\)|\"|`|\\||<|>|&','`$&'"+`
}
[scriptblock]$__%[2]sCompleterBlock = {
[scriptblock]${__%[2]sCompleterBlock} = {
param(
$WordToComplete,
$CommandAst,
Expand Down Expand Up @@ -122,7 +122,7 @@ filter __%[1]s_escapeStringWithSpecialChars {
__%[1]s_debug "Calling $RequestComp"
# First disable ActiveHelp which is not supported for Powershell
$env:%[10]s=0
${env:%[10]s}=0
#call the command store the output in $out and redirect stderr and stdout to null
# $Out is an array contains each line per element
Expand Down Expand Up @@ -279,7 +279,7 @@ filter __%[1]s_escapeStringWithSpecialChars {
}
}
Register-ArgumentCompleter -CommandName '%[1]s' -ScriptBlock $__%[2]sCompleterBlock
Register-ArgumentCompleter -CommandName '%[1]s' -ScriptBlock ${__%[2]sCompleterBlock}
`, name, nameForVar, compCmd,
ShellCompDirectiveError, ShellCompDirectiveNoSpace, ShellCompDirectiveNoFileComp,
ShellCompDirectiveFilterFileExt, ShellCompDirectiveFilterDirs, ShellCompDirectiveKeepOrder, activeHelpEnvVar(name)))
Expand Down
3 changes: 0 additions & 3 deletions powershell_completions.md

This file was deleted.

2 changes: 1 addition & 1 deletion powershell_completions_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,5 +29,5 @@ func TestPwshCompletionNoActiveHelp(t *testing.T) {

// check that active help is being disabled
activeHelpVar := activeHelpEnvVar(c.Name())
check(t, output, fmt.Sprintf("%s=0", activeHelpVar))
check(t, output, fmt.Sprintf("${env:%s}=0", activeHelpVar))
}
File renamed without changes.
12 changes: 6 additions & 6 deletions shell_completions.md → site/content/completions/_index.md
Original file line number Diff line number Diff line change
Expand Up @@ -416,7 +416,7 @@ completion firstcommand secondcommand
### Bash legacy dynamic completions

For backward compatibility, Cobra still supports its bash legacy dynamic completion solution.
Please refer to [Bash Completions](bash_completions.md) for details.
Please refer to [Bash Completions](bash.md) for details.

### Bash completion V2

Expand All @@ -425,13 +425,13 @@ Cobra provides two versions for bash completion. The original bash completion (

A new V2 bash completion version is also available. This version can be used by calling `GenBashCompletionV2()` or
`GenBashCompletionFileV2()`. The V2 version does **not** support the legacy dynamic completion
(see [Bash Completions](bash_completions.md)) but instead works only with the Go dynamic completion
(see [Bash Completions](bash.md)) but instead works only with the Go dynamic completion
solution described in this document.
Unless your program already uses the legacy dynamic completion solution, it is recommended that you use the bash
completion V2 solution which provides the following extra features:
- Supports completion descriptions (like the other shells)
- Small completion script of less than 300 lines (v1 generates scripts of thousands of lines; `kubectl` for example has a bash v1 completion script of over 13K lines)
- Streamlined user experience thanks to a completion behavior aligned with the other shells
- Streamlined user experience thanks to a completion behavior aligned with the other shells

`Bash` completion V2 supports descriptions for completions. When calling `GenBashCompletionV2()` or `GenBashCompletionFileV2()`
you must provide these functions with a parameter indicating if the completions should be annotated with a description; Cobra
Expand All @@ -448,7 +448,7 @@ show (show information of a chart)
$ helm s[tab][tab]
search show status
```
**Note**: Cobra's default `completion` command uses bash completion V2. If for some reason you need to use bash completion V1, you will need to implement your own `completion` command.
**Note**: Cobra's default `completion` command uses bash completion V2. If for some reason you need to use bash completion V1, you will need to implement your own `completion` command.
## Zsh completions

Cobra supports native zsh completion generated from the root `cobra.Command`.
Expand Down Expand Up @@ -482,7 +482,7 @@ search show status
### Zsh completions standardization

Cobra 1.1 standardized its zsh completion support to align it with its other shell completions. Although the API was kept backward-compatible, some small changes in behavior were introduced.
Please refer to [Zsh Completions](zsh_completions.md) for details.
Please refer to [Zsh Completions](zsh.md) for details.

## fish completions

Expand Down Expand Up @@ -535,7 +535,7 @@ search (search for a keyword in charts) show (show information of a chart) s
# With descriptions and Mode 'MenuComplete' The description of the current selected value will be displayed below the suggestions.
$ helm s[tab]
search show status
search show status
search for a keyword in charts
Expand Down
2 changes: 1 addition & 1 deletion bash_completions.md → site/content/completions/bash.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# Generating Bash Completions For Your cobra.Command

Please refer to [Shell Completions](shell_completions.md) for details.
Please refer to [Shell Completions](_index.md) for details.

## Bash legacy dynamic completions

Expand Down
4 changes: 4 additions & 0 deletions site/content/completions/fish.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
## Generating Fish Completions For Your cobra.Command

Please refer to [Shell Completions](_index.md) for details.

3 changes: 3 additions & 0 deletions site/content/completions/powershell.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
# Generating PowerShell Completions For Your Own cobra.Command

Please refer to [Shell Completions](_index.md#powershell-completions) for details.
2 changes: 1 addition & 1 deletion zsh_completions.md → site/content/completions/zsh.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
## Generating Zsh Completion For Your cobra.Command

Please refer to [Shell Completions](shell_completions.md) for details.
Please refer to [Shell Completions](_index.md) for details.

## Zsh completions standardization

Expand Down
8 changes: 4 additions & 4 deletions doc/README.md → site/content/docgen/_index.md
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
# Documentation generation

- [Man page docs](./man_docs.md)
- [Markdown docs](./md_docs.md)
- [Rest docs](./rest_docs.md)
- [Yaml docs](./yaml_docs.md)
- [Man page docs](man.md)
- [Markdown docs](md.md)
- [Rest docs](rest.md)
- [Yaml docs](yaml.md)

## Options
### `DisableAutoGenTag`
Expand Down
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
15 changes: 10 additions & 5 deletions user_guide.md → site/content/user_guide.md
Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,8 @@ func main() {

## Using the Cobra Generator

Cobra-CLI is its own program that will create your application and add any
commands you want. It's the easiest way to incorporate Cobra into your application.
Cobra-CLI is its own program that will create your application and add any commands you want.
It's the easiest way to incorporate Cobra into your application.

For complete details on using the Cobra generator, please refer to [The Cobra-CLI Generator README](https://github.com/spf13/cobra-cli/blob/main/README.md)

Expand Down Expand Up @@ -715,12 +715,17 @@ Run 'kubectl help' for usage.

## Generating documentation for your command

Cobra can generate documentation based on subcommands, flags, etc. Read more about it in the [docs generation documentation](doc/README.md).
Cobra can generate documentation based on subcommands, flags, etc.
Read more about it in the [docs generation documentation](docgen/_index.md).

## Generating shell completions

Cobra can generate a shell-completion file for the following shells: bash, zsh, fish, PowerShell. If you add more information to your commands, these completions can be amazingly powerful and flexible. Read more about it in [Shell Completions](shell_completions.md).
Cobra can generate a shell-completion file for the following shells: bash, zsh, fish, PowerShell.
If you add more information to your commands, these completions can be amazingly powerful and flexible.
Read more about it in [Shell Completions](completions/_index.md).

## Providing Active Help

Cobra makes use of the shell-completion system to define a framework allowing you to provide Active Help to your users. Active Help are messages (hints, warnings, etc) printed as the program is being used. Read more about it in [Active Help](active_help.md).
Cobra makes use of the shell-completion system to define a framework allowing you to provide Active Help to your users.
Active Help are messages (hints, warnings, etc) printed as the program is being used.
Read more about it in [Active Help](active_help.md).

0 comments on commit 6f3b983

Please sign in to comment.