Questions tagged [go-cobra]

A Commander for modern Go CLI interactions

A Commander for modern Go CLI interactions (https://github.com/spf13/cobra)

125 questions
3
votes
1 answer

How can the default helpCommand be called inside a cobra command?

In the following code I'm defining a command, where two options are possible: 1. myapp info --flag1 text 2. myapp info --flag2 if none of the two options are specified, I want to show the helpCommand var infoCmd = &cobra.Command{ Use: …
adm_
  • 642
  • 6
  • 13
3
votes
1 answer

Creating sub-commands with the same name in Go w/ Cobra

I'm very new to Go in general, and this is my first project in Cobra. It seems easy enough to create commands in Cobra with the command-line tool: cobra add And adding sub-commands seems easy enough as well. cobra add -p…
Rabid Penguin
  • 1,084
  • 9
  • 22
2
votes
1 answer

GO cobra: space separated values in StringArray flags

In GO's Cobra, lib for making CLIs, there are two input flags accepting multiple values being passed. One of the options is StringArray, when used as follows: --flag=value1 --flag=value2 it yields an array ["value1", "value2"]. I am working on a…
Piotr Zakrzewski
  • 3,591
  • 6
  • 26
  • 28
2
votes
1 answer

Impossible to scan a filepath with Golang

I am creating a client with the Cobra framework in Go that consists of uploading a file to a transfer service that will be released very soon. I want it to be a minimally customizable with a default download folder. Problem when I want to check that…
Elie
  • 45
  • 3
2
votes
0 answers

Dynamically registering command in Cobra

is there a way of dynamically registering cobra commands based on a viper configuration file? Basically I want to only register commands that are available within my config file as "modules". My root.go's init function looks like this func init() { …
2
votes
1 answer

ValidArgsFunction dynamic autocomplete not working with Golang Cobra cli program

I'm trying to get autocomplete working with Cobra but nothing happens after pressing the tab key - on something that I think should autocomplete. Am I missing something? var HelloCmd = &cobra.Command{ Use: "hello ", Short: "Say hello…
johnny
  • 51
  • 4
2
votes
1 answer

Retrieve persistent flags only once in cobra

I have the following cobra setup var rootCmd = &cobra.Command{ Use: "basic", Short: "This is the basic command", PreRunE: func(cmd *cobra.Command, args []string) error { if err := viper.BindPFlags(cmd.Flags()); err != nil { …
pkaramol
  • 16,451
  • 43
  • 149
  • 324
2
votes
1 answer

How to pass flags as arguments in cobra (golang)?

I am using cobra to create a CLI application (app). I need to implement a behavior in which I can pass flags as arguments. These flags will be passed|used further to another application via exec.Command(). All flag arguments passed to app must be…
fabelx
  • 197
  • 1
  • 12
2
votes
1 answer

How can I provide JSON array as argument to cobra cli

I'm building a CLI using Go and Cobra library. I've the following JSON that needs to be deserialized in the corresponding struct. Argument as JSON array: "[ (stringA, stringB), stringC ]" Struct type MyStruct struct { StringArray…
tortuga
  • 737
  • 2
  • 13
  • 34
2
votes
1 answer

Configure a default directory path in Go with Cobra and Viper

I want the user of my CLI app to enter a specific path where they want to save files. I tried to use Cobra's --config flag but I don't know how to. Example: app --config path "~/Documents/" How could I do this?
2
votes
0 answers

OnInitialize vs PersistentPreRun in a Cobra-based CLI

I'm working with the Cobra library for Go. The README shows an example that handles configuration setup via the cobra.OnInitialize function. The documentation for OnInitialize says: OnInitialize sets the passed functions to be run when each…
larsks
  • 277,717
  • 41
  • 399
  • 399
2
votes
2 answers

how do I pass a command status to Postrun in cobra cli

Cobra CLI has support for PostRun to invoke after execution of a command. https://github.com/spf13/cobra#prerun-and-postrun-hooks How do I pass on the command status to PostRun call? I have requirement of posting the command status after its…
gbvikas
  • 47
  • 4
  • 9
2
votes
1 answer

How to pass space to Cobra CLI string slice flag?

I am using a StringSliceP from the Pflag libraray accept a list of strings as CLI arguments. I am calling the Go application from the Windows Command Prompt. I would like some of the strings of the list to contain a (") double quote character, but I…
brandon_busby
  • 122
  • 11
2
votes
1 answer

Cobra CMD does not executes completly

I have the following code, is really simple. The point is that the code after the first fmt.Println is never executed. Any idea why? The code creates a random string, and then creates a Gin Router. The code that executes the router is never being…
2
votes
3 answers

spf13/cobra can't download binary to $GOPATH/bin

I want to use spf13/cobra on my project. spf13/cobra: A Commander for modern Go CLI interactions typed install command result $ go get -u github.com/spf13/cobra/cobra go: downloading github.com/spf13/cobra v1.0.0 go: downloading…
nabeen
  • 434
  • 3
  • 12
1 2
3
8 9