mirror of
https://github.com/golang/go
synced 2024-11-22 03:34:40 -07:00
flag: add Parsed, restore Usage
R=r CC=golang-dev https://golang.org/cl/4973050
This commit is contained in:
parent
3f1269ff1e
commit
2cc4a54dec
@ -204,6 +204,7 @@ type FlagSet struct {
|
|||||||
Usage func()
|
Usage func()
|
||||||
|
|
||||||
name string
|
name string
|
||||||
|
parsed bool
|
||||||
actual map[string]*Flag
|
actual map[string]*Flag
|
||||||
formal map[string]*Flag
|
formal map[string]*Flag
|
||||||
args []string // arguments after flags
|
args []string // arguments after flags
|
||||||
@ -318,10 +319,15 @@ func defaultUsage(f *FlagSet) {
|
|||||||
f.PrintDefaults()
|
f.PrintDefaults()
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// 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.
|
||||||
|
|
||||||
// Usage prints to standard error a usage message documenting all defined command-line flags.
|
// Usage prints to standard error a usage message documenting all defined command-line flags.
|
||||||
// The function is a variable that may be changed to point to a custom function.
|
// The function is a variable that may be changed to point to a custom function.
|
||||||
var Usage = func() {
|
var Usage = func() {
|
||||||
defaultUsage(commandLine)
|
fmt.Fprintf(os.Stderr, "Usage of %s:\n", os.Args[0])
|
||||||
|
PrintDefaults()
|
||||||
}
|
}
|
||||||
|
|
||||||
// NFlag returns the number of flags that have been set.
|
// NFlag returns the number of flags that have been set.
|
||||||
@ -660,6 +666,7 @@ func (f *FlagSet) parseOne() (bool, os.Error) {
|
|||||||
// are defined and before flags are accessed by the program.
|
// are defined and before flags are accessed by the program.
|
||||||
// The return value will be ErrHelp if -help was set but not defined.
|
// The return value will be ErrHelp if -help was set but not defined.
|
||||||
func (f *FlagSet) Parse(arguments []string) os.Error {
|
func (f *FlagSet) Parse(arguments []string) os.Error {
|
||||||
|
f.parsed = true
|
||||||
f.args = arguments
|
f.args = arguments
|
||||||
for {
|
for {
|
||||||
seen, err := f.parseOne()
|
seen, err := f.parseOne()
|
||||||
@ -681,6 +688,11 @@ func (f *FlagSet) Parse(arguments []string) os.Error {
|
|||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Parsed reports whether f.Parse has been called.
|
||||||
|
func (f *FlagSet) Parsed() bool {
|
||||||
|
return f.parsed
|
||||||
|
}
|
||||||
|
|
||||||
// Parse parses the command-line flags from os.Args[1:]. Must be called
|
// 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.
|
// after all flags are defined and before flags are accessed by the program.
|
||||||
func Parse() {
|
func Parse() {
|
||||||
@ -688,6 +700,11 @@ func Parse() {
|
|||||||
commandLine.Parse(os.Args[1:])
|
commandLine.Parse(os.Args[1:])
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Parsed returns true if the command-line flags have been parsed.
|
||||||
|
func Parsed() bool {
|
||||||
|
return commandLine.Parsed()
|
||||||
|
}
|
||||||
|
|
||||||
// The default set of command-line flags, parsed from os.Args.
|
// The default set of command-line flags, parsed from os.Args.
|
||||||
var commandLine = NewFlagSet(os.Args[0], ExitOnError)
|
var commandLine = NewFlagSet(os.Args[0], ExitOnError)
|
||||||
|
|
||||||
|
@ -98,6 +98,9 @@ func TestUsage(t *testing.T) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func testParse(f *FlagSet, t *testing.T) {
|
func testParse(f *FlagSet, t *testing.T) {
|
||||||
|
if f.Parsed() {
|
||||||
|
t.Error("f.Parse() = true before Parse")
|
||||||
|
}
|
||||||
boolFlag := f.Bool("bool", false, "bool value")
|
boolFlag := f.Bool("bool", false, "bool value")
|
||||||
bool2Flag := f.Bool("bool2", false, "bool2 value")
|
bool2Flag := f.Bool("bool2", false, "bool2 value")
|
||||||
intFlag := f.Int("int", 0, "int value")
|
intFlag := f.Int("int", 0, "int value")
|
||||||
@ -121,6 +124,9 @@ func testParse(f *FlagSet, t *testing.T) {
|
|||||||
if err := f.Parse(args); err != nil {
|
if err := f.Parse(args); err != nil {
|
||||||
t.Fatal(err)
|
t.Fatal(err)
|
||||||
}
|
}
|
||||||
|
if !f.Parsed() {
|
||||||
|
t.Error("f.Parse() = false after Parse")
|
||||||
|
}
|
||||||
if *boolFlag != true {
|
if *boolFlag != true {
|
||||||
t.Error("bool flag should be true, is ", *boolFlag)
|
t.Error("bool flag should be true, is ", *boolFlag)
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user