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:
…
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.
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…
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…
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…
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.
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…
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…
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"`
…
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"
…
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", ".",…
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…
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…
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:…
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:
//…