| 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 getoptscip-go gomod github.com/pborman/getopt . `github.com/pborman/getopt`/ |
| 6 | |
| 7 | import ( |
| 8 | "fmtscip-go gomod github.com/golang/go/src go1.13 fmt/" |
| 9 | "strconvscip-go gomod github.com/golang/go/src go1.13 strconv/" |
| 10 | ) |
| 11 | |
| 12 | type signedscip-go gomod github.com/pborman/getopt . `github.com/pborman/getopt`/signed# int64 |
| 13 | |
| 14 | type SignedLimitscip-go gomod github.com/pborman/getopt . `github.com/pborman/getopt`/SignedLimit# struct { |
| 15 | Basescip-go gomod github.com/pborman/getopt . `github.com/pborman/getopt`/SignedLimit#Base. int // Base for conversion as per strconv.ParseInt |
| 16 | Bitsscip-go gomod github.com/pborman/getopt . `github.com/pborman/getopt`/SignedLimit#Bits. int // Number of bits as per strconv.ParseInt |
| 17 | Minscip-go gomod github.com/pborman/getopt . `github.com/pborman/getopt`/SignedLimit#Min. int64 // Minimum allowed value if both Min and Max are not 0 |
| 18 | Maxscip-go gomod github.com/pborman/getopt . `github.com/pborman/getopt`/SignedLimit#Max. int64 // Maximum allowed value if both Min and Max are not 0 |
| 19 | } |
| 20 | |
| 21 | var signedLimitsscip-go gomod github.com/pborman/getopt . `github.com/pborman/getopt`/signedLimits. = make(map[*signedscip-go gomod github.com/pborman/getopt . `github.com/pborman/getopt`/signed#]*SignedLimitscip-go gomod github.com/pborman/getopt . `github.com/pborman/getopt`/SignedLimit#) |
| 22 | |
| 23 | func (n *signedscip-go gomod github.com/pborman/getopt . `github.com/pborman/getopt`/signed#) Setscip-go gomod github.com/pborman/getopt . `github.com/pborman/getopt`/signed#Set().(value string, opt Optionscip-go gomod github.com/pborman/getopt . `github.com/pborman/getopt`/Option#) error { |
| 24 | l := signedLimitsscip-go gomod github.com/pborman/getopt . `github.com/pborman/getopt`/signedLimits.[n] |
| 25 | if l == nil { |
| 26 | return fmtscip-go gomod github.com/golang/go/src go1.13 fmt/.Errorfscip-go gomod github.com/golang/go/src go1.13 fmt/Errorf().("no limits defined for %s", opt.Namescip-go gomod github.com/pborman/getopt . `github.com/pborman/getopt`/Option#Name.()) |
| 27 | } |
| 28 | v, err := strconvscip-go gomod github.com/golang/go/src go1.13 strconv/.ParseIntscip-go gomod github.com/golang/go/src go1.13 strconv/ParseInt().(value, l.Basescip-go gomod github.com/pborman/getopt . `github.com/pborman/getopt`/SignedLimit#Base., l.Bitsscip-go gomod github.com/pborman/getopt . `github.com/pborman/getopt`/SignedLimit#Bits.) |
| 29 | if err != nil { |
| 30 | if e, ok := err.(*strconvscip-go gomod github.com/golang/go/src go1.13 strconv/.NumErrorscip-go gomod github.com/golang/go/src go1.13 strconv/NumError#); ok { |
| 31 | switch e.Errscip-go gomod github.com/golang/go/src go1.13 strconv/NumError#Err. { |
| 32 | case strconvscip-go gomod github.com/golang/go/src go1.13 strconv/.ErrRangescip-go gomod github.com/golang/go/src go1.13 strconv/ErrRange.: |
| 33 | err = fmtscip-go gomod github.com/golang/go/src go1.13 fmt/.Errorfscip-go gomod github.com/golang/go/src go1.13 fmt/Errorf().("value out of range: %s", value) |
| 34 | case strconvscip-go gomod github.com/golang/go/src go1.13 strconv/.ErrSyntaxscip-go gomod github.com/golang/go/src go1.13 strconv/ErrSyntax.: |
| 35 | err = fmtscip-go gomod github.com/golang/go/src go1.13 fmt/.Errorfscip-go gomod github.com/golang/go/src go1.13 fmt/Errorf().("not a valid number: %s", value) |
| 36 | } |
| 37 | } |
| 38 | return err |
| 39 | } |
| 40 | if l.Minscip-go gomod github.com/pborman/getopt . `github.com/pborman/getopt`/SignedLimit#Min. != 0 || l.Maxscip-go gomod github.com/pborman/getopt . `github.com/pborman/getopt`/SignedLimit#Max. != 0 { |
| 41 | if v < l.Minscip-go gomod github.com/pborman/getopt . `github.com/pborman/getopt`/SignedLimit#Min. { |
| 42 | return fmtscip-go gomod github.com/golang/go/src go1.13 fmt/.Errorfscip-go gomod github.com/golang/go/src go1.13 fmt/Errorf().("value out of range (<%v): %s", l.Minscip-go gomod github.com/pborman/getopt . `github.com/pborman/getopt`/SignedLimit#Min., value) |
| 43 | } |
| 44 | if v > l.Maxscip-go gomod github.com/pborman/getopt . `github.com/pborman/getopt`/SignedLimit#Max. { |
| 45 | return fmtscip-go gomod github.com/golang/go/src go1.13 fmt/.Errorfscip-go gomod github.com/golang/go/src go1.13 fmt/Errorf().("value out of range (>%v): %s", l.Maxscip-go gomod github.com/pborman/getopt . `github.com/pborman/getopt`/SignedLimit#Max., value) |
| 46 | } |
| 47 | } |
| 48 | *n = signedscip-go gomod github.com/pborman/getopt . `github.com/pborman/getopt`/signed#(v) |
| 49 | return nil |
| 50 | } |
| 51 | |
| 52 | func (n *signedscip-go gomod github.com/pborman/getopt . `github.com/pborman/getopt`/signed#) Stringscip-go gomod github.com/pborman/getopt . `github.com/pborman/getopt`/signed#String().() string { |
| 53 | l := signedLimitsscip-go gomod github.com/pborman/getopt . `github.com/pborman/getopt`/signedLimits.[n] |
| 54 | if l != nil && l.Basescip-go gomod github.com/pborman/getopt . `github.com/pborman/getopt`/SignedLimit#Base. != 0 { |
| 55 | return strconvscip-go gomod github.com/golang/go/src go1.13 strconv/.FormatIntscip-go gomod github.com/golang/go/src go1.13 strconv/FormatInt().(int64(*n), l.Basescip-go gomod github.com/pborman/getopt . `github.com/pborman/getopt`/SignedLimit#Base.) |
| 56 | } |
| 57 | return strconvscip-go gomod github.com/golang/go/src go1.13 strconv/.FormatIntscip-go gomod github.com/golang/go/src go1.13 strconv/FormatInt().(int64(*n), 10) |
| 58 | } |
| 59 | |
| 60 | // Signed creates an option that is stored in an int64 and is constrained |
| 61 | // by the limits pointed to by l. The Max and Min values are only used if |
| 62 | // at least one of the values are not 0. If Base is 0, the base is implied by |
| 63 | // the string's prefix: base 16 for "0x", base 8 for "0", and base 10 otherwise. |
| 64 | func Signedscip-go gomod github.com/pborman/getopt . `github.com/pborman/getopt`/Signed().(name rune, value int64, l *SignedLimitscip-go gomod github.com/pborman/getopt . `github.com/pborman/getopt`/SignedLimit#, helpvalue ...string) *int64 { |
| 65 | return CommandLinescip-go gomod github.com/pborman/getopt . `github.com/pborman/getopt`/CommandLine..Signedscip-go gomod github.com/pborman/getopt . `github.com/pborman/getopt`/Set#Signed().(name, value, l, helpvalue...) |
| 66 | } |
| 67 | |
| 68 | func (s *Setscip-go gomod github.com/pborman/getopt . `github.com/pborman/getopt`/Set#) Signedscip-go gomod github.com/pborman/getopt . `github.com/pborman/getopt`/Set#Signed().(name rune, value int64, l *SignedLimitscip-go gomod github.com/pborman/getopt . `github.com/pborman/getopt`/SignedLimit#, helpvalue ...string) *int64 { |
| 69 | return s.SignedLongscip-go gomod github.com/pborman/getopt . `github.com/pborman/getopt`/Set#SignedLong().("", name, value, l, helpvalue...) |
| 70 | } |
| 71 | |
| 72 | func SignedLongscip-go gomod github.com/pborman/getopt . `github.com/pborman/getopt`/SignedLong().(name string, short rune, value int64, l *SignedLimitscip-go gomod github.com/pborman/getopt . `github.com/pborman/getopt`/SignedLimit#, helpvalue ...string) *int64 { |
| 73 | return CommandLinescip-go gomod github.com/pborman/getopt . `github.com/pborman/getopt`/CommandLine..SignedLongscip-go gomod github.com/pborman/getopt . `github.com/pborman/getopt`/Set#SignedLong().(name, short, value, l, helpvalue...) |
| 74 | } |
| 75 | |
| 76 | func (s *Setscip-go gomod github.com/pborman/getopt . `github.com/pborman/getopt`/Set#) SignedLongscip-go gomod github.com/pborman/getopt . `github.com/pborman/getopt`/Set#SignedLong().(name string, short rune, value int64, l *SignedLimitscip-go gomod github.com/pborman/getopt . `github.com/pborman/getopt`/SignedLimit#, helpvalue ...string) *int64 { |
| 77 | s.SignedVarLongscip-go gomod github.com/pborman/getopt . `github.com/pborman/getopt`/Set#SignedVarLong().(&value, name, short, l, helpvalue...) |
| 78 | return &value |
| 79 | } |
| 80 | |
| 81 | func SignedVarscip-go gomod github.com/pborman/getopt . `github.com/pborman/getopt`/SignedVar().(p *int64, name rune, l *SignedLimitscip-go gomod github.com/pborman/getopt . `github.com/pborman/getopt`/SignedLimit#, helpvalue ...string) Optionscip-go gomod github.com/pborman/getopt . `github.com/pborman/getopt`/Option# { |
| 82 | return CommandLinescip-go gomod github.com/pborman/getopt . `github.com/pborman/getopt`/CommandLine..SignedVarscip-go gomod github.com/pborman/getopt . `github.com/pborman/getopt`/Set#SignedVar().(p, name, l, helpvalue...) |
| 83 | } |
| 84 | |
| 85 | func (s *Setscip-go gomod github.com/pborman/getopt . `github.com/pborman/getopt`/Set#) SignedVarscip-go gomod github.com/pborman/getopt . `github.com/pborman/getopt`/Set#SignedVar().(p *int64, name rune, l *SignedLimitscip-go gomod github.com/pborman/getopt . `github.com/pborman/getopt`/SignedLimit#, helpvalue ...string) Optionscip-go gomod github.com/pborman/getopt . `github.com/pborman/getopt`/Option# { |
| 86 | return s.SignedVarLongscip-go gomod github.com/pborman/getopt . `github.com/pborman/getopt`/Set#SignedVarLong().(p, "", name, l, helpvalue...) |
| 87 | } |
| 88 | |
| 89 | func SignedVarLongscip-go gomod github.com/pborman/getopt . `github.com/pborman/getopt`/SignedVarLong().(p *int64, name string, short rune, l *SignedLimitscip-go gomod github.com/pborman/getopt . `github.com/pborman/getopt`/SignedLimit#, helpvalue ...string) Optionscip-go gomod github.com/pborman/getopt . `github.com/pborman/getopt`/Option# { |
| 90 | return CommandLinescip-go gomod github.com/pborman/getopt . `github.com/pborman/getopt`/CommandLine..SignedVarLongscip-go gomod github.com/pborman/getopt . `github.com/pborman/getopt`/Set#SignedVarLong().(p, name, short, l, helpvalue...) |
| 91 | } |
| 92 | |
| 93 | func (s *Setscip-go gomod github.com/pborman/getopt . `github.com/pborman/getopt`/Set#) SignedVarLongscip-go gomod github.com/pborman/getopt . `github.com/pborman/getopt`/Set#SignedVarLong().(p *int64, name string, short rune, l *SignedLimitscip-go gomod github.com/pborman/getopt . `github.com/pborman/getopt`/SignedLimit#, helpvalue ...string) Optionscip-go gomod github.com/pborman/getopt . `github.com/pborman/getopt`/Option# { |
| 94 | opt := s.VarLongscip-go gomod github.com/pborman/getopt . `github.com/pborman/getopt`/Set#VarLong().((*signedscip-go gomod github.com/pborman/getopt . `github.com/pborman/getopt`/signed#)(p), name, short, helpvalue...) |
| 95 | if l.Basescip-go gomod github.com/pborman/getopt . `github.com/pborman/getopt`/SignedLimit#Base. > 36 || l.Basescip-go gomod github.com/pborman/getopt . `github.com/pborman/getopt`/SignedLimit#Base. == 1 || l.Basescip-go gomod github.com/pborman/getopt . `github.com/pborman/getopt`/SignedLimit#Base. < 0 { |
| 96 | fmtscip-go gomod github.com/golang/go/src go1.13 fmt/.Fprintfscip-go gomod github.com/golang/go/src go1.13 fmt/Fprintf().(stderrscip-go gomod github.com/pborman/getopt . `github.com/pborman/getopt`/stderr., "invalid base for %s: %d\n", opt.Namescip-go gomod github.com/pborman/getopt . `github.com/pborman/getopt`/Option#Name.(), l.Basescip-go gomod github.com/pborman/getopt . `github.com/pborman/getopt`/SignedLimit#Base.) |
| 97 | exitscip-go gomod github.com/pborman/getopt . `github.com/pborman/getopt`/exit.(1) |
| 98 | } |
| 99 | if l.Bitsscip-go gomod github.com/pborman/getopt . `github.com/pborman/getopt`/SignedLimit#Bits. < 0 || l.Bitsscip-go gomod github.com/pborman/getopt . `github.com/pborman/getopt`/SignedLimit#Bits. > 64 { |
| 100 | fmtscip-go gomod github.com/golang/go/src go1.13 fmt/.Fprintfscip-go gomod github.com/golang/go/src go1.13 fmt/Fprintf().(stderrscip-go gomod github.com/pborman/getopt . `github.com/pborman/getopt`/stderr., "invalid bit size for %s: %d\n", opt.Namescip-go gomod github.com/pborman/getopt . `github.com/pborman/getopt`/Option#Name.(), l.Bitsscip-go gomod github.com/pborman/getopt . `github.com/pborman/getopt`/SignedLimit#Bits.) |
| 101 | exitscip-go gomod github.com/pborman/getopt . `github.com/pborman/getopt`/exit.(1) |
| 102 | } |
| 103 | if l.Minscip-go gomod github.com/pborman/getopt . `github.com/pborman/getopt`/SignedLimit#Min. > l.Maxscip-go gomod github.com/pborman/getopt . `github.com/pborman/getopt`/SignedLimit#Max. { |
| 104 | fmtscip-go gomod github.com/golang/go/src go1.13 fmt/.Fprintfscip-go gomod github.com/golang/go/src go1.13 fmt/Fprintf().(stderrscip-go gomod github.com/pborman/getopt . `github.com/pborman/getopt`/stderr., "min greater than max for %s\n", opt.Namescip-go gomod github.com/pborman/getopt . `github.com/pborman/getopt`/Option#Name.()) |
| 105 | exitscip-go gomod github.com/pborman/getopt . `github.com/pborman/getopt`/exit.(1) |
| 106 | } |
| 107 | lim := *l |
| 108 | signedLimitsscip-go gomod github.com/pborman/getopt . `github.com/pborman/getopt`/signedLimits.[(*signedscip-go gomod github.com/pborman/getopt . `github.com/pborman/getopt`/signed#)(p)] = &lim |
| 109 | return opt |
| 110 | } |