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
27
votes
3 answers

Cobra Subcommand Invoke Help By Default

I want sub command to print out the Help menu if no argument or flags is passed (The main command does this by default). For example, the main command without any arguments or flags: chris@pop-os:~$ ./tk Command line application to deploy Usage: …
Christopher
  • 273
  • 3
  • 6
15
votes
2 answers

What is the difference between go-Cobra PersistentFlags and Flags?

I'm trying to figure out whats is the difference between PersistentFlags and Flags in go-Cobra, and when should we use each of them. I've read this but I didn't understand it.
Vahid
  • 1,265
  • 10
  • 20
12
votes
2 answers

How to mark some global persistent flags as hidden for some Cobra commands

I'm developing some CLI utility with Cobra. For my RootCmd I've set up some persistent flags (i.e. flags which also affect all the commands). But some of the commands don't use those flags, so I'd like to make them hidden for these particular…
Petr Razumov
  • 1,952
  • 2
  • 17
  • 32
10
votes
2 answers

Cobra init not working,: Command not found

I am following this guide. ***@pop-os:~/go/src/foo$ cobra init --pkg-name foo Command 'cobra' not found, but can be installed with: sudo apt install cobra My setup: I have go 1.16 installed $ go version go version go1.16.3 linux/amd64 my $GOPATH…
herrwolken
  • 389
  • 1
  • 4
  • 12
9
votes
2 answers

How to implement unit tests for CLI commands in go

I'm starting a new OSS CLI tool that utilizes spf13/cobra. Being new to golang, I'm having a hard time figuring out the best way to test commands in isolation. Can anybody give me an example of how to test a command? Couple of caveats: you can't…
Busch
  • 857
  • 10
  • 29
9
votes
3 answers

Permitted flag values for Cobra

Is there built-in tooling (and if so how do I use it?) to require a flag to be one of several values and throw an error if a flag is not one of the permitted values, in the Cobra library? I didn't see this on the Github page.
Bren
  • 3,516
  • 11
  • 41
  • 73
9
votes
1 answer

Config file with cobra and viper

Basic information: I have created a go application and used Cobra for it. Cobra uses Viper for command line parameters and flags. I have a command listen with a flag bind and I want to configure it in a yaml file. Code: The init function of the…
Sascha
  • 10,231
  • 4
  • 41
  • 65
8
votes
1 answer

Cobra change usage line in help template

I want to be able to set the Usage line to specify that an argument NEEDS to be passed if the help function is invoked on the cobra command in Go. This is what the regular help flag outputs: Cancel the order specified by the order id by submitting a…
Robert M.
  • 83
  • 1
  • 3
8
votes
1 answer

How to unmarshall viper config to struct with dash character

I have this following config file defined as toml file: [staging] project-id = "projectId" cluster-name = "cluster" zone = "asia-southeast1-a" Then, I have this struct type ConfigureOpts struct { GCPProjectID string `json:"project-id"` …
Agung Pratama
  • 3,666
  • 7
  • 36
  • 77
7
votes
1 answer

Validating flags using Cobra

The sketch below is a command line application written using Cobra and Go. I'd like to throw an error if the value of flag1 doesn't match the regex ^\s+\/\s+. How do I do that? package cmd import ( "fmt" "os" …
Aurelia Peters
  • 2,169
  • 1
  • 20
  • 34
7
votes
1 answer

What is the difference between StringVar vs StringVarP or String vs StringP when using Go (golang), cobra and viper?

I'm maintaining some code written using Go (golang), Viper and Cobra. On one line, it has: rootCmd.PersistentFlags().String("cfg", "", "A description") And then on the following line it has rootCmd.PersistentFlags().StringP("output", "o", ".",…
M_M
  • 1,955
  • 2
  • 17
  • 23
7
votes
2 answers

golang cobra subcommands without arguments matching

i want to create golang CLI with cobra. For now, it runs well. I have created something like foobar create --username johndoe. But I need subcommands like foobar create user --username johndoe. There is user subcommand. I have created with…
fzlrhmn
  • 411
  • 1
  • 4
  • 14
6
votes
3 answers

Why am I getting a nil pointer error depending on where I call BindPFlag?

I've just recently started working with Go, and I've run into some behavior working with Cobra and Viper that I'm not sure I understand. This is a slightly modified version of the sample code you get by running cobra init. In main.go I have: package…
larsks
  • 277,717
  • 41
  • 399
  • 399
5
votes
2 answers

How can I invoke a default subcommand with cobra?

Using cobra, if my app is invoked without a specific action (but arguments), I'd like to run a default command: // rootCmd represents the base command when called without any subcommands var rootCmd = &cobra.Command{ Use: "mbmd", Short:…
andig
  • 13,378
  • 13
  • 61
  • 98
5
votes
1 answer

Cobra: how to set flags programmatically in tests

I'm using cobra to build a CLI and want to simulate a command being run with different sets of options/flags. I've been trying to figure out how I can use the cobra APIs to set flags within my test but haven't really gotten it yet. I have this: //…
sixty4bit
  • 7,422
  • 7
  • 33
  • 57
1
2 3
8 9