Skip to content

Completions modify os.Args #2257

Open
Open
@jonathan-conder

Description

@jonathan-conder

I'm trying to workaround #1877 by inspecting os.Args by hand. In some cases cobra accidentally inserts -- into os.Args.

Minimal example:

package main

import (
	"os"
	"strings"

	"github.com/spf13/cobra"
)

func main() {
	cmd := &cobra.Command{
		TraverseChildren: true,
		ValidArgsFunction: func(cmd *cobra.Command, args []string, toComplete string) ([]cobra.Completion, cobra.ShellCompDirective) {
			cobra.CompErrorln(strings.Join(os.Args[1:], ", "))
			return nil, cobra.ShellCompDirectiveDefault
		},
	}
	if err := cmd.Execute(); err != nil {
		panic(err)
	}
}

Expected behaviour:

$ go run . __complete x
[Debug] [Error] __complete, x
:0
Completion ended with directive: ShellCompDirectiveDefault

Actual behaviour:

$ go run . __complete x
[Debug] [Error] __complete, --
:0
Completion ended with directive: ShellCompDirectiveDefault

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions