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 BoolVar(fs *flag.FlagSet, p *bool, names string, value bool, usage string)
- func DurationVar(fs *flag.FlagSet, p *time.Duration, names string, value time.Duration, ...)
- 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 StringVar(fs *flag.FlagSet, p *string, names, value, 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 BoolVar ¶
BoolVar registers a boolean flag under the |-separated names, as in Var. All names write to p, which is 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 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 StringVar ¶
StringVar registers a string flag under the |-separated names, as in Var. All names write to p, which is initialized to value.
Types ¶
This section is empty.