Giter VIP home page Giter VIP logo

commandeer's Introduction

Commandeer

Go Report Card GoDoc Coverage

Image

Commandeer sets up command line flags based on struct fields and tags.

Do you...

  • like to develop Go apps as libraries with tiny main packages?
  • get frustrated keeping your flags up to date as your code evolves?
  • feel irked by the overlap between comments on struct fields and help strings for flags?
  • hate switching between your app's main and library packages?

You might like Commandeer. See the godoc for detailed usage, or just...

Try It!

Here's how it works, define your app like so:

package myapp

import "fmt"

type Main struct {
	Num     int    `help:"How many does it take?"`
	Vehicle string `help:"What did they get?"`
}

func NewMain() *Main { return &Main{Num: 5, Vehicle: "jeep"} }

func (m *Main) Run() error {
	if m.Num < 2 || m.Vehicle == "" {
		return fmt.Errorf("Need more gophers and/or vehicles.")
	}
	fmt.Printf("%d gophers stole my %s!\n", m.Num, m.Vehicle)
	return nil
}

and your main package:

package main

import (
	"fmt"

	"github.com/jaffee/commandeer"
	"github.com/jaffee/commandeer/examples/myapp"
)

func main() {
	err := commandeer.Run(myapp.NewMain())
	if err != nil {
		fmt.Println(err)
	}
}

Now...

$ ./myapp -h
Usage of ./myapp:
  -num int
    	How many does it take? (default 5)
  -vehicle string
    	What did they get? (default "jeep")

$ ./myapp
5 gophers stole my jeep!
$ ./myapp -num 3 -vehicle horse
3 gophers stole my horse!

Notice that Commandeer set up the default values for each flag based on the values in the struct passed to Run.

Commandeer is set up for minimal dependency pollution - it uses only stdlib dependencies and is a few hundred lines of code itself. You need only import it from a tiny main package (as in the example), and shouldn't need to reference it anywhere else.

If you aren't allergic to external dependencies, you can also try github.com/jaffee/commandeer/cobrafy which pulls in the excellent Cobra and pflag packages giving you GNU/POSIX style flags and some other nice features should you care to use them. See the godoc, or the myapp-cobrafy example.

Features

In addition to the help struct tag, you can use flag to override the computed flag name, e.g.

type Main struct {
	Num     int    `flag:"number"`
}

You can also use flag:"-" to explicitly ignore fields from being used as flags, e.g.

type Main struct {
	Num     int    `flag:"-"`
}

Nested structs are supported, by default the field names will be joined with "." to create the flag name, e.g.

type Main struct {
	Vehicle struct {
		Color string
		Weight int
	}
}

produces:

  -vehicle.color string
    	
  -vehicle.weight int

If you wish to avoid this prefix behavior (e.g. if you have an embedded struct field and you want to elevate its fields to the top level) you can use flag:"!embed", e.g.

type Main struct {
	Vehicle struct {
		Color string
		Weight int
	} `flag:"!embed"`
}

which will produce:

  -color string
    	
  -weight int

Contributing

Yes please!

For small stuff, feel free to submit a PR directly. For larger things, especially API changes, it's best to make an issue first so it can be discussed.

Similar projects

commandeer's People

Contributors

anacrolix avatar codysoyland avatar jaffee avatar niaow avatar pafuent avatar seebs avatar travisturner avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

commandeer's Issues

panic when using named non-struct types

While attempting to use a wrapped version of time.Duration, I ran into a crash when using commandeer.

commandeer/com.go

Lines 274 to 358 in 07c6265

