Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
62 changes: 12 additions & 50 deletions bool.go
Original file line number Diff line number Diff line change
@@ -1,74 +1,36 @@
// Copyright 2013 Google Inc. All rights reserved.
// Copyright 2017 Google Inc. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.

package getopt

import (
"fmt"
"strings"
)

type boolValue bool

func (b *boolValue) Set(value string, opt Option) error {
switch strings.ToLower(value) {
case "", "1", "true", "on", "t":
*b = true
case "0", "false", "off", "f":
*b = false
default:
return fmt.Errorf("invalid value for bool %s: %q", opt.Name(), value)
}
return nil
}

func (b *boolValue) String() string {
if *b {
return "true"
}
return "false"
}

// Bool creates a flag option that is a bool. Bools normally do not take a
// value however one can be assigned by using the long form of the option:
//
// --option=true
// --o=false
//
// Its value is case insenstive and one of true, false, t, f, on, off, t and 0.
// The value is case insensitive and one of true, false, t, f, on, off, t and 0.
func Bool(name rune, helpvalue ...string) *bool {
return CommandLine.Bool(name, helpvalue...)
var b bool
CommandLine.Flag(&b, name, helpvalue...)
return &b
}

func (s *Set) Bool(name rune, helpvalue ...string) *bool {
func BoolLong(name string, short rune, helpvalue ...string) *bool {
var p bool
s.BoolVarLong(&p, "", name, helpvalue...)
CommandLine.FlagLong(&p, name, short, helpvalue...)
return &p
}

func BoolLong(name string, short rune, helpvalue ...string) *bool {
return CommandLine.BoolLong(name, short, helpvalue...)
func (s *Set) Bool(name rune, helpvalue ...string) *bool {
var b bool
s.Flag(&b, name, helpvalue...)
return &b
}

func (s *Set) BoolLong(name string, short rune, helpvalue ...string) *bool {
var p bool
s.BoolVarLong(&p, name, short, helpvalue...)
s.FlagLong(&p, name, short, helpvalue...)
return &p
}

func BoolVar(p *bool, name rune, helpvalue ...string) Option {
return CommandLine.BoolVar(p, name, helpvalue...)
}

func (s *Set) BoolVar(p *bool, name rune, helpvalue ...string) Option {
return s.BoolVarLong(p, "", name, helpvalue...)
}

func BoolVarLong(p *bool, name string, short rune, helpvalue ...string) Option {
return CommandLine.BoolVarLong(p, name, short, helpvalue...)
}

func (s *Set) BoolVarLong(p *bool, name string, short rune, helpvalue ...string) Option {
return s.VarLong((*boolValue)(p), name, short, helpvalue...).SetFlag()
}
2 changes: 1 addition & 1 deletion bool_test.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// Copyright 2013 Google Inc. All rights reserved.
// Copyright 2017 Google Inc. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.

Expand Down
2 changes: 1 addition & 1 deletion breakup_test.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// Copyright 2013 Google Inc. All rights reserved.
// Copyright 2017 Google Inc. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.

Expand Down
30 changes: 9 additions & 21 deletions counter.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// Copyright 2013 Google Inc. All rights reserved.
// Copyright 2017 Google Inc. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.

Expand Down Expand Up @@ -45,37 +45,25 @@ func (b *counterValue) String() string {
//
// Further instances of the option will increment from the set value.
func Counter(name rune, helpvalue ...string) *int {
return CommandLine.Counter(name, helpvalue...)
var p int
CommandLine.FlagLong((*counterValue)(&p), "", name, helpvalue...).SetFlag()
return &p
}

func (s *Set) Counter(name rune, helpvalue ...string) *int {
var p int
s.CounterVarLong(&p, "", name, helpvalue...)
s.FlagLong((*counterValue)(&p), "", name, helpvalue...).SetFlag()
return &p
}

func CounterLong(name string, short rune, helpvalue ...string) *int {
return CommandLine.CounterLong(name, short, helpvalue...)
var p int
CommandLine.FlagLong((*counterValue)(&p), name, short, helpvalue...).SetFlag()
return &p
}

func (s *Set) CounterLong(name string, short rune, helpvalue ...string) *int {
var p int
s.CounterVarLong(&p, name, short, helpvalue...)
s.FlagLong((*counterValue)(&p), name, short, helpvalue...).SetFlag()
return &p
}

func CounterVar(p *int, name rune, helpvalue ...string) Option {
return CommandLine.CounterVar(p, name, helpvalue...)
}

func (s *Set) CounterVar(p *int, name rune, helpvalue ...string) Option {
return s.CounterVarLong(p, "", name, helpvalue...)
}

func CounterVarLong(p *int, name string, short rune, helpvalue ...string) Option {
return CommandLine.CounterVarLong(p, name, short, helpvalue...)
}

func (s *Set) CounterVarLong(p *int, name string, short rune, helpvalue ...string) Option {
return s.VarLong((*counterValue)(p), name, short, helpvalue...).SetFlag()
}
17 changes: 1 addition & 16 deletions counter_test.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// Copyright 2013 Google Inc. All rights reserved.
// Copyright 2017 Google Inc. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.

Expand Down Expand Up @@ -73,19 +73,4 @@ func TestCounter(t *testing.T) {
t.Errorf("%s: got %v, want %v", tt.where, got, want)
}
}

reset()
c := 5
opt := CounterVar(&c, 'c')
parse([]string{"test", "-c"})
if c != 6 {
t.Errorf("got %d, want 6", c)
}
if opt.Count() != 1 {
t.Errorf("got %d, want 1", c)
}
Reset()
if c != 5 {
t.Errorf("got %d, want 5", c)
}
}
44 changes: 8 additions & 36 deletions duration.go
Original file line number Diff line number Diff line change
@@ -1,56 +1,28 @@
// Copyright 2015 Google Inc. All rights reserved.
// Copyright 2017 Google Inc. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.

package getopt

import "time"

type durationValue time.Duration

func (d *durationValue) Set(value string, opt Option) error {
v, err := time.ParseDuration(value)
if err != nil {
return err
}
*d = durationValue(v)
return nil
}

func (d *durationValue) String() string {
return time.Duration(*d).String()
}

// Duration creates an option that parses its value as a time.Duration.
func Duration(name rune, value time.Duration, helpvalue ...string) *time.Duration {
return CommandLine.Duration(name, value, helpvalue...)
CommandLine.FlagLong(&value, "", name, helpvalue...)
return &value
}

func (s *Set) Duration(name rune, value time.Duration, helpvalue ...string) *time.Duration {
return s.DurationLong("", name, value, helpvalue...)
s.FlagLong(&value, "", name, helpvalue...)
return &value
}

func DurationLong(name string, short rune, value time.Duration, helpvalue ...string) *time.Duration {
return CommandLine.DurationLong(name, short, value, helpvalue...)
CommandLine.FlagLong(&value, name, short, helpvalue...)
return &value
}

func (s *Set) DurationLong(name string, short rune, value time.Duration, helpvalue ...string) *time.Duration {
s.DurationVarLong(&value, name, short, helpvalue...)
s.FlagLong(&value, name, short, helpvalue...)
return &value
}

func DurationVar(p *time.Duration, name rune, helpvalue ...string) Option {
return CommandLine.DurationVar(p, name, helpvalue...)
}

func (s *Set) DurationVar(p *time.Duration, name rune, helpvalue ...string) Option {
return s.DurationVarLong(p, "", name, helpvalue...)
}

func DurationVarLong(p *time.Duration, name string, short rune, helpvalue ...string) Option {
return CommandLine.DurationVarLong(p, name, short, helpvalue...)
}

func (s *Set) DurationVarLong(p *time.Duration, name string, short rune, helpvalue ...string) Option {
return s.VarLong((*durationValue)(p), name, short, helpvalue...)
}
2 changes: 1 addition & 1 deletion duration_test.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// Copyright 2013 Google Inc. All rights reserved.
// Copyright 2017 Google Inc. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.

Expand Down
72 changes: 39 additions & 33 deletions enum.go
Original file line number Diff line number Diff line change
@@ -1,17 +1,26 @@
// Copyright 2013 Google Inc. All rights reserved.
// Copyright 2017 Google Inc. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.

package getopt

import "errors"
import (
"errors"
"fmt"
"sync"
)

type enumValue string

var enumValues = make(map[*enumValue]map[string]struct{})
var (
enumValuesMu sync.Mutex
enumValues = make(map[*enumValue]map[string]struct{})
)

func (s *enumValue) Set(value string, opt Option) error {
enumValuesMu.Lock()
es, ok := enumValues[s]
enumValuesMu.Unlock()
if !ok || es == nil {
return errors.New("this option has no values")
}
Expand All @@ -28,46 +37,43 @@ func (s *enumValue) String() string {

// Enum creates an option that can only be set to one of the enumerated strings
// passed in values. Passing nil or an empty slice results in an option that
// will always fail.
func Enum(name rune, values []string, helpvalue ...string) *string {
return CommandLine.Enum(name, values, helpvalue...)
// will always fail. If not "", value is the default value of the enum. If
// value is not listed in values then Enum will produce an error on standard
// error and then exit the program with a status of 1.
func Enum(name rune, values []string, value string, helpvalue ...string) *string {
return CommandLine.Enum(name, values, value, helpvalue...)
}

func (s *Set) Enum(name rune, values []string, helpvalue ...string) *string {
var p string
s.EnumVarLong(&p, "", name, values, helpvalue...)
return &p
func (s *Set) Enum(name rune, values []string, value string, helpvalue ...string) *string {
var p enumValue
p.define(values, value, &option{short: name})
s.FlagLong(&p, "", name, helpvalue...)
return (*string)(&p)
}

func EnumLong(name string, short rune, values []string, helpvalue ...string) *string {
return CommandLine.EnumLong(name, short, values, helpvalue...)
func EnumLong(name string, short rune, values []string, value string, helpvalue ...string) *string {
return CommandLine.EnumLong(name, short, values, value, helpvalue...)
}

func (s *Set) EnumLong(name string, short rune, values []string, helpvalue ...string) *string {
var p string
s.EnumVarLong(&p, name, short, values, helpvalue...)
return &p
func (s *Set) EnumLong(name string, short rune, values []string, value string, helpvalue ...string) *string {
var p enumValue
p.define(values, value, &option{short: short, long: name})
s.FlagLong(&p, name, short, helpvalue...)
return (*string)(&p)
}

// EnumVar creates an enum option that defaults to the starting value of *p.
// If *p is not found in values then a reset of this option will fail.
func EnumVar(p *string, name rune, values []string, helpvalue ...string) Option {
return CommandLine.EnumVar(p, name, values, helpvalue...)
}

func (s *Set) EnumVar(p *string, name rune, values []string, helpvalue ...string) Option {
return s.EnumVarLong(p, "", name, values, helpvalue...)
}

func EnumVarLong(p *string, name string, short rune, values []string, helpvalue ...string) Option {
return CommandLine.EnumVarLong(p, name, short, values, helpvalue...)
}

func (s *Set) EnumVarLong(p *string, name string, short rune, values []string, helpvalue ...string) Option {
func (e *enumValue) define(values []string, def string, opt Option) {
m := make(map[string]struct{})
for _, v := range values {
m[v] = struct{}{}
}
enumValues[(*enumValue)(p)] = m
return s.VarLong((*enumValue)(p), name, short, helpvalue...)
enumValuesMu.Lock()
enumValues[e] = m
enumValuesMu.Unlock()
if def != "" {
if err := e.Set(def, nil); err != nil {
fmt.Fprintf(stderr, "setting default for %s: %v\n", opt.Name(), err)
exit(1)
}
}
}
Loading