cobra_cmd

package
v1.0.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Jun 12, 2021 License: MIT Imports: 3 Imported by: 0

Documentation

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

func KnownCommandArgs

func KnownCommandArgs(extraArgs *[]string, knownArgs ...string) cobra.PositionalArgs
Example
package main

import (
	"containrrr.dev/flagargs/cobra_cmd"
	"fmt"
	"github.com/spf13/cobra"
)

var extraArgs []string
var rootCmd = &cobra.Command{
	Short: "hello",
	Args:  cobra_cmd.KnownCommandArgs(&extraArgs, "name", "animal"),
	Run: func(cmd *cobra.Command, args []string) {
		name, _ := cmd.Flags().GetString("name")
		animal, _ := cmd.Flags().GetString("animal")
		fmt.Printf("The animal %q is named %q. Here is the extra args: %v", animal, name, extraArgs)
	},
}

func init() {
	rootCmd.PersistentFlags().String("name", "NAME", "the name")
	rootCmd.PersistentFlags().String("animal", "ANIMAL", "the animal")
}

func main() {
	// Override arguments for example, normally this uses os.Args[1:]
	rootCmd.SetArgs([]string{"fish", "fiddle", "sprockets", "shoestrings"})
	if err := rootCmd.Execute(); err != nil {
		fmt.Println("Error:", err)
	}
}
Output:

The animal "fiddle" is named "fish". Here is the extra args: [sprockets shoestrings]

Types

This section is empty.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL