Git Product home page Git Product logo

flagargs's Introduction

Flagargs

A helper for flag-derived command line parsers for mapping non-flag arguments to a set of ordered flags and separating all unknown flags (as well as anyhthing after --) to a separate slice.

The containrrr.dev/flagargs is dependency-free, but containrrr.dev/flagargs/cobra_cmd depends on github.com/spf13/cobra.

Install

go get containrrr.dev/flagargs/cobra_cmd

or for the core library (for usage with pflag or flag):

go get containrrr.dev/flagargs

Usage

Cobra

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() {
	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]
}

pflag

package main

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

var (
	flags *pflag.FlagSet
	parser *flagargs.Parser
)

func init() {
	flags = pflag.NewFlagSet("example", pflag.ExitOnError)
	flags.String("name", "", "the name of the thing")
	parser = flagargs.NewParser("name")
}

func main() {
	extra, err := parser.ParseAndUpdateFlags(flags, flags.Args())
	if err != nil {
		_ = fmt.Errorf("error setting known arg flag: %v", err)
		return
	}
	name, _ := flags.GetString("name")
	fmt.Printf("name was set to %q, ", name)
	fmt.Printf("got %v extra argument(s): %v\n", len(extra), extra)
}

stdlib flag

package main

import (
	"containrrr.dev/flagargs"
	"flag"
	"fmt"
)

var parser *flagargs.Parser
var name string

func init() {
	flag.StringVar(&name, "name", "", "the name of the thing")
	parser = flagargs.NewParser("name")
}

func main() {
	flag.Parse()
	extra, err := parser.ParseAndUpdateFlags(flag.CommandLine, flag.CommandLine.Args())
	if err != nil {
		_ = fmt.Errorf("error setting known arg flag: %v", err)
		return
	}
	fmt.Printf("name was set to %q\n", name)
	fmt.Printf("got %v extra argument(s): %v\n", len(extra), extra)
}

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.