| 1 | // Copyright 2013 Google Inc. All rights reserved. |
| 2 | // Use of this source code is governed by a BSD-style |
| 3 | // license that can be found in the LICENSE file. |
| 4 | |
| 5 | package getopt |
| 6 | |
| 7 | import ( |
| 8 | "fmt" |
| 9 | "strings" |
| 10 | ) |
| 11 | |
| 12 | type boolValue bool |
| 13 | |
| 14 | func (b *boolValue) Set(value string, opt Option) error { |
| 15 | switch strings.ToLower(value) { |
| 16 | case "", "1", "true", "on", "t": |
| 17 | *b = true |
| 18 | case "0", "false", "off", "f": |
| 19 | *b = false |
| 20 | default: |
| 21 | return fmt.Errorf("invalid value for bool %s: %q", opt.Name(), value) |
| 22 | } |
| 23 | return nil |
| 24 | } |
| 25 | |
| 26 | func (b *boolValue) String() string { |
| 27 | if *b { |
| 28 | return "true" |
| 29 | } |
| 30 | return "false" |
| 31 | } |
| 32 | |
| 33 | // Bool creates a flag option that is a bool. Bools normally do not take a |
| 34 | // value however one can be assigned by using the long form of the option: |
| 35 | // |
| 36 | // --option=true |
| 37 | // --o=false |
| 38 | // |
| 39 | // Its value is case insensitive and one of true, false, t, f, on, off, t and 0. |
| 40 | func Bool(name rune, helpvalue ...string) *bool { |
| 41 | return CommandLine.Bool(name, helpvalue...) |
| 42 | } |
| 43 | |
| 44 | func (s *Set) Bool(name rune, helpvalue ...string) *bool { |
| 45 | var p bool |
| 46 | s.BoolVarLong(&p, "", name, helpvalue...) |
| 47 | return &p |
| 48 | } |
| 49 | |
| 50 | func BoolLong(name string, short rune, helpvalue ...string) *bool { |
| 51 | return CommandLine.BoolLong(name, short, helpvalue...) |
| 52 | } |
| 53 | |
| 54 | func (s *Set) BoolLong(name string, short rune, helpvalue ...string) *bool { |
| 55 | var p bool |
| 56 | s.BoolVarLong(&p, name, short, helpvalue...) |
| 57 | return &p |
| 58 | } |
| 59 | |
| 60 | func BoolVar(p *bool, name rune, helpvalue ...string) Option { |
| 61 | return CommandLine.BoolVar(p, name, helpvalue...) |
| 62 | } |
| 63 | |
| 64 | func (s *Set) BoolVar(p *bool, name rune, helpvalue ...string) Option { |
| 65 | return s.BoolVarLong(p, "", name, helpvalue...) |
| 66 | } |
| 67 | |
| 68 | func BoolVarLong(p *bool, name string, short rune, helpvalue ...string) Option { |
| 69 | return CommandLine.BoolVarLong(p, name, short, helpvalue...) |
| 70 | } |
| 71 | |
| 72 | func (s *Set) BoolVarLong(p *bool, name string, short rune, helpvalue ...string) Option { |
| 73 | return s.VarLong((*boolValue)(p), name, short, helpvalue...).SetFlag() |
| 74 | } |