Documentation
¶
Overview ¶
Package flagx adds flag aliases and formatted help to the standard library's flag.FlagSet without changing its parsing rules.
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)
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 ¶
This section is empty.