case reflect.String:
p := f.Addr().Interface().(*string)
flags.string(p, flagName, shorthand, f.String(), flagHelp(ft))
case reflect.Bool:
p := f.Addr().Interface().(*bool)
flags.bool(p, flagName, shorthand, f.Bool(), flagHelp(ft))
case reflect.Int:
p := f.Addr().Interface().(*int)
val := int(f.Int())
flags.int(p, flagName, shorthand, val, flagHelp(ft))
case reflect.Int64:
p := f.Addr().Interface().(*int64)
flags.int64(p, flagName, shorthand, f.Int(), flagHelp(ft))
case reflect.Float64:
p := f.Addr().Interface().(*float64)
flags.float64(p, flagName, shorthand, f.Float(), flagHelp(ft))
case reflect.Uint:
p := f.Addr().Interface().(*uint)
val := uint(f.Uint())
flags.uint(p, flagName, shorthand, val, flagHelp(ft))
case reflect.Uint64:
p := f.Addr().Interface().(*uint64)
flags.uint64(p, flagName, shorthand, f.Uint(), flagHelp(ft))
case reflect.Slice:
if !flags.pflag {
return fmt.Errorf("cannot support slice field at '%v' with stdlib flag pkg.", flagName)
}
switch ft.Type.Elem().Kind() {
case reflect.String:
p := f.Addr().Interface().(*[]string)
flags.stringSlice(p, flagName, shorthand, *p, flagHelp(ft))
case reflect.Bool:
p := f.Addr().Interface().(*[]bool)
flags.boolSlice(p, flagName, shorthand, *p, flagHelp(ft))
case reflect.Int:
p := f.Addr().Interface().(*[]int)
flags.intSlice(p, flagName, shorthand, *p, flagHelp(ft))
case reflect.Uint:
p := f.Addr().Interface().(*[]uint)
flags.uintSlice(p, flagName, shorthand, *p, flagHelp(ft))
default:
return fmt.Errorf("encountered unsupported slice type/kind: %#v at %s", f, prefix)
}
case reflect.Float32:
if !flags.pflag {
return fmt.Errorf("cannot support float32 field at '%v' with stdlib flag pkg.", flagName)
}
p := f.Addr().Interface().(*float32)
flags.float32(p, flagName, shorthand, *p, flagHelp(ft))
case reflect.Int16:
if !flags.pflag {
return fmt.Errorf("cannot support int16 field at '%v' with stdlib flag pkg.", flagName)
}
p := f.Addr().Interface().(*int16)
flags.int16(p, flagName, shorthand, *p, flagHelp(ft))
case reflect.Int32:
if !flags.pflag {
return fmt.Errorf("cannot support int32 field at '%v' with stdlib flag pkg.", flagName)
}
p := f.Addr().Interface().(*int32)
flags.int32(p, flagName, shorthand, *p, flagHelp(ft))
case reflect.Uint16:
if !flags.pflag {
return fmt.Errorf("cannot support uint16 field at '%v' with stdlib flag pkg.", flagName)
}
p := f.Addr().Interface().(*uint16)
flags.uint16(p, flagName, shorthand, *p, flagHelp(ft))
case reflect.Uint32:
if !flags.pflag {
return fmt.Errorf("cannot support uint32 field at '%v' with stdlib flag pkg.", flagName)
}
p := f.Addr().Interface().(*uint32)
flags.uint32(p, flagName, shorthand, *p, flagHelp(ft))
case reflect.Uint8:
if !flags.pflag {
return fmt.Errorf("cannot support uint8 field at '%v' with stdlib flag pkg.", flagName)
}
p := f.Addr().Interface().(*uint8)
flags.uint8(p, flagName, shorthand, *p, flagHelp(ft))
case reflect.Int8:
if !flags.pflag {
return fmt.Errorf("cannot support int8 field at '%v' with stdlib flag pkg.", flagName)
}
p := f.Addr().Interface().(*int8)
flags.int8(p, flagName, shorthand, *p, flagHelp(ft))

All of these type assertions will panic if the type is named.

support setting custom help messages

By default, running a binary set up with commandeer will print out the available flags, but I don't think there's an easy/general way to set a usage message above the flags. Maybe I'm wrong, but need to look into this regardless.

support sub command

like docker
docker [OPTIONS] COMMAND

ex:
docker --debug run --it busybox

go:

type MyApp struct {
      Debug bool `flag:"debug"`
      CurCommand interface{} `flag:"&command"`
      Run  RunCmd `command:"run"`
      Build BuildCmd `command:"build"`
} 
type RunCmd struct {
       Interactive bool `flag:"i" help:"Keep STDIN open even if not attached"`
}

generated shortflags are potentially unstable

When new fields are added to structs, are the same short flags generated for existing fields? Are the same short flags even generated from build to build? Can we always ensure that we iterate the struct fields in the order they're defined?

Readme typo

Hi, very nice package :)

Just pointing out a small typo in your readme, I believe the following part:

$ ./myapp
5 gophers stole my jeep!
$ ./myapp
3 gophers stole my horse!

Should be:

$ ./myapp
5 gophers stole my jeep!
$ ./myapp -num 3
3 gophers stole my horse!

With the -num 3 flag included. Am I correct?

undefined: commandeer.ImplementsRunner

I tried to run myapp-cobrafy which gives undefined: commandeer.ImplementsRunner at line 31 in cobrafy/cobra.go

full error

~/dev/golang/src/github.com/jaffee/commandeer/examples/myapp/cmd/myapp-cobrafy master
» go run main.go
# github.com/jaffee/commandeer/cobrafy
../../../../cobrafy/cobra.go:31:5: undefined: commandeer.ImplementsRunner

cobrafy confused by JSON attributes

I haven't used base commandeer yet, but the wanted to try using cobrafy to reuse some structs we use for JSON parsing as arguments for a sub command we shell out to.

However, it seems like the generated Cobra.Command is confused by the omitempty on the json attribute.

See for example: https://go.dev/play/p/dTj_Fxo63sr

package main

import (
	"fmt"

	"github.com/jaffee/commandeer/cobrafy"
)

type Args struct {
	Count      int  `json:"count,omitempty"`
	Reticulate bool `json:"reticulate,omitempty"`
}

func main() {
	args := Args{
		Count:      1,
		Reticulate: false,
	}
	cmd, err := cobrafy.Command(&args)
	if err != nil {
		panic(err)
	}

	fmt.Println("Trying --count ... ")
	cmd.SetArgs([]string{"--count"})
	if err := cmd.Execute(); err != nil {
		fmt.Println(err)
	}

	fmt.Println("Trying --reticulate,omitempty ... ")
	cmd.SetArgs([]string{"--reticulate,omitempty"})
	if err := cmd.Execute(); err != nil {
		fmt.Println(err)
	} else {
		fmt.Println("Accepted")
	}
}

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.