Documentation
¶
Overview ¶
Package flagx adds flag aliases and formatted help to the standard library's flag.FlagSet without changing its parsing rules. Use NewFlagSet for a method-based API, or the package functions to extend an existing flag.FlagSet.
Example ¶
package main
import (
"flag"
"fmt"
"os"
"github.com/Songmu/flagx"
)
func main() {
fs := flag.NewFlagSet("myapp", flag.ContinueOnError)
fs.SetOutput(os.Stdout)
var output string
var verbose bool
flagx.StringVar(fs, &output, "output|o", "", "write to `file`")
flagx.BoolVar(fs, &verbose, "verbose|v", false, "verbose output")
fs.Usage = flagx.Usage(fs, "myapp [options] <input>", "Process input files.")
if err := fs.Parse([]string{"-o", "result.txt", "--verbose", "input.txt"}); err != nil {
return
}
fmt.Println(output, verbose, fs.Args())
fs.Usage()
}
Output: result.txt true [input.txt] Usage: myapp [options] <input> Process input files. Options: -output, -o file write to file -verbose, -v verbose output -help, -h Show this help message
Index ¶
- func Bool(fs *flag.FlagSet, names string, value bool, usage string) *bool
- func BoolFunc(fs *flag.FlagSet, names, usage string, fn func(string) error)
- func BoolVar(fs *flag.FlagSet, p *bool, names string, value bool, usage string)
- func Duration(fs *flag.FlagSet, names string, value time.Duration, usage string) *time.Duration
- func DurationVar(fs *flag.FlagSet, p *time.Duration, names string, value time.Duration, ...)
- func Float64(fs *flag.FlagSet, names string, value float64, usage string) *float64
- func Float64Var(fs *flag.FlagSet, p *float64, names string, value float64, usage string)
- func Func(fs *flag.FlagSet, names, usage string, fn func(string) error)
- func Int(fs *flag.FlagSet, names string, value int, usage string) *int
- func Int64(fs *flag.FlagSet, names string, value int64, usage string) *int64
- func Int64Var(fs *flag.FlagSet, p *int64, names string, value int64, usage string)
- func IntVar(fs *flag.FlagSet, p *int, names string, value int, usage string)
- func PrintDefaults(fs *flag.FlagSet)
- func PrintUsage(w io.Writer, fs *flag.FlagSet, synopsis, description string)
- func String(fs *flag.FlagSet, names, value, usage string) *string
- func StringVar(fs *flag.FlagSet, p *string, names, value, usage string)
- func TextVar(fs *flag.FlagSet, p encoding.TextUnmarshaler, names string, ...)
- func Uint(fs *flag.FlagSet, names string, value uint, usage string) *uint
- func Uint64(fs *flag.FlagSet, names string, value uint64, usage string) *uint64
- func Uint64Var(fs *flag.FlagSet, p *uint64, names string, value uint64, usage string)
- func UintVar(fs *flag.FlagSet, p *uint, names string, value uint, usage string)
- func Usage(fs *flag.FlagSet, synopsis, description string) func()
- func Var(fs *flag.FlagSet, value flag.Value, names, usage string)
- type FlagSet
- func (fs *FlagSet) Bool(names string, value bool, usage string) *bool
- func (fs *FlagSet) BoolFunc(names, usage string, fn func(string) error)
- func (fs *FlagSet) BoolVar(p *bool, names string, value bool, usage string)
- func (fs *FlagSet) Duration(names string, value time.Duration, usage string) *time.Duration
- func (fs *FlagSet) DurationVar(p *time.Duration, names string, value time.Duration, usage string)
- func (fs *FlagSet) Float64(names string, value float64, usage string) *float64
- func (fs *FlagSet) Float64Var(p *float64, names string, value float64, usage string)
- func (fs *FlagSet) Func(names, usage string, fn func(string) error)
- func (fs *FlagSet) Init(name string, errorHandling flag.ErrorHandling)
- func (fs *FlagSet) Int(names string, value int, usage string) *int
- func (fs *FlagSet) Int64(names string, value int64, usage string) *int64
- func (fs *FlagSet) Int64Var(p *int64, names string, value int64, usage string)
- func (fs *FlagSet) IntVar(p *int, names string, value int, usage string)
- func (fs *FlagSet) Parse(arguments []string) error
- func (fs *FlagSet) PrintDefaults()
- func (fs *FlagSet) PrintUsage(w io.Writer, synopsis, description string)
- func (fs *FlagSet) String(names, value, usage string) *string
- func (fs *FlagSet) StringVar(p *string, names, value, usage string)
- func (fs *FlagSet) TextVar(p encoding.TextUnmarshaler, names string, value encoding.TextMarshaler, ...)
- func (fs *FlagSet) Uint(names string, value uint, usage string) *uint
- func (fs *FlagSet) Uint64(names string, value uint64, usage string) *uint64
- func (fs *FlagSet) Uint64Var(p *uint64, names string, value uint64, usage string)
- func (fs *FlagSet) UintVar(p *uint, names string, value uint, usage string)
- func (fs *FlagSet) Var(value flag.Value, names, usage string)
Examples ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func Bool ¶ added in v0.0.2
Bool registers a boolean flag under the |-separated names, as in Var. It returns a pointer to the shared value, initialized to value.
func BoolFunc ¶ added in v0.0.2
BoolFunc registers a callback flag under the |-separated names, as in Var. Values are optional and default to "true" when omitted. Each occurrence calls fn with its value; a returned error stops parsing.
func BoolVar ¶
BoolVar registers a boolean flag under the |-separated names, as in Var. All names write to p, which is initialized to value.
func Duration ¶ added in v0.0.2
Duration registers a duration flag under the |-separated names, as in Var. It returns a pointer to the shared value, initialized to value.
func DurationVar ¶
func DurationVar(fs *flag.FlagSet, p *time.Duration, names string, value time.Duration, usage string)
DurationVar registers a duration flag under the |-separated names, as in Var. All names write to p, which is initialized to value.
func Float64 ¶ added in v0.0.2
Float64 registers a float64 flag under the |-separated names, as in Var. It returns a pointer to the shared value, initialized to value.
func Float64Var ¶ added in v0.0.2
Float64Var registers a float64 flag under the |-separated names, as in Var. All names write to p, which is initialized to value.
func Func ¶ added in v0.0.2
Func registers a callback flag under the |-separated names, as in Var. Each occurrence calls fn with its value; a returned error stops parsing.
func Int ¶ added in v0.0.2
Int registers an integer flag under the |-separated names, as in Var. It returns a pointer to the shared value, initialized to value.
func Int64 ¶ added in v0.0.2
Int64 registers an int64 flag under the |-separated names, as in Var. It returns a pointer to the shared value, initialized to value.
func Int64Var ¶ added in v0.0.2
Int64Var registers an int64 flag under the |-separated names, as in Var. All names write to p, which is initialized to value.
func IntVar ¶
IntVar registers an integer flag under the |-separated names, as in Var. All names write to p, which is initialized to value.
func PrintDefaults ¶
PrintDefaults writes only the formatted option list to fs.Output. Names registered together are displayed in their specified order. Options registered through flagx come first, in registration order, followed by directly registered options in lexical order. Pointers to non-zero-sized values with the same type and address are also displayed as aliases. Missing help and h names are included for flag's built-in help handling, not registered.
func PrintUsage ¶
PrintUsage writes a synopsis, an optional description, and an Options section to w. It does not change fs or its output writer.
func String ¶ added in v0.0.2
String registers a string flag under the |-separated names, as in Var. It returns a pointer to the shared value, initialized to value.
func StringVar ¶
StringVar registers a string flag under the |-separated names, as in Var. All names write to p, which is initialized to value.
func TextVar ¶ added in v0.0.2
func TextVar(fs *flag.FlagSet, p encoding.TextUnmarshaler, names string, value encoding.TextMarshaler, usage string)
TextVar registers a text flag under the |-separated names, as in Var. All names write to p, which is initialized to value. p must be a pointer; value must have its type or the type it points to.
func Uint ¶ added in v0.0.2
Uint registers an unsigned integer flag under the |-separated names, as in Var. It returns a pointer to the shared value, initialized to value.
func Uint64 ¶ added in v0.0.2
Uint64 registers a uint64 flag under the |-separated names, as in Var. It returns a pointer to the shared value, initialized to value.
func Uint64Var ¶ added in v0.0.2
Uint64Var registers a uint64 flag under the |-separated names, as in Var. All names write to p, which is initialized to value.
func UintVar ¶ added in v0.0.2
UintVar registers an unsigned integer flag under the |-separated names, as in Var. All names write to p, which is initialized to value.
Types ¶
type FlagSet ¶ added in v0.0.4
FlagSet extends flag.FlagSet with alias-aware registration and formatted help. Registration methods accept |-separated names, as in Var. The zero value has no name and uses flag.ContinueOnError. A FlagSet must not be copied after first use.
func NewFlagSet ¶ added in v0.0.4
func NewFlagSet(name string, errorHandling flag.ErrorHandling) *FlagSet
NewFlagSet returns an empty flag set with the specified name and error policy. Its default Usage prints the name and the formatted option list.
Example ¶
package main
import (
"flag"
"fmt"
"os"
"github.com/Songmu/flagx"
)
func main() {
fs := flagx.NewFlagSet("myapp", flag.ContinueOnError)
fs.SetOutput(os.Stdout)
output := fs.String("output|o", "", "write to `file`")
verbose := fs.Bool("verbose|v", false, "verbose output")
if err := fs.Parse([]string{"-o", "result.txt", "--verbose", "input.txt"}); err != nil {
return
}
fmt.Println(*output, *verbose, fs.Args())
fs.Usage()
}
Output: result.txt true [input.txt] Usage: myapp Options: -output, -o file write to file -verbose, -v verbose output -help, -h Show this help message
func (*FlagSet) BoolFunc ¶ added in v0.0.4
BoolFunc registers a boolean callback under the |-separated names, as in BoolFunc.
func (*FlagSet) Duration ¶ added in v0.0.4
Duration registers the |-separated names, as in Duration.
func (*FlagSet) DurationVar ¶ added in v0.0.4
DurationVar binds p to the |-separated names, as in DurationVar.
func (*FlagSet) Float64Var ¶ added in v0.0.4
Float64Var binds p to the |-separated names, as in Float64Var.
func (*FlagSet) Func ¶ added in v0.0.4
Func registers a callback under the |-separated names, as in Func.
func (*FlagSet) Init ¶ added in v0.0.4
func (fs *FlagSet) Init(name string, errorHandling flag.ErrorHandling)
Init sets the name and error policy, installing formatted help if Usage is nil. It preserves registered flags and any existing Usage function.
func (*FlagSet) Int64Var ¶ added in v0.0.4
Int64Var binds p to the |-separated names, as in Int64Var.
func (*FlagSet) Parse ¶ added in v0.0.4
Parse parses arguments using flag.FlagSet's rules. If Usage is nil, it installs the default formatted help before parsing.
func (*FlagSet) PrintDefaults ¶ added in v0.0.4
func (fs *FlagSet) PrintDefaults()
PrintDefaults writes the formatted option list to fs.Output.
func (*FlagSet) PrintUsage ¶ added in v0.0.4
PrintUsage writes a synopsis, optional description, and formatted option list to w without changing fs.Output.
func (*FlagSet) StringVar ¶ added in v0.0.4
StringVar binds p to the |-separated names, as in StringVar.
func (*FlagSet) TextVar ¶ added in v0.0.4
func (fs *FlagSet) TextVar(p encoding.TextUnmarshaler, names string, value encoding.TextMarshaler, usage string)
TextVar binds p to the |-separated names, as in TextVar.
func (*FlagSet) Uint64Var ¶ added in v0.0.4
Uint64Var binds p to the |-separated names, as in Uint64Var.