flagen

package module
Version: v0.0.0-...-9eb7a1a Latest Latest
Warning

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

Go to latest
Published: Oct 24, 2019 License: MIT Imports: 8 Imported by: 0

README

flagen Build Status GoDoc

A command line option parser generator using command line option. It generates a useful boilerplate of option parser for various programing languages using the format of command line options that will be used.

Usage

$ flagen YOUR_TEMPLATE YOUR_COMMAND_LINE_OPTIONS...
$ flagen go -i 1 -f 1.1 -s abc -b1 -b2=true arg1 arg2
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")
}

See also other examples for Python, Ruby and Shell.

Templates

Flagen has preset templates for {go,py,rb,sh}. You can specify them as template name.

Of course you can also specify your template file path.

The template is parsed as text/template of Go.

In your template, you can use .Flags and .Args. Flags has some Flag that has Name and Value of each option. And Value has Get and Type method. Get returns the value of option. Type returns the estimated type of option (int, float, string, bool). Args has some string which is argument.

And you can use functions for converting string case. It provided from iancoleman/strcase.

Collaboration

Vim

In your source which is being opened by Vim, you can insert the boilerplate.

:r!flagen YOUR_TEMPLATE YOUR_COMMAND_LINE_OPTIONS...
Your boilerplate tool

Flagen provides generator as library. You can use it in your boilerplate tool in Go as the following,

	tmpl, err := flagen.NewTemplate(args[0])
	if err != nil {
		return err
	}
	return tmpl.Execute(outStream, args[1:])

Workarounds

Ambiguous flag

Flagen consider the flag which has no value as bool type. If you specify bool flag and argument as the following,

$ flagen TEMPLATE --bool-flag arg1

it consider the flag that has string value.

When you want to avoid the case, you can use = as the following,

$ flagen TEMPLATE --bool-flag=false arg1

Installation

$ go get github.com/monochromegane/flagen/...

License

MIT

Author

monochromegane

Documentation

Overview

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

Index

Examples

Constants

This section is empty.

Variables

View Source
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.

Functions

func Run

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

Run runs the flagen.

Types

type Flag

type Flag struct {
	Name  string
	Value value
}

Flag representation the parsed option.

type Template

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

Template is the representation of a parsed template.

func NewTemplate

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

NewTemplate creates a new Template.

func (*Template) Execute

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.

Example (Go)
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")
}
Example (Python)
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()
Example (Ruby)
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
Example (Shell)
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`

Directories

Path Synopsis
cmd

Jump to

Keyboard shortcuts

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