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
5package getopt
6
7import (
8	"fmt"
9	"strings"
10)
11
12type boolValue bool
13
14func (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
26func (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 insenstive and one of true, false, t, f, on, off, t and 0.
40func Bool(name rune, helpvalue ...string) *bool {
41	return CommandLine.Bool(name, helpvalue...)
42}
43
44func (s *Set) Bool(name rune, helpvalue ...string) *bool {
45	var p bool
46	s.BoolVarLong(&p, "", name, helpvalue...)
47	return &p
48}
49
50func BoolLong(name string, short rune, helpvalue ...string) *bool {
51	return CommandLine.BoolLong(name, short, helpvalue...)
52}
53
54func (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
60func BoolVar(p *bool, name rune, helpvalue ...string) Option {
61	return CommandLine.BoolVar(p, name, helpvalue...)
62}
63
64func (s *Set) BoolVar(p *bool, name rune, helpvalue ...string) Option {
65	return s.BoolVarLong(p, "", name, helpvalue...)
66}
67
68func BoolVarLong(p *bool, name string, short rune, helpvalue ...string) Option {
69	return CommandLine.BoolVarLong(p, name, short, helpvalue...)
70}
71
72func (s *Set) BoolVarLong(p *bool, name string, short rune, helpvalue ...string) Option {
73	return s.VarLong((*boolValue)(p), name, short, helpvalue...).SetFlag()
74}
75