2010-12-03 05:34:57 +01:00
|
|
|
// Copyright 2009 The Go Authors. All rights reserved.
|
|
|
|
// Use of this source code is governed by a BSD-style
|
|
|
|
// license that can be found in the LICENSE file.
|
|
|
|
|
|
|
|
/*
|
2011-05-20 02:18:15 +02:00
|
|
|
Package flag implements command-line flag parsing.
|
2010-12-03 05:34:57 +01:00
|
|
|
|
|
|
|
Usage:
|
|
|
|
|
2012-05-04 17:01:11 +02:00
|
|
|
Define flags using flag.String(), Bool(), Int(), etc.
|
|
|
|
|
|
|
|
This declares an integer flag, -flagname, stored in the pointer ip, with type *int.
|
2010-12-03 05:34:57 +01:00
|
|
|
import "flag"
|
2012-05-04 17:01:11 +02:00
|
|
|
var ip = flag.Int("flagname", 1234, "help message for flagname")
|
2010-12-03 05:34:57 +01:00
|
|
|
If you like, you can bind the flag to a variable using the Var() functions.
|
|
|
|
var flagvar int
|
|
|
|
func init() {
|
|
|
|
flag.IntVar(&flagvar, "flagname", 1234, "help message for flagname")
|
|
|
|
}
|
|
|
|
Or you can create custom flags that satisfy the Value interface (with
|
|
|
|
pointer receivers) and couple them to flag parsing by
|
|
|
|
flag.Var(&flagVal, "name", "help message for flagname")
|
|
|
|
For such flags, the default value is just the initial value of the variable.
|
|
|
|
|
2011-01-21 19:19:03 +01:00
|
|
|
After all flags are defined, call
|
2010-12-03 05:34:57 +01:00
|
|
|
flag.Parse()
|
|
|
|
to parse the command line into the defined flags.
|
|
|
|
|
2011-01-21 19:19:03 +01:00
|
|
|
Flags may then be used directly. If you're using the flags themselves,
|
2010-12-03 05:34:57 +01:00
|
|
|
they are all pointers; if you bind to variables, they're values.
|
2012-05-04 17:01:11 +02:00
|
|
|
fmt.Println("ip has value ", *ip)
|
|
|
|
fmt.Println("flagvar has value ", flagvar)
|
2010-12-03 05:34:57 +01:00
|
|
|
|
2011-01-21 19:19:03 +01:00
|
|
|
After parsing, the arguments after the flag are available as the
|
|
|
|
slice flag.Args() or individually as flag.Arg(i).
|
|
|
|
The arguments are indexed from 0 up to flag.NArg().
|
2010-12-03 05:34:57 +01:00
|
|
|
|
|
|
|
Command line flag syntax:
|
|
|
|
-flag
|
|
|
|
-flag=x
|
|
|
|
-flag x // non-boolean flags only
|
|
|
|
One or two minus signs may be used; they are equivalent.
|
|
|
|
The last form is not permitted for boolean flags because the
|
|
|
|
meaning of the command
|
|
|
|
cmd -x *
|
|
|
|
will change if there is a file called 0, false, etc. You must
|
|
|
|
use the -flag=false form to turn off a boolean flag.
|
|
|
|
|
|
|
|
Flag parsing stops just before the first non-flag argument
|
|
|
|
("-" is a non-flag argument) or after the terminator "--".
|
|
|
|
|
|
|
|
Integer flags accept 1234, 0664, 0x1234 and may be negative.
|
|
|
|
Boolean flags may be 1, 0, t, f, true, false, TRUE, FALSE, True, False.
|
2012-02-09 09:19:58 +01:00
|
|
|
Duration flags accept any input valid for time.ParseDuration.
|
2011-01-21 19:19:03 +01:00
|
|
|
|
2011-09-16 17:47:21 +02:00
|
|
|
The default set of command-line flags is controlled by
|
|
|
|
top-level functions. The FlagSet type allows one to define
|
|
|
|
independent sets of flags, such as to implement subcommands
|
|
|
|
in a command-line interface. The methods of FlagSet are
|
|
|
|
analogous to the top-level functions for the command-line
|
|
|
|
flag set.
|
2010-12-03 05:34:57 +01:00
|
|
|
*/
|
|
|
|
package flag
|
|
|
|
|
|
|
|
import (
|
2011-12-03 03:17:34 +01:00
|
|
|
"errors"
|
2010-12-03 05:34:57 +01:00
|
|
|
"fmt"
|
2012-02-09 09:19:58 +01:00
|
|
|
"io"
|
2010-12-03 05:34:57 +01:00
|
|
|
"os"
|
2011-03-25 00:46:17 +01:00
|
|
|
"sort"
|
2010-12-03 05:34:57 +01:00
|
|
|
"strconv"
|
2012-01-25 21:56:26 +01:00
|
|
|
"time"
|
2010-12-03 05:34:57 +01:00
|
|
|
)
|
|
|
|
|
2011-09-16 17:47:21 +02:00
|
|
|
// ErrHelp is the error returned if the flag -help is invoked but no such flag is defined.
|
2011-12-03 03:17:34 +01:00
|
|
|
var ErrHelp = errors.New("flag: help requested")
|
2011-09-16 17:47:21 +02:00
|
|
|
|
2012-01-25 21:56:26 +01:00
|
|
|
// -- bool Value
|
2010-12-03 05:34:57 +01:00
|
|
|
type boolValue bool
|
|
|
|
|
|
|
|
func newBoolValue(val bool, p *bool) *boolValue {
|
|
|
|
*p = val
|
|
|
|
return (*boolValue)(p)
|
|
|
|
}
|
|
|
|
|
2012-01-25 21:56:26 +01:00
|
|
|
func (b *boolValue) Set(s string) error {
|
2011-12-14 16:41:54 +01:00
|
|
|
v, err := strconv.ParseBool(s)
|
2010-12-03 05:34:57 +01:00
|
|
|
*b = boolValue(v)
|
2012-01-25 21:56:26 +01:00
|
|
|
return err
|
2010-12-03 05:34:57 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
func (b *boolValue) String() string { return fmt.Sprintf("%v", *b) }
|
|
|
|
|
2012-01-25 21:56:26 +01:00
|
|
|
// -- int Value
|
2010-12-03 05:34:57 +01:00
|
|
|
type intValue int
|
|
|
|
|
|
|
|
func newIntValue(val int, p *int) *intValue {
|
|
|
|
*p = val
|
|
|
|
return (*intValue)(p)
|
|
|
|
}
|
|
|
|
|
2012-01-25 21:56:26 +01:00
|
|
|
func (i *intValue) Set(s string) error {
|
2011-12-14 16:41:54 +01:00
|
|
|
v, err := strconv.ParseInt(s, 0, 64)
|
2010-12-03 05:34:57 +01:00
|
|
|
*i = intValue(v)
|
2012-01-25 21:56:26 +01:00
|
|
|
return err
|
2010-12-03 05:34:57 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
func (i *intValue) String() string { return fmt.Sprintf("%v", *i) }
|
|
|
|
|
2012-01-25 21:56:26 +01:00
|
|
|
// -- int64 Value
|
2010-12-03 05:34:57 +01:00
|
|
|
type int64Value int64
|
|
|
|
|
|
|
|
func newInt64Value(val int64, p *int64) *int64Value {
|
|
|
|
*p = val
|
|
|
|
return (*int64Value)(p)
|
|
|
|
}
|
|
|
|
|
2012-01-25 21:56:26 +01:00
|
|
|
func (i *int64Value) Set(s string) error {
|
2011-12-14 16:41:54 +01:00
|
|
|
v, err := strconv.ParseInt(s, 0, 64)
|
2010-12-03 05:34:57 +01:00
|
|
|
*i = int64Value(v)
|
2012-01-25 21:56:26 +01:00
|
|
|
return err
|
2010-12-03 05:34:57 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
func (i *int64Value) String() string { return fmt.Sprintf("%v", *i) }
|
|
|
|
|
2012-01-25 21:56:26 +01:00
|
|
|
// -- uint Value
|
2010-12-03 05:34:57 +01:00
|
|
|
type uintValue uint
|
|
|
|
|
|
|
|
func newUintValue(val uint, p *uint) *uintValue {
|
|
|
|
*p = val
|
|
|
|
return (*uintValue)(p)
|
|
|
|
}
|
|
|
|
|
2012-01-25 21:56:26 +01:00
|
|
|
func (i *uintValue) Set(s string) error {
|
2011-12-14 16:41:54 +01:00
|
|
|
v, err := strconv.ParseUint(s, 0, 64)
|
2010-12-03 05:34:57 +01:00
|
|
|
*i = uintValue(v)
|
2012-01-25 21:56:26 +01:00
|
|
|
return err
|
2010-12-03 05:34:57 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
func (i *uintValue) String() string { return fmt.Sprintf("%v", *i) }
|
|
|
|
|
|
|
|
// -- uint64 Value
|
|
|
|
type uint64Value uint64
|
|
|
|
|
|
|
|
func newUint64Value(val uint64, p *uint64) *uint64Value {
|
|
|
|
*p = val
|
|
|
|
return (*uint64Value)(p)
|
|
|
|
}
|
|
|
|
|
2012-01-25 21:56:26 +01:00
|
|
|
func (i *uint64Value) Set(s string) error {
|
2011-12-14 16:41:54 +01:00
|
|
|
v, err := strconv.ParseUint(s, 0, 64)
|
2010-12-03 05:34:57 +01:00
|
|
|
*i = uint64Value(v)
|
2012-01-25 21:56:26 +01:00
|
|
|
return err
|
2010-12-03 05:34:57 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
func (i *uint64Value) String() string { return fmt.Sprintf("%v", *i) }
|
|
|
|
|
|
|
|
// -- string Value
|
|
|
|
type stringValue string
|
|
|
|
|
|
|
|
func newStringValue(val string, p *string) *stringValue {
|
|
|
|
*p = val
|
|
|
|
return (*stringValue)(p)
|
|
|
|
}
|
|
|
|
|
2012-01-25 21:56:26 +01:00
|
|
|
func (s *stringValue) Set(val string) error {
|
2010-12-03 05:34:57 +01:00
|
|
|
*s = stringValue(val)
|
2012-01-25 21:56:26 +01:00
|
|
|
return nil
|
2010-12-03 05:34:57 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
func (s *stringValue) String() string { return fmt.Sprintf("%s", *s) }
|
|
|
|
|
2012-01-25 21:56:26 +01:00
|
|
|
// -- float64 Value
|
2010-12-03 05:34:57 +01:00
|
|
|
type float64Value float64
|
|
|
|
|
|
|
|
func newFloat64Value(val float64, p *float64) *float64Value {
|
|
|
|
*p = val
|
|
|
|
return (*float64Value)(p)
|
|
|
|
}
|
|
|
|
|
2012-01-25 21:56:26 +01:00
|
|
|
func (f *float64Value) Set(s string) error {
|
2011-12-14 16:41:54 +01:00
|
|
|
v, err := strconv.ParseFloat(s, 64)
|
2010-12-03 05:34:57 +01:00
|
|
|
*f = float64Value(v)
|
2012-01-25 21:56:26 +01:00
|
|
|
return err
|
2010-12-03 05:34:57 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
func (f *float64Value) String() string { return fmt.Sprintf("%v", *f) }
|
|
|
|
|
2012-01-25 21:56:26 +01:00
|
|
|
// -- time.Duration Value
|
|
|
|
type durationValue time.Duration
|
|
|
|
|
|
|
|
func newDurationValue(val time.Duration, p *time.Duration) *durationValue {
|
|
|
|
*p = val
|
|
|
|
return (*durationValue)(p)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (d *durationValue) Set(s string) error {
|
|
|
|
v, err := time.ParseDuration(s)
|
|
|
|
*d = durationValue(v)
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
func (d *durationValue) String() string { return (*time.Duration)(d).String() }
|
|
|
|
|
2010-12-03 05:34:57 +01:00
|
|
|
// Value is the interface to the dynamic value stored in a flag.
|
|
|
|
// (The default value is represented as a string.)
|
|
|
|
type Value interface {
|
|
|
|
String() string
|
2012-01-25 21:56:26 +01:00
|
|
|
Set(string) error
|
2010-12-03 05:34:57 +01:00
|
|
|
}
|
|
|
|
|
2011-09-16 17:47:21 +02:00
|
|
|
// ErrorHandling defines how to handle flag parsing errors.
|
|
|
|
type ErrorHandling int
|
|
|
|
|
|
|
|
const (
|
|
|
|
ContinueOnError ErrorHandling = iota
|
|
|
|
ExitOnError
|
|
|
|
PanicOnError
|
|
|
|
)
|
|
|
|
|
|
|
|
// A FlagSet represents a set of defined flags.
|
|
|
|
type FlagSet struct {
|
|
|
|
// Usage is the function called when an error occurs while parsing flags.
|
|
|
|
// The field is a function (not a method) that may be changed to point to
|
|
|
|
// a custom error handler.
|
|
|
|
Usage func()
|
|
|
|
|
|
|
|
name string
|
2011-10-27 01:57:58 +02:00
|
|
|
parsed bool
|
2011-09-16 17:47:21 +02:00
|
|
|
actual map[string]*Flag
|
|
|
|
formal map[string]*Flag
|
|
|
|
args []string // arguments after flags
|
|
|
|
exitOnError bool // does the program exit if there's an error?
|
|
|
|
errorHandling ErrorHandling
|
2012-02-09 09:19:58 +01:00
|
|
|
output io.Writer // nil means stderr; use out() accessor
|
2011-09-16 17:47:21 +02:00
|
|
|
}
|
|
|
|
|
2010-12-03 05:34:57 +01:00
|
|
|
// A Flag represents the state of a flag.
|
|
|
|
type Flag struct {
|
|
|
|
Name string // name as it appears on command line
|
|
|
|
Usage string // help message
|
|
|
|
Value Value // value as set
|
|
|
|
DefValue string // default value (as text); for usage message
|
|
|
|
}
|
|
|
|
|
2011-03-25 00:46:17 +01:00
|
|
|
// sortFlags returns the flags as a slice in lexicographical sorted order.
|
|
|
|
func sortFlags(flags map[string]*Flag) []*Flag {
|
2011-09-16 17:47:21 +02:00
|
|
|
list := make(sort.StringSlice, len(flags))
|
2011-03-25 00:46:17 +01:00
|
|
|
i := 0
|
|
|
|
for _, f := range flags {
|
|
|
|
list[i] = f.Name
|
|
|
|
i++
|
|
|
|
}
|
|
|
|
list.Sort()
|
|
|
|
result := make([]*Flag, len(list))
|
|
|
|
for i, name := range list {
|
|
|
|
result[i] = flags[name]
|
|
|
|
}
|
|
|
|
return result
|
|
|
|
}
|
|
|
|
|
2012-02-09 09:19:58 +01:00
|
|
|
func (f *FlagSet) out() io.Writer {
|
|
|
|
if f.output == nil {
|
|
|
|
return os.Stderr
|
|
|
|
}
|
|
|
|
return f.output
|
|
|
|
}
|
|
|
|
|
|
|
|
// SetOutput sets the destination for usage and error messages.
|
|
|
|
// If output is nil, os.Stderr is used.
|
|
|
|
func (f *FlagSet) SetOutput(output io.Writer) {
|
|
|
|
f.output = output
|
|
|
|
}
|
|
|
|
|
2011-03-25 00:46:17 +01:00
|
|
|
// VisitAll visits the flags in lexicographical order, calling fn for each.
|
|
|
|
// It visits all flags, even those not set.
|
2011-09-16 17:47:21 +02:00
|
|
|
func (f *FlagSet) VisitAll(fn func(*Flag)) {
|
|
|
|
for _, flag := range sortFlags(f.formal) {
|
|
|
|
fn(flag)
|
2010-12-03 05:34:57 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-09-16 17:47:21 +02:00
|
|
|
// VisitAll visits the command-line flags in lexicographical order, calling
|
|
|
|
// fn for each. It visits all flags, even those not set.
|
|
|
|
func VisitAll(fn func(*Flag)) {
|
|
|
|
commandLine.VisitAll(fn)
|
|
|
|
}
|
|
|
|
|
2011-03-25 00:46:17 +01:00
|
|
|
// Visit visits the flags in lexicographical order, calling fn for each.
|
|
|
|
// It visits only those flags that have been set.
|
2011-09-16 17:47:21 +02:00
|
|
|
func (f *FlagSet) Visit(fn func(*Flag)) {
|
|
|
|
for _, flag := range sortFlags(f.actual) {
|
|
|
|
fn(flag)
|
2010-12-03 05:34:57 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-09-16 17:47:21 +02:00
|
|
|
// Visit visits the command-line flags in lexicographical order, calling fn
|
|
|
|
// for each. It visits only those flags that have been set.
|
|
|
|
func Visit(fn func(*Flag)) {
|
|
|
|
commandLine.Visit(fn)
|
|
|
|
}
|
|
|
|
|
2010-12-03 05:34:57 +01:00
|
|
|
// Lookup returns the Flag structure of the named flag, returning nil if none exists.
|
2011-09-16 17:47:21 +02:00
|
|
|
func (f *FlagSet) Lookup(name string) *Flag {
|
|
|
|
return f.formal[name]
|
|
|
|
}
|
|
|
|
|
|
|
|
// Lookup returns the Flag structure of the named command-line flag,
|
|
|
|
// returning nil if none exists.
|
2010-12-03 05:34:57 +01:00
|
|
|
func Lookup(name string) *Flag {
|
2011-09-16 17:47:21 +02:00
|
|
|
return commandLine.formal[name]
|
2010-12-03 05:34:57 +01:00
|
|
|
}
|
|
|
|
|
2012-01-25 21:56:26 +01:00
|
|
|
// Set sets the value of the named flag.
|
|
|
|
func (f *FlagSet) Set(name, value string) error {
|
2011-09-16 17:47:21 +02:00
|
|
|
flag, ok := f.formal[name]
|
2010-12-03 05:34:57 +01:00
|
|
|
if !ok {
|
2012-01-25 21:56:26 +01:00
|
|
|
return fmt.Errorf("no such flag -%v", name)
|
2010-12-03 05:34:57 +01:00
|
|
|
}
|
2012-01-25 21:56:26 +01:00
|
|
|
err := flag.Value.Set(value)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
2010-12-03 05:34:57 +01:00
|
|
|
}
|
2011-10-27 01:57:58 +02:00
|
|
|
if f.actual == nil {
|
|
|
|
f.actual = make(map[string]*Flag)
|
|
|
|
}
|
2011-09-16 17:47:21 +02:00
|
|
|
f.actual[name] = flag
|
2012-01-25 21:56:26 +01:00
|
|
|
return nil
|
2010-12-03 05:34:57 +01:00
|
|
|
}
|
|
|
|
|
2012-01-25 21:56:26 +01:00
|
|
|
// Set sets the value of the named command-line flag.
|
|
|
|
func Set(name, value string) error {
|
2011-09-16 17:47:21 +02:00
|
|
|
return commandLine.Set(name, value)
|
|
|
|
}
|
|
|
|
|
2012-02-09 09:19:58 +01:00
|
|
|
// PrintDefaults prints, to standard error unless configured
|
|
|
|
// otherwise, the default values of all defined flags in the set.
|
2011-09-16 17:47:21 +02:00
|
|
|
func (f *FlagSet) PrintDefaults() {
|
2012-02-09 09:19:58 +01:00
|
|
|
f.VisitAll(func(flag *Flag) {
|
2010-12-03 05:34:57 +01:00
|
|
|
format := " -%s=%s: %s\n"
|
2012-02-09 09:19:58 +01:00
|
|
|
if _, ok := flag.Value.(*stringValue); ok {
|
2010-12-03 05:34:57 +01:00
|
|
|
// put quotes on the value
|
|
|
|
format = " -%s=%q: %s\n"
|
|
|
|
}
|
2012-02-09 09:19:58 +01:00
|
|
|
fmt.Fprintf(f.out(), format, flag.Name, flag.DefValue, flag.Usage)
|
2010-12-03 05:34:57 +01:00
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2011-09-16 17:47:21 +02:00
|
|
|
// PrintDefaults prints to standard error the default values of all defined command-line flags.
|
|
|
|
func PrintDefaults() {
|
|
|
|
commandLine.PrintDefaults()
|
|
|
|
}
|
|
|
|
|
|
|
|
// defaultUsage is the default function to print a usage message.
|
|
|
|
func defaultUsage(f *FlagSet) {
|
2012-02-09 09:19:58 +01:00
|
|
|
fmt.Fprintf(f.out(), "Usage of %s:\n", f.name)
|
2011-09-16 17:47:21 +02:00
|
|
|
f.PrintDefaults()
|
|
|
|
}
|
|
|
|
|
2011-10-27 01:57:58 +02:00
|
|
|
// NOTE: Usage is not just defaultUsage(commandLine)
|
|
|
|
// because it serves (via godoc flag Usage) as the example
|
|
|
|
// for how to write your own usage function.
|
|
|
|
|
2011-09-16 17:47:21 +02:00
|
|
|
// Usage prints to standard error a usage message documenting all defined command-line flags.
|
2010-12-03 05:34:57 +01:00
|
|
|
// The function is a variable that may be changed to point to a custom function.
|
|
|
|
var Usage = func() {
|
2011-10-27 01:57:58 +02:00
|
|
|
fmt.Fprintf(os.Stderr, "Usage of %s:\n", os.Args[0])
|
|
|
|
PrintDefaults()
|
2010-12-03 05:34:57 +01:00
|
|
|
}
|
|
|
|
|
2011-09-16 17:47:21 +02:00
|
|
|
// NFlag returns the number of flags that have been set.
|
|
|
|
func (f *FlagSet) NFlag() int { return len(f.actual) }
|
2010-12-03 05:34:57 +01:00
|
|
|
|
2011-09-16 17:47:21 +02:00
|
|
|
// NFlag returns the number of command-line flags that have been set.
|
|
|
|
func NFlag() int { return len(commandLine.actual) }
|
|
|
|
|
|
|
|
// Arg returns the i'th argument. Arg(0) is the first remaining argument
|
|
|
|
// after flags have been processed.
|
|
|
|
func (f *FlagSet) Arg(i int) string {
|
|
|
|
if i < 0 || i >= len(f.args) {
|
|
|
|
return ""
|
2010-12-03 05:34:57 +01:00
|
|
|
}
|
2011-09-16 17:47:21 +02:00
|
|
|
return f.args[i]
|
2010-12-03 05:34:57 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// Arg returns the i'th command-line argument. Arg(0) is the first remaining argument
|
|
|
|
// after flags have been processed.
|
|
|
|
func Arg(i int) string {
|
2011-09-16 17:47:21 +02:00
|
|
|
return commandLine.Arg(i)
|
2010-12-03 05:34:57 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// NArg is the number of arguments remaining after flags have been processed.
|
2011-09-16 17:47:21 +02:00
|
|
|
func (f *FlagSet) NArg() int { return len(f.args) }
|
|
|
|
|
|
|
|
// NArg is the number of arguments remaining after flags have been processed.
|
|
|
|
func NArg() int { return len(commandLine.args) }
|
|
|
|
|
|
|
|
// Args returns the non-flag arguments.
|
|
|
|
func (f *FlagSet) Args() []string { return f.args }
|
2010-12-03 05:34:57 +01:00
|
|
|
|
|
|
|
// Args returns the non-flag command-line arguments.
|
2011-09-16 17:47:21 +02:00
|
|
|
func Args() []string { return commandLine.args }
|
|
|
|
|
|
|
|
// BoolVar defines a bool flag with specified name, default value, and usage string.
|
|
|
|
// The argument p points to a bool variable in which to store the value of the flag.
|
|
|
|
func (f *FlagSet) BoolVar(p *bool, name string, value bool, usage string) {
|
|
|
|
f.Var(newBoolValue(value, p), name, usage)
|
|
|
|
}
|
2010-12-03 05:34:57 +01:00
|
|
|
|
|
|
|
// BoolVar defines a bool flag with specified name, default value, and usage string.
|
|
|
|
// The argument p points to a bool variable in which to store the value of the flag.
|
|
|
|
func BoolVar(p *bool, name string, value bool, usage string) {
|
2011-09-16 17:47:21 +02:00
|
|
|
commandLine.Var(newBoolValue(value, p), name, usage)
|
2010-12-03 05:34:57 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// Bool defines a bool flag with specified name, default value, and usage string.
|
|
|
|
// The return value is the address of a bool variable that stores the value of the flag.
|
2011-09-16 17:47:21 +02:00
|
|
|
func (f *FlagSet) Bool(name string, value bool, usage string) *bool {
|
2010-12-03 05:34:57 +01:00
|
|
|
p := new(bool)
|
2011-09-16 17:47:21 +02:00
|
|
|
f.BoolVar(p, name, value, usage)
|
2010-12-03 05:34:57 +01:00
|
|
|
return p
|
|
|
|
}
|
|
|
|
|
2011-09-16 17:47:21 +02:00
|
|
|
// Bool defines a bool flag with specified name, default value, and usage string.
|
|
|
|
// The return value is the address of a bool variable that stores the value of the flag.
|
|
|
|
func Bool(name string, value bool, usage string) *bool {
|
|
|
|
return commandLine.Bool(name, value, usage)
|
|
|
|
}
|
|
|
|
|
|
|
|
// IntVar defines an int flag with specified name, default value, and usage string.
|
|
|
|
// The argument p points to an int variable in which to store the value of the flag.
|
|
|
|
func (f *FlagSet) IntVar(p *int, name string, value int, usage string) {
|
|
|
|
f.Var(newIntValue(value, p), name, usage)
|
|
|
|
}
|
|
|
|
|
2010-12-03 05:34:57 +01:00
|
|
|
// IntVar defines an int flag with specified name, default value, and usage string.
|
|
|
|
// The argument p points to an int variable in which to store the value of the flag.
|
|
|
|
func IntVar(p *int, name string, value int, usage string) {
|
2011-09-16 17:47:21 +02:00
|
|
|
commandLine.Var(newIntValue(value, p), name, usage)
|
2010-12-03 05:34:57 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// Int defines an int flag with specified name, default value, and usage string.
|
|
|
|
// The return value is the address of an int variable that stores the value of the flag.
|
2011-09-16 17:47:21 +02:00
|
|
|
func (f *FlagSet) Int(name string, value int, usage string) *int {
|
2010-12-03 05:34:57 +01:00
|
|
|
p := new(int)
|
2011-09-16 17:47:21 +02:00
|
|
|
f.IntVar(p, name, value, usage)
|
2010-12-03 05:34:57 +01:00
|
|
|
return p
|
|
|
|
}
|
|
|
|
|
2011-09-16 17:47:21 +02:00
|
|
|
// Int defines an int flag with specified name, default value, and usage string.
|
|
|
|
// The return value is the address of an int variable that stores the value of the flag.
|
|
|
|
func Int(name string, value int, usage string) *int {
|
|
|
|
return commandLine.Int(name, value, usage)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Int64Var defines an int64 flag with specified name, default value, and usage string.
|
|
|
|
// The argument p points to an int64 variable in which to store the value of the flag.
|
|
|
|
func (f *FlagSet) Int64Var(p *int64, name string, value int64, usage string) {
|
|
|
|
f.Var(newInt64Value(value, p), name, usage)
|
|
|
|
}
|
|
|
|
|
2010-12-03 05:34:57 +01:00
|
|
|
// Int64Var defines an int64 flag with specified name, default value, and usage string.
|
|
|
|
// The argument p points to an int64 variable in which to store the value of the flag.
|
|
|
|
func Int64Var(p *int64, name string, value int64, usage string) {
|
2011-09-16 17:47:21 +02:00
|
|
|
commandLine.Var(newInt64Value(value, p), name, usage)
|
2010-12-03 05:34:57 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// Int64 defines an int64 flag with specified name, default value, and usage string.
|
|
|
|
// The return value is the address of an int64 variable that stores the value of the flag.
|
2011-09-16 17:47:21 +02:00
|
|
|
func (f *FlagSet) Int64(name string, value int64, usage string) *int64 {
|
2010-12-03 05:34:57 +01:00
|
|
|
p := new(int64)
|
2011-09-16 17:47:21 +02:00
|
|
|
f.Int64Var(p, name, value, usage)
|
2010-12-03 05:34:57 +01:00
|
|
|
return p
|
|
|
|
}
|
|
|
|
|
2011-09-16 17:47:21 +02:00
|
|
|
// Int64 defines an int64 flag with specified name, default value, and usage string.
|
|
|
|
// The return value is the address of an int64 variable that stores the value of the flag.
|
|
|
|
func Int64(name string, value int64, usage string) *int64 {
|
|
|
|
return commandLine.Int64(name, value, usage)
|
|
|
|
}
|
|
|
|
|
2010-12-03 05:34:57 +01:00
|
|
|
// UintVar defines a uint flag with specified name, default value, and usage string.
|
|
|
|
// The argument p points to a uint variable in which to store the value of the flag.
|
2011-09-16 17:47:21 +02:00
|
|
|
func (f *FlagSet) UintVar(p *uint, name string, value uint, usage string) {
|
|
|
|
f.Var(newUintValue(value, p), name, usage)
|
|
|
|
}
|
|
|
|
|
|
|
|
// UintVar defines a uint flag with specified name, default value, and usage string.
|
|
|
|
// The argument p points to a uint variable in which to store the value of the flag.
|
2010-12-03 05:34:57 +01:00
|
|
|
func UintVar(p *uint, name string, value uint, usage string) {
|
2011-09-16 17:47:21 +02:00
|
|
|
commandLine.Var(newUintValue(value, p), name, usage)
|
2010-12-03 05:34:57 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// Uint defines a uint flag with specified name, default value, and usage string.
|
2011-09-16 17:47:21 +02:00
|
|
|
// The return value is the address of a uint variable that stores the value of the flag.
|
|
|
|
func (f *FlagSet) Uint(name string, value uint, usage string) *uint {
|
2010-12-03 05:34:57 +01:00
|
|
|
p := new(uint)
|
2011-09-16 17:47:21 +02:00
|
|
|
f.UintVar(p, name, value, usage)
|
2010-12-03 05:34:57 +01:00
|
|
|
return p
|
|
|
|
}
|
|
|
|
|
2011-09-16 17:47:21 +02:00
|
|
|
// Uint defines a uint flag with specified name, default value, and usage string.
|
|
|
|
// The return value is the address of a uint variable that stores the value of the flag.
|
|
|
|
func Uint(name string, value uint, usage string) *uint {
|
|
|
|
return commandLine.Uint(name, value, usage)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Uint64Var defines a uint64 flag with specified name, default value, and usage string.
|
|
|
|
// The argument p points to a uint64 variable in which to store the value of the flag.
|
|
|
|
func (f *FlagSet) Uint64Var(p *uint64, name string, value uint64, usage string) {
|
|
|
|
f.Var(newUint64Value(value, p), name, usage)
|
|
|
|
}
|
|
|
|
|
2010-12-03 05:34:57 +01:00
|
|
|
// Uint64Var defines a uint64 flag with specified name, default value, and usage string.
|
|
|
|
// The argument p points to a uint64 variable in which to store the value of the flag.
|
|
|
|
func Uint64Var(p *uint64, name string, value uint64, usage string) {
|
2011-09-16 17:47:21 +02:00
|
|
|
commandLine.Var(newUint64Value(value, p), name, usage)
|
2010-12-03 05:34:57 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// Uint64 defines a uint64 flag with specified name, default value, and usage string.
|
|
|
|
// The return value is the address of a uint64 variable that stores the value of the flag.
|
2011-09-16 17:47:21 +02:00
|
|
|
func (f *FlagSet) Uint64(name string, value uint64, usage string) *uint64 {
|
2010-12-03 05:34:57 +01:00
|
|
|
p := new(uint64)
|
2011-09-16 17:47:21 +02:00
|
|
|
f.Uint64Var(p, name, value, usage)
|
2010-12-03 05:34:57 +01:00
|
|
|
return p
|
|
|
|
}
|
|
|
|
|
2011-09-16 17:47:21 +02:00
|
|
|
// Uint64 defines a uint64 flag with specified name, default value, and usage string.
|
|
|
|
// The return value is the address of a uint64 variable that stores the value of the flag.
|
|
|
|
func Uint64(name string, value uint64, usage string) *uint64 {
|
|
|
|
return commandLine.Uint64(name, value, usage)
|
|
|
|
}
|
|
|
|
|
|
|
|
// StringVar defines a string flag with specified name, default value, and usage string.
|
|
|
|
// The argument p points to a string variable in which to store the value of the flag.
|
|
|
|
func (f *FlagSet) StringVar(p *string, name string, value string, usage string) {
|
|
|
|
f.Var(newStringValue(value, p), name, usage)
|
|
|
|
}
|
|
|
|
|
2010-12-03 05:34:57 +01:00
|
|
|
// StringVar defines a string flag with specified name, default value, and usage string.
|
|
|
|
// The argument p points to a string variable in which to store the value of the flag.
|
2011-09-16 17:47:21 +02:00
|
|
|
func StringVar(p *string, name string, value string, usage string) {
|
|
|
|
commandLine.Var(newStringValue(value, p), name, usage)
|
2010-12-03 05:34:57 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// String defines a string flag with specified name, default value, and usage string.
|
|
|
|
// The return value is the address of a string variable that stores the value of the flag.
|
2011-09-16 17:47:21 +02:00
|
|
|
func (f *FlagSet) String(name string, value string, usage string) *string {
|
2010-12-03 05:34:57 +01:00
|
|
|
p := new(string)
|
2011-09-16 17:47:21 +02:00
|
|
|
f.StringVar(p, name, value, usage)
|
2010-12-03 05:34:57 +01:00
|
|
|
return p
|
|
|
|
}
|
|
|
|
|
2011-09-16 17:47:21 +02:00
|
|
|
// String defines a string flag with specified name, default value, and usage string.
|
|
|
|
// The return value is the address of a string variable that stores the value of the flag.
|
|
|
|
func String(name string, value string, usage string) *string {
|
|
|
|
return commandLine.String(name, value, usage)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Float64Var defines a float64 flag with specified name, default value, and usage string.
|
|
|
|
// The argument p points to a float64 variable in which to store the value of the flag.
|
|
|
|
func (f *FlagSet) Float64Var(p *float64, name string, value float64, usage string) {
|
|
|
|
f.Var(newFloat64Value(value, p), name, usage)
|
|
|
|
}
|
|
|
|
|
2010-12-03 05:34:57 +01:00
|
|
|
// Float64Var defines a float64 flag with specified name, default value, and usage string.
|
|
|
|
// The argument p points to a float64 variable in which to store the value of the flag.
|
|
|
|
func Float64Var(p *float64, name string, value float64, usage string) {
|
2011-09-16 17:47:21 +02:00
|
|
|
commandLine.Var(newFloat64Value(value, p), name, usage)
|
2010-12-03 05:34:57 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// Float64 defines a float64 flag with specified name, default value, and usage string.
|
|
|
|
// The return value is the address of a float64 variable that stores the value of the flag.
|
2011-09-16 17:47:21 +02:00
|
|
|
func (f *FlagSet) Float64(name string, value float64, usage string) *float64 {
|
2010-12-03 05:34:57 +01:00
|
|
|
p := new(float64)
|
2011-09-16 17:47:21 +02:00
|
|
|
f.Float64Var(p, name, value, usage)
|
2010-12-03 05:34:57 +01:00
|
|
|
return p
|
|
|
|
}
|
|
|
|
|
2012-01-25 21:56:26 +01:00
|
|
|
// Float64 defines a float64 flag with specified name, default value, and usage string.
|
2011-09-16 17:47:21 +02:00
|
|
|
// The return value is the address of a float64 variable that stores the value of the flag.
|
|
|
|
func Float64(name string, value float64, usage string) *float64 {
|
|
|
|
return commandLine.Float64(name, value, usage)
|
|
|
|
}
|
|
|
|
|
2012-01-25 21:56:26 +01:00
|
|
|
// DurationVar defines a time.Duration flag with specified name, default value, and usage string.
|
|
|
|
// The argument p points to a time.Duration variable in which to store the value of the flag.
|
|
|
|
func (f *FlagSet) DurationVar(p *time.Duration, name string, value time.Duration, usage string) {
|
|
|
|
f.Var(newDurationValue(value, p), name, usage)
|
|
|
|
}
|
|
|
|
|
|
|
|
// DurationVar defines a time.Duration flag with specified name, default value, and usage string.
|
|
|
|
// The argument p points to a time.Duration variable in which to store the value of the flag.
|
|
|
|
func DurationVar(p *time.Duration, name string, value time.Duration, usage string) {
|
|
|
|
commandLine.Var(newDurationValue(value, p), name, usage)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Duration defines a time.Duration flag with specified name, default value, and usage string.
|
|
|
|
// The return value is the address of a time.Duration variable that stores the value of the flag.
|
|
|
|
func (f *FlagSet) Duration(name string, value time.Duration, usage string) *time.Duration {
|
|
|
|
p := new(time.Duration)
|
|
|
|
f.DurationVar(p, name, value, usage)
|
|
|
|
return p
|
|
|
|
}
|
|
|
|
|
|
|
|
// Duration defines a time.Duration flag with specified name, default value, and usage string.
|
|
|
|
// The return value is the address of a time.Duration variable that stores the value of the flag.
|
|
|
|
func Duration(name string, value time.Duration, usage string) *time.Duration {
|
|
|
|
return commandLine.Duration(name, value, usage)
|
|
|
|
}
|
|
|
|
|
2011-09-16 17:47:21 +02:00
|
|
|
// Var defines a flag with the specified name and usage string. The type and
|
|
|
|
// value of the flag are represented by the first argument, of type Value, which
|
|
|
|
// typically holds a user-defined implementation of Value. For instance, the
|
|
|
|
// caller could create a flag that turns a comma-separated string into a slice
|
|
|
|
// of strings by giving the slice the methods of Value; in particular, Set would
|
|
|
|
// decompose the comma-separated string into the slice.
|
|
|
|
func (f *FlagSet) Var(value Value, name string, usage string) {
|
2010-12-03 05:34:57 +01:00
|
|
|
// Remember the default value as a string; it won't change.
|
2011-09-16 17:47:21 +02:00
|
|
|
flag := &Flag{name, usage, value, value.String()}
|
|
|
|
_, alreadythere := f.formal[name]
|
2010-12-03 05:34:57 +01:00
|
|
|
if alreadythere {
|
2012-06-25 18:20:03 +02:00
|
|
|
msg := fmt.Sprintf("%s flag redefined: %s", f.name, name)
|
|
|
|
fmt.Fprintln(f.out(), msg)
|
|
|
|
panic(msg) // Happens only if flags are declared with identical names
|
2010-12-03 05:34:57 +01:00
|
|
|
}
|
2011-10-27 01:57:58 +02:00
|
|
|
if f.formal == nil {
|
|
|
|
f.formal = make(map[string]*Flag)
|
|
|
|
}
|
2011-09-16 17:47:21 +02:00
|
|
|
f.formal[name] = flag
|
|
|
|
}
|
|
|
|
|
|
|
|
// Var defines a flag with the specified name and usage string. The type and
|
|
|
|
// value of the flag are represented by the first argument, of type Value, which
|
|
|
|
// typically holds a user-defined implementation of Value. For instance, the
|
|
|
|
// caller could create a flag that turns a comma-separated string into a slice
|
|
|
|
// of strings by giving the slice the methods of Value; in particular, Set would
|
|
|
|
// decompose the comma-separated string into the slice.
|
|
|
|
func Var(value Value, name string, usage string) {
|
|
|
|
commandLine.Var(value, name, usage)
|
|
|
|
}
|
|
|
|
|
|
|
|
// failf prints to standard error a formatted error and usage message and
|
|
|
|
// returns the error.
|
2011-12-03 03:17:34 +01:00
|
|
|
func (f *FlagSet) failf(format string, a ...interface{}) error {
|
2011-09-16 17:47:21 +02:00
|
|
|
err := fmt.Errorf(format, a...)
|
2012-02-09 09:19:58 +01:00
|
|
|
fmt.Fprintln(f.out(), err)
|
2011-09-16 17:47:21 +02:00
|
|
|
f.usage()
|
|
|
|
return err
|
2010-12-03 05:34:57 +01:00
|
|
|
}
|
|
|
|
|
2011-09-16 17:47:21 +02:00
|
|
|
// usage calls the Usage method for the flag set, or the usage function if
|
|
|
|
// the flag set is commandLine.
|
|
|
|
func (f *FlagSet) usage() {
|
|
|
|
if f == commandLine {
|
|
|
|
Usage()
|
2011-10-27 01:57:58 +02:00
|
|
|
} else if f.Usage == nil {
|
|
|
|
defaultUsage(f)
|
2011-09-16 17:47:21 +02:00
|
|
|
} else {
|
|
|
|
f.Usage()
|
|
|
|
}
|
|
|
|
}
|
2010-12-03 05:34:57 +01:00
|
|
|
|
2011-09-16 17:47:21 +02:00
|
|
|
// parseOne parses one flag. It returns whether a flag was seen.
|
2011-12-03 03:17:34 +01:00
|
|
|
func (f *FlagSet) parseOne() (bool, error) {
|
2011-01-21 19:19:03 +01:00
|
|
|
if len(f.args) == 0 {
|
2011-09-16 17:47:21 +02:00
|
|
|
return false, nil
|
2010-12-03 05:34:57 +01:00
|
|
|
}
|
2011-01-21 19:19:03 +01:00
|
|
|
s := f.args[0]
|
|
|
|
if len(s) == 0 || s[0] != '-' || len(s) == 1 {
|
2011-09-16 17:47:21 +02:00
|
|
|
return false, nil
|
2010-12-03 05:34:57 +01:00
|
|
|
}
|
|
|
|
num_minuses := 1
|
|
|
|
if s[1] == '-' {
|
|
|
|
num_minuses++
|
|
|
|
if len(s) == 2 { // "--" terminates the flags
|
2011-01-21 19:19:03 +01:00
|
|
|
f.args = f.args[1:]
|
2011-09-16 17:47:21 +02:00
|
|
|
return false, nil
|
2010-12-03 05:34:57 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
name := s[num_minuses:]
|
|
|
|
if len(name) == 0 || name[0] == '-' || name[0] == '=' {
|
2011-09-16 17:47:21 +02:00
|
|
|
return false, f.failf("bad flag syntax: %s", s)
|
2010-12-03 05:34:57 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// it's a flag. does it have an argument?
|
2011-01-21 19:19:03 +01:00
|
|
|
f.args = f.args[1:]
|
2010-12-03 05:34:57 +01:00
|
|
|
has_value := false
|
|
|
|
value := ""
|
|
|
|
for i := 1; i < len(name); i++ { // equals cannot be first
|
|
|
|
if name[i] == '=' {
|
|
|
|
value = name[i+1:]
|
|
|
|
has_value = true
|
|
|
|
name = name[0:i]
|
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
2011-09-16 17:47:21 +02:00
|
|
|
m := f.formal
|
2010-12-03 05:34:57 +01:00
|
|
|
flag, alreadythere := m[name] // BUG
|
|
|
|
if !alreadythere {
|
2011-09-16 17:47:21 +02:00
|
|
|
if name == "help" || name == "h" { // special case for nice help message.
|
|
|
|
f.usage()
|
|
|
|
return false, ErrHelp
|
|
|
|
}
|
|
|
|
return false, f.failf("flag provided but not defined: -%s", name)
|
2010-12-03 05:34:57 +01:00
|
|
|
}
|
2011-01-21 19:19:03 +01:00
|
|
|
if fv, ok := flag.Value.(*boolValue); ok { // special case: doesn't need an arg
|
2010-12-03 05:34:57 +01:00
|
|
|
if has_value {
|
2012-01-25 21:56:26 +01:00
|
|
|
if err := fv.Set(value); err != nil {
|
|
|
|
f.failf("invalid boolean value %q for -%s: %v", value, name, err)
|
2010-12-03 05:34:57 +01:00
|
|
|
}
|
|
|
|
} else {
|
2011-01-21 19:19:03 +01:00
|
|
|
fv.Set("true")
|
2010-12-03 05:34:57 +01:00
|
|
|
}
|
|
|
|
} else {
|
|
|
|
// It must have a value, which might be the next argument.
|
2011-01-21 19:19:03 +01:00
|
|
|
if !has_value && len(f.args) > 0 {
|
2010-12-03 05:34:57 +01:00
|
|
|
// value is the next arg
|
|
|
|
has_value = true
|
2011-01-21 19:19:03 +01:00
|
|
|
value, f.args = f.args[0], f.args[1:]
|
2010-12-03 05:34:57 +01:00
|
|
|
}
|
|
|
|
if !has_value {
|
2011-09-16 17:47:21 +02:00
|
|
|
return false, f.failf("flag needs an argument: -%s", name)
|
2010-12-03 05:34:57 +01:00
|
|
|
}
|
2012-01-25 21:56:26 +01:00
|
|
|
if err := flag.Value.Set(value); err != nil {
|
|
|
|
return false, f.failf("invalid value %q for flag -%s: %v", value, name, err)
|
2010-12-03 05:34:57 +01:00
|
|
|
}
|
|
|
|
}
|
2011-10-27 01:57:58 +02:00
|
|
|
if f.actual == nil {
|
|
|
|
f.actual = make(map[string]*Flag)
|
|
|
|
}
|
2011-09-16 17:47:21 +02:00
|
|
|
f.actual[name] = flag
|
|
|
|
return true, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// Parse parses flag definitions from the argument list, which should not
|
|
|
|
// include the command name. Must be called after all flags in the FlagSet
|
|
|
|
// are defined and before flags are accessed by the program.
|
|
|
|
// The return value will be ErrHelp if -help was set but not defined.
|
2011-12-03 03:17:34 +01:00
|
|
|
func (f *FlagSet) Parse(arguments []string) error {
|
2011-10-27 01:57:58 +02:00
|
|
|
f.parsed = true
|
2011-09-16 17:47:21 +02:00
|
|
|
f.args = arguments
|
|
|
|
for {
|
|
|
|
seen, err := f.parseOne()
|
|
|
|
if seen {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
if err == nil {
|
|
|
|
break
|
|
|
|
}
|
|
|
|
switch f.errorHandling {
|
|
|
|
case ContinueOnError:
|
|
|
|
return err
|
|
|
|
case ExitOnError:
|
|
|
|
os.Exit(2)
|
|
|
|
case PanicOnError:
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return nil
|
2010-12-03 05:34:57 +01:00
|
|
|
}
|
|
|
|
|
2011-10-27 01:57:58 +02:00
|
|
|
// Parsed reports whether f.Parse has been called.
|
|
|
|
func (f *FlagSet) Parsed() bool {
|
|
|
|
return f.parsed
|
|
|
|
}
|
|
|
|
|
2011-09-16 17:47:21 +02:00
|
|
|
// Parse parses the command-line flags from os.Args[1:]. Must be called
|
|
|
|
// after all flags are defined and before flags are accessed by the program.
|
2010-12-03 05:34:57 +01:00
|
|
|
func Parse() {
|
2011-09-16 17:47:21 +02:00
|
|
|
// Ignore errors; commandLine is set for ExitOnError.
|
|
|
|
commandLine.Parse(os.Args[1:])
|
2010-12-03 05:34:57 +01:00
|
|
|
}
|
|
|
|
|
2011-10-27 01:57:58 +02:00
|
|
|
// Parsed returns true if the command-line flags have been parsed.
|
|
|
|
func Parsed() bool {
|
|
|
|
return commandLine.Parsed()
|
|
|
|
}
|
|
|
|
|
2011-09-16 17:47:21 +02:00
|
|
|
// The default set of command-line flags, parsed from os.Args.
|
|
|
|
var commandLine = NewFlagSet(os.Args[0], ExitOnError)
|
|
|
|
|
|
|
|
// NewFlagSet returns a new, empty flag set with the specified name and
|
|
|
|
// error handling property.
|
|
|
|
func NewFlagSet(name string, errorHandling ErrorHandling) *FlagSet {
|
|
|
|
f := &FlagSet{
|
|
|
|
name: name,
|
|
|
|
errorHandling: errorHandling,
|
|
|
|
}
|
|
|
|
return f
|
2010-12-03 05:34:57 +01:00
|
|
|
}
|
2011-10-27 01:57:58 +02:00
|
|
|
|
|
|
|
// Init sets the name and error handling property for a flag set.
|
|
|
|
// By default, the zero FlagSet uses an empty name and the
|
|
|
|
// ContinueOnError error handling policy.
|
|
|
|
func (f *FlagSet) Init(name string, errorHandling ErrorHandling) {
|
|
|
|
f.name = name
|
|
|
|
f.errorHandling = errorHandling
|
|
|
|
}
|