summaryrefslogtreecommitdiff
path: root/src/pkg/flag
diff options
context:
space:
mode:
authorRobert Griesemer <gri@golang.org>2009-12-15 15:27:16 -0800
committerRobert Griesemer <gri@golang.org>2009-12-15 15:27:16 -0800
commit881d6064d23d9da5c7ff368bc7d41d271290deff (patch)
tree44d5d948e3f27cc7eff15ec8cd7ee5165d9a7e90 /src/pkg/flag
parentd9dfea3ebd51cea89fef8afc6b2377c2958b24f1 (diff)
downloadgolang-881d6064d23d9da5c7ff368bc7d41d271290deff.tar.gz
1) Change default gofmt default settings for
parsing and printing to new syntax. Use -oldparser to parse the old syntax, use -oldprinter to print the old syntax. 2) Change default gofmt formatting settings to use tabs for indentation only and to use spaces for alignment. This will make the code alignment insensitive to an editor's tabwidth. Use -spaces=false to use tabs for alignment. 3) Manually changed src/exp/parser/parser_test.go so that it doesn't try to parse the parser's source files using the old syntax (they have new syntax now). 4) gofmt -w src misc test/bench 2nd set of files. R=rsc CC=golang-dev http://codereview.appspot.com/179067
Diffstat (limited to 'src/pkg/flag')
-rw-r--r--src/pkg/flag/flag.go312
-rw-r--r--src/pkg/flag/flag_test.go64
2 files changed, 188 insertions, 188 deletions
diff --git a/src/pkg/flag/flag.go b/src/pkg/flag/flag.go
index 86a3b3dc1..d57a59c03 100644
--- a/src/pkg/flag/flag.go
+++ b/src/pkg/flag/flag.go
@@ -48,9 +48,9 @@
package flag
import (
- "fmt";
- "os";
- "strconv";
+ "fmt"
+ "os"
+ "strconv"
)
// TODO(r): BUG: atob belongs elsewhere
@@ -61,171 +61,171 @@ func atob(str string) (value bool, ok bool) {
case "0", "f", "F", "false", "FALSE", "False":
return false, true
}
- return false, false;
+ return false, false
}
// -- Bool Value
type boolValue struct {
- p *bool;
+ p *bool
}
func newBoolValue(val bool, p *bool) *boolValue {
- *p = val;
- return &boolValue{p};
+ *p = val
+ return &boolValue{p}
}
func (b *boolValue) set(s string) bool {
- v, ok := atob(s);
- *b.p = v;
- return ok;
+ v, ok := atob(s)
+ *b.p = v
+ return ok
}
-func (b *boolValue) String() string { return fmt.Sprintf("%v", *b.p) }
+func (b *boolValue) String() string { return fmt.Sprintf("%v", *b.p) }
// -- Int Value
type intValue struct {
- p *int;
+ p *int
}
func newIntValue(val int, p *int) *intValue {
- *p = val;
- return &intValue{p};
+ *p = val
+ return &intValue{p}
}
func (i *intValue) set(s string) bool {
- v, err := strconv.Atoi(s);
- *i.p = int(v);
- return err == nil;
+ v, err := strconv.Atoi(s)
+ *i.p = int(v)
+ return err == nil
}
-func (i *intValue) String() string { return fmt.Sprintf("%v", *i.p) }
+func (i *intValue) String() string { return fmt.Sprintf("%v", *i.p) }
// -- Int64 Value
type int64Value struct {
- p *int64;
+ p *int64
}
func newInt64Value(val int64, p *int64) *int64Value {
- *p = val;
- return &int64Value{p};
+ *p = val
+ return &int64Value{p}
}
func (i *int64Value) set(s string) bool {
- v, err := strconv.Atoi64(s);
- *i.p = v;
- return err == nil;
+ v, err := strconv.Atoi64(s)
+ *i.p = v
+ return err == nil
}
-func (i *int64Value) String() string { return fmt.Sprintf("%v", *i.p) }
+func (i *int64Value) String() string { return fmt.Sprintf("%v", *i.p) }
// -- Uint Value
type uintValue struct {
- p *uint;
+ p *uint
}
func newUintValue(val uint, p *uint) *uintValue {
- *p = val;
- return &uintValue{p};
+ *p = val
+ return &uintValue{p}
}
func (i *uintValue) set(s string) bool {
- v, err := strconv.Atoui(s);
- *i.p = uint(v);
- return err == nil;
+ v, err := strconv.Atoui(s)
+ *i.p = uint(v)
+ return err == nil
}
-func (i *uintValue) String() string { return fmt.Sprintf("%v", *i.p) }
+func (i *uintValue) String() string { return fmt.Sprintf("%v", *i.p) }
// -- uint64 Value
type uint64Value struct {
- p *uint64;
+ p *uint64
}
func newUint64Value(val uint64, p *uint64) *uint64Value {
- *p = val;
- return &uint64Value{p};
+ *p = val
+ return &uint64Value{p}
}
func (i *uint64Value) set(s string) bool {
- v, err := strconv.Atoui64(s);
- *i.p = uint64(v);
- return err == nil;
+ v, err := strconv.Atoui64(s)
+ *i.p = uint64(v)
+ return err == nil
}
-func (i *uint64Value) String() string { return fmt.Sprintf("%v", *i.p) }
+func (i *uint64Value) String() string { return fmt.Sprintf("%v", *i.p) }
// -- string Value
type stringValue struct {
- p *string;
+ p *string
}
func newStringValue(val string, p *string) *stringValue {
- *p = val;
- return &stringValue{p};
+ *p = val
+ return &stringValue{p}
}
func (s *stringValue) set(val string) bool {
- *s.p = val;
- return true;
+ *s.p = val
+ return true
}
-func (s *stringValue) String() string { return fmt.Sprintf("%s", *s.p) }
+func (s *stringValue) String() string { return fmt.Sprintf("%s", *s.p) }
// -- Float Value
type floatValue struct {
- p *float;
+ p *float
}
func newFloatValue(val float, p *float) *floatValue {
- *p = val;
- return &floatValue{p};
+ *p = val
+ return &floatValue{p}
}
func (f *floatValue) set(s string) bool {
- v, err := strconv.Atof(s);
- *f.p = v;
- return err == nil;
+ v, err := strconv.Atof(s)
+ *f.p = v
+ return err == nil
}
-func (f *floatValue) String() string { return fmt.Sprintf("%v", *f.p) }
+func (f *floatValue) String() string { return fmt.Sprintf("%v", *f.p) }
// -- Float64 Value
type float64Value struct {
- p *float64;
+ p *float64
}
func newFloat64Value(val float64, p *float64) *float64Value {
- *p = val;
- return &float64Value{p};
+ *p = val
+ return &float64Value{p}
}
func (f *float64Value) set(s string) bool {
- v, err := strconv.Atof64(s);
- *f.p = v;
- return err == nil;
+ v, err := strconv.Atof64(s)
+ *f.p = v
+ return err == nil
}
-func (f *float64Value) String() string { return fmt.Sprintf("%v", *f.p) }
+func (f *float64Value) String() string { return fmt.Sprintf("%v", *f.p) }
// FlagValue is the interface to the dynamic value stored in a flag.
// (The default value is represented as a string.)
type FlagValue interface {
- String() string;
- set(string) bool;
+ String() string
+ set(string) bool
}
// 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 FlagValue; // value as set
- DefValue string; // default value (as text); for usage message
+ Name string // name as it appears on command line
+ Usage string // help message
+ Value FlagValue // value as set
+ DefValue string // default value (as text); for usage message
}
type allFlags struct {
- actual map[string]*Flag;
- formal map[string]*Flag;
- first_arg int; // 0 is the program name, 1 is first arg
+ actual map[string]*Flag
+ formal map[string]*Flag
+ first_arg int // 0 is the program name, 1 is first arg
}
var flags *allFlags = &allFlags{make(map[string]*Flag), make(map[string]*Flag), 1}
@@ -246,74 +246,74 @@ func Visit(fn func(*Flag)) {
// Lookup returns the Flag structure of the named flag, returning nil if none exists.
func Lookup(name string) *Flag {
- f, ok := flags.formal[name];
+ f, ok := flags.formal[name]
if !ok {
return nil
}
- return f;
+ return f
}
// Set sets the value of the named flag. It returns true if the set succeeded; false if
// there is no such flag defined.
func Set(name, value string) bool {
- f, ok := flags.formal[name];
+ f, ok := flags.formal[name]
if !ok {
return false
}
- ok = f.Value.set(value);
+ ok = f.Value.set(value)
if !ok {
return false
}
- flags.actual[name] = f;
- return true;
+ flags.actual[name] = f
+ return true
}
// PrintDefaults prints to standard error the default values of all defined flags.
func PrintDefaults() {
VisitAll(func(f *Flag) {
- format := " -%s=%s: %s\n";
+ format := " -%s=%s: %s\n"
if _, ok := f.Value.(*stringValue); ok {
// put quotes on the value
format = " -%s=%q: %s\n"
}
- fmt.Fprintf(os.Stderr, format, f.Name, f.DefValue, f.Usage);
+ fmt.Fprintf(os.Stderr, format, f.Name, f.DefValue, f.Usage)
})
}
// Usage prints to standard error a default usage message documenting all defined flags.
// The function is a variable that may be changed to point to a custom function.
var Usage = func() {
- fmt.Fprintf(os.Stderr, "Usage of %s:\n", os.Args[0]);
- PrintDefaults();
+ fmt.Fprintf(os.Stderr, "Usage of %s:\n", os.Args[0])
+ PrintDefaults()
}
-func NFlag() int { return len(flags.actual) }
+func NFlag() int { return len(flags.actual) }
// 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 {
- i += flags.first_arg;
+ i += flags.first_arg
if i < 0 || i >= len(os.Args) {
return ""
}
- return os.Args[i];
+ return os.Args[i]
}
// NArg is the number of arguments remaining after flags have been processed.
-func NArg() int { return len(os.Args) - flags.first_arg }
+func NArg() int { return len(os.Args) - flags.first_arg }
// Args returns the non-flag command-line arguments.
-func Args() []string { return os.Args[flags.first_arg:] }
+func Args() []string { return os.Args[flags.first_arg:] }
func add(name string, value FlagValue, usage string) {
// Remember the default value as a string; it won't change.
- f := &Flag{name, usage, value, value.String()};
- _, alreadythere := flags.formal[name];
+ f := &Flag{name, usage, value, value.String()}
+ _, alreadythere := flags.formal[name]
if alreadythere {
- fmt.Fprintln(os.Stderr, "flag redefined:", name);
- panic("flag redefinition"); // Happens only if flags are declared with identical names
+ fmt.Fprintln(os.Stderr, "flag redefined:", name)
+ panic("flag redefinition") // Happens only if flags are declared with identical names
}
- flags.formal[name] = f;
+ flags.formal[name] = f
}
// BoolVar defines a bool flag with specified name, default value, and usage string.
@@ -325,9 +325,9 @@ func BoolVar(p *bool, name string, value bool, usage string) {
// 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 {
- p := new(bool);
- BoolVar(p, name, value, usage);
- return p;
+ p := new(bool)
+ BoolVar(p, name, value, usage)
+ return p
}
// IntVar defines an int flag with specified name, default value, and usage string.
@@ -339,9 +339,9 @@ func IntVar(p *int, name string, value int, usage string) {
// 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 {
- p := new(int);
- IntVar(p, name, value, usage);
- return p;
+ p := new(int)
+ IntVar(p, name, value, usage)
+ return p
}
// Int64Var defines an int64 flag with specified name, default value, and usage string.
@@ -353,9 +353,9 @@ func Int64Var(p *int64, name string, value int64, usage string) {
// 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 {
- p := new(int64);
- Int64Var(p, name, value, usage);
- return p;
+ p := new(int64)
+ Int64Var(p, name, value, usage)
+ return p
}
// UintVar defines a uint flag with specified name, default value, and usage string.
@@ -367,9 +367,9 @@ func UintVar(p *uint, name string, value uint, usage string) {
// 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 {
- p := new(uint);
- UintVar(p, name, value, usage);
- return p;
+ p := new(uint)
+ UintVar(p, name, value, usage)
+ return p
}
// Uint64Var defines a uint64 flag with specified name, default value, and usage string.
@@ -381,9 +381,9 @@ func Uint64Var(p *uint64, name string, value uint64, usage string) {
// 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 {
- p := new(uint64);
- Uint64Var(p, name, value, usage);
- return p;
+ p := new(uint64)
+ Uint64Var(p, name, value, usage)
+ return p
}
// StringVar defines a string flag with specified name, default value, and usage string.
@@ -395,9 +395,9 @@ func StringVar(p *string, name, value string, usage string) {
// 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, value string, usage string) *string {
- p := new(string);
- StringVar(p, name, value, usage);
- return p;
+ p := new(string)
+ StringVar(p, name, value, usage)
+ return p
}
// FloatVar defines a float flag with specified name, default value, and usage string.
@@ -409,9 +409,9 @@ func FloatVar(p *float, name string, value float, usage string) {
// Float defines a float flag with specified name, default value, and usage string.
// The return value is the address of a float variable that stores the value of the flag.
func Float(name string, value float, usage string) *float {
- p := new(float);
- FloatVar(p, name, value, usage);
- return p;
+ p := new(float)
+ FloatVar(p, name, value, usage)
+ return p
}
// Float64Var defines a float64 flag with specified name, default value, and usage string.
@@ -423,68 +423,68 @@ func Float64Var(p *float64, name string, value float64, usage string) {
// 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.
func Float64(name string, value float64, usage string) *float64 {
- p := new(float64);
- Float64Var(p, name, value, usage);
- return p;
+ p := new(float64)
+ Float64Var(p, name, value, usage)
+ return p
}
func (f *allFlags) parseOne(index int) (ok bool, next int) {
- s := os.Args[index];
- f.first_arg = index; // until proven otherwise
+ s := os.Args[index]
+ f.first_arg = index // until proven otherwise
if len(s) == 0 {
return false, -1
}
if s[0] != '-' {
return false, -1
}
- num_minuses := 1;
+ num_minuses := 1
if len(s) == 1 {
return false, index
}
if s[1] == '-' {
- num_minuses++;
- if len(s) == 2 { // "--" terminates the flags
+ num_minuses++
+ if len(s) == 2 { // "--" terminates the flags
return false, index + 1
}
}
- name := s[num_minuses:];
+ name := s[num_minuses:]
if len(name) == 0 || name[0] == '-' || name[0] == '=' {
- fmt.Fprintln(os.Stderr, "bad flag syntax:", s);
- Usage();
- os.Exit(2);
+ fmt.Fprintln(os.Stderr, "bad flag syntax:", s)
+ Usage()
+ os.Exit(2)
}
// it's a flag. does it have an argument?
- has_value := false;
- value := "";
- for i := 1; i < len(name); i++ { // equals cannot be first
+ 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;
+ value = name[i+1:]
+ has_value = true
+ name = name[0:i]
+ break
}
}
- flag, alreadythere := flags.actual[name];
+ flag, alreadythere := flags.actual[name]
if alreadythere {
- fmt.Fprintf(os.Stderr, "flag specified twice: -%s\n", name);
- Usage();
- os.Exit(2);
+ fmt.Fprintf(os.Stderr, "flag specified twice: -%s\n", name)
+ Usage()
+ os.Exit(2)
}
- m := flags.formal;
- flag, alreadythere = m[name]; // BUG
+ m := flags.formal
+ flag, alreadythere = m[name] // BUG
if !alreadythere {
- fmt.Fprintf(os.Stderr, "flag provided but not defined: -%s\n", name);
- Usage();
- os.Exit(2);
+ fmt.Fprintf(os.Stderr, "flag provided but not defined: -%s\n", name)
+ Usage()
+ os.Exit(2)
}
- if f, ok := flag.Value.(*boolValue); ok { // special case: doesn't need an arg
+ if f, ok := flag.Value.(*boolValue); ok { // special case: doesn't need an arg
if has_value {
if !f.set(value) {
- fmt.Fprintf(os.Stderr, "invalid boolean value %t for flag: -%s\n", value, name);
- Usage();
- os.Exit(2);
+ fmt.Fprintf(os.Stderr, "invalid boolean value %t for flag: -%s\n", value, name)
+ Usage()
+ os.Exit(2)
}
} else {
f.set("true")
@@ -493,34 +493,34 @@ func (f *allFlags) parseOne(index int) (ok bool, next int) {
// It must have a value, which might be the next argument.
if !has_value && index < len(os.Args)-1 {
// value is the next arg
- has_value = true;
- index++;
- value = os.Args[index];
+ has_value = true
+ index++
+ value = os.Args[index]
}
if !has_value {
- fmt.Fprintf(os.Stderr, "flag needs an argument: -%s\n", name);
- Usage();
- os.Exit(2);
+ fmt.Fprintf(os.Stderr, "flag needs an argument: -%s\n", name)
+ Usage()
+ os.Exit(2)
}
- ok = flag.Value.set(value);
+ ok = flag.Value.set(value)
if !ok {
- fmt.Fprintf(os.Stderr, "invalid value %s for flag: -%s\n", value, name);
- Usage();
- os.Exit(2);
+ fmt.Fprintf(os.Stderr, "invalid value %s for flag: -%s\n", value, name)
+ Usage()
+ os.Exit(2)
}
}
- flags.actual[name] = flag;
- return true, index + 1;
+ flags.actual[name] = flag
+ return true, index + 1
}
// Parse parses the command-line flags. Must be called after all flags are defined
// and before any are accessed by the program.
func Parse() {
for i := 1; i < len(os.Args); {
- ok, next := flags.parseOne(i);
+ ok, next := flags.parseOne(i)
if next > 0 {
- flags.first_arg = next;
- i = next;
+ flags.first_arg = next
+ i = next
}
if !ok {
break
diff --git a/src/pkg/flag/flag_test.go b/src/pkg/flag/flag_test.go
index 0acfc2f96..d6e642b2b 100644
--- a/src/pkg/flag/flag_test.go
+++ b/src/pkg/flag/flag_test.go
@@ -5,35 +5,35 @@
package flag_test
import (
- . "flag";
- "testing";
+ . "flag"
+ "testing"
)
var (
- test_bool = Bool("test_bool", false, "bool value");
- test_int = Int("test_int", 0, "int value");
- test_int64 = Int64("test_int64", 0, "int64 value");
- test_uint = Uint("test_uint", 0, "uint value");
- test_uint64 = Uint64("test_uint64", 0, "uint64 value");
- test_string = String("test_string", "0", "string value");
- test_float = Float("test_float", 0, "float value");
- test_float64 = Float("test_float64", 0, "float64 value");
+ test_bool = Bool("test_bool", false, "bool value")
+ test_int = Int("test_int", 0, "int value")
+ test_int64 = Int64("test_int64", 0, "int64 value")
+ test_uint = Uint("test_uint", 0, "uint value")
+ test_uint64 = Uint64("test_uint64", 0, "uint64 value")
+ test_string = String("test_string", "0", "string value")
+ test_float = Float("test_float", 0, "float value")
+ test_float64 = Float("test_float64", 0, "float64 value")
)
func boolString(s string) string {
if s == "0" {
return "false"
}
- return "true";
+ return "true"
}
func TestEverything(t *testing.T) {
- m := make(map[string]*Flag);
- desired := "0";
+ m := make(map[string]*Flag)
+ desired := "0"
visitor := func(f *Flag) {
if len(f.Name) > 5 && f.Name[0:5] == "test_" {
- m[f.Name] = f;
- ok := false;
+ m[f.Name] = f
+ ok := false
switch {
case f.Value.String() == desired:
ok = true
@@ -44,35 +44,35 @@ func TestEverything(t *testing.T) {
t.Error("Visit: bad value", f.Value.String(), "for", f.Name)
}
}
- };
- VisitAll(visitor);
+ }
+ VisitAll(visitor)
if len(m) != 8 {
- t.Error("VisitAll misses some flags");
+ t.Error("VisitAll misses some flags")
for k, v := range m {
t.Log(k, *v)
}
}
- m = make(map[string]*Flag);
- Visit(visitor);
+ m = make(map[string]*Flag)
+ Visit(visitor)
if len(m) != 0 {
- t.Errorf("Visit sees unset flags");
+ t.Errorf("Visit sees unset flags")
for k, v := range m {
t.Log(k, *v)
}
}
// Now set all flags
- Set("test_bool", "true");
- Set("test_int", "1");
- Set("test_int64", "1");
- Set("test_uint", "1");
- Set("test_uint64", "1");
- Set("test_string", "1");
- Set("test_float", "1");
- Set("test_float64", "1");
- desired = "1";
- Visit(visitor);
+ Set("test_bool", "true")
+ Set("test_int", "1")
+ Set("test_int64", "1")
+ Set("test_uint", "1")
+ Set("test_uint64", "1")
+ Set("test_string", "1")
+ Set("test_float", "1")
+ Set("test_float64", "1")
+ desired = "1"
+ Visit(visitor)
if len(m) != 8 {
- t.Error("Visit fails after set");
+ t.Error("Visit fails after set")
for k, v := range m {
t.Log(k, *v)
}