flagen: github.com/monochromegane/flagen Index | Examples | Files

package flagen

import "github.com/monochromegane/flagen"

Package flagen provides a command line option parser generator using command line option.

Index

Examples

Package Files

flag.go flagen.go flagset.go template.go templates.go value.go

Variables

var TemplateFuncMap template.FuncMap = template.FuncMap{
    "ToSnake":              strcase.ToSnake,
    "ToScreamingSnake":     strcase.ToScreamingSnake,
    "ToKebab":              strcase.ToKebab,
    "ToScreamingKebab":     strcase.ToScreamingKebab,
    "ToDelimited":          strcase.ToDelimited,
    "ToScreamingDelimited": strcase.ToScreamingDelimited,
    "ToCamel":              strcase.ToCamel,
    "ToLowerCamel":         strcase.ToLowerCamel,
}

TemplateFuncMap has template functions for converting string case.

func Run Uses

func Run(args []string, outStream, errStream io.Writer) error

Run runs the flagen.

type Flag Uses

type Flag struct {
    Name  string
    Value value
}

Flag representation the parsed option.

type Template Uses

type Template struct {
    // contains filtered or unexported fields
}

Template is the representation of a parsed template.

func NewTemplate Uses

func NewTemplate(path string) (*Template, error)

NewTemplate creates a new Template.

func (*Template) Execute Uses

func (t *Template) Execute(outStream io.Writer, args []string) error

Execute applies a parsed template to the specified arguments, and writes the output to outStream.

Code:

tmpl, _ := NewTemplate("go")
args := []string{"-i", "1", "-f", "1.1", "-s", "abc", "-b1", "-b2=true", "arg1", "arg2"}
tmpl.Execute(os.Stdout, args)

Output:

var (
	i	int
	f	float64
	s	string
	b1	bool
	b2	bool
)

func init() {
	flag.IntVar(&i, "i", 1, "usage of i")
	flag.Float64Var(&f, "f", 1.1, "usage of f")
	flag.StringVar(&s, "s", "abc", "usage of s")
	flag.BoolVar(&b1, "b1", false, "usage of b1")
	flag.BoolVar(&b2, "b2", true, "usage of b2")
}

Code:

tmpl, _ := NewTemplate("py")
args := []string{"-i", "1", "-f", "1.1", "-s", "abc", "-b1", "-b2=true", "arg1", "arg2"}
tmpl.Execute(os.Stdout, args)

Output:

import argparse
parser = argparse.ArgumentParser()
parser.add_argument("-i", type=int, default=1, help="Help of i")
parser.add_argument("-f", type=float, default=1.1, help="Help of f")
parser.add_argument("-s", default="abc", help="Help of s")
parser.add_argument("--b1", action="store_false", help="Help of b1")
parser.add_argument("--b2", action="store_true", help="Help of b2")
parser.add_argument("arg1", help="Help of arg1")
parser.add_argument("arg2", help="Help of arg2")
args = parser.parse_args()

Code:

tmpl, _ := NewTemplate("rb")
args := []string{"-i", "1", "-f", "1.1", "-s", "abc", "-b1", "-b2=true", "arg1", "arg2"}
tmpl.Execute(os.Stdout, args)

Output:

require 'optparse'

opts = {
  i: 1,
  f: 1.1,
  s: 'abc',
  b_1: false,
  b_2: true,
}

OptionParser.new do |op|
  op.on('-i [VALUE]', 'Desc of i') {|v| opts[:i] = v.to_i }
  op.on('-f [VALUE]', 'Desc of f') {|v| opts[:f] = v.to_f }
  op.on('-s [VALUE]', 'Desc of s') {|v| opts[:s] = v }
  op.on('--b1', 'Desc of b1') {|v| opts[:b_1] = v }
  op.on('--b2', 'Desc of b2') {|v| opts[:b_2] = v }

  op.parse!(ARGV)
end

Code:

tmpl, _ := NewTemplate("sh")
args := []string{"-i", "1", "-s", "abc", "-b", "-c=true", "arg1", "arg2"}
tmpl.Execute(os.Stdout, args)

Output:

I="1"
S="abc"
B="FALSE"
C="TRUE"

while getopts i:s:bc OPT
do
  case $OPT in
    "i" ) I="$OPTARG";;
    "s" ) S="$OPTARG";;
    "b" ) B="TRUE";;
    "c" ) C="FALSE";;
  esac
done

shift `expr $OPTIND - 1`

Package flagen imports 8 packages (graph). Updated 2019-10-24. Refresh now. Tools for package owners.