| 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 | "strconv" |
| 10 | ) |
| 11 | |
| 12 | type int16Value int16 |
| 13 | |
| 14 | func (i *int16Value) Set(value string, opt Option) error { |
| 15 | v, err := strconv.ParseInt(value, 0, 16) |
| 16 | if err != nil { |
| 17 | if e, ok := err.(*strconv.NumError); ok { |
| 18 | switch e.Err { |
| 19 | case strconv.ErrRange: |
| 20 | err = fmt.Errorf("value out of range: %s", value) |
| 21 | case strconv.ErrSyntax: |
| 22 | err = fmt.Errorf("not a valid number: %s", value) |
| 23 | } |
| 24 | } |
| 25 | return err |
| 26 | } |
| 27 | *i = int16Value(v) |
| 28 | return nil |
| 29 | } |
| 30 | |
| 31 | func (i *int16Value) String() string { |
| 32 | return strconv.FormatInt(int64(*i), 10) |
| 33 | } |
| 34 | |
| 35 | // Int16 creates an option that parses its value as an int16. |
| 36 | func Int16(name rune, value int16, helpvalue ...string) *int16 { |
| 37 | return CommandLine.Int16(name, value, helpvalue...) |
| 38 | } |
| 39 | |
| 40 | func (s *Set) Int16(name rune, value int16, helpvalue ...string) *int16 { |
| 41 | return s.Int16Long("", name, value, helpvalue...) |
| 42 | } |
| 43 | |
| 44 | func Int16Long(name string, short rune, value int16, helpvalue ...string) *int16 { |
| 45 | return CommandLine.Int16Long(name, short, value, helpvalue...) |
| 46 | } |
| 47 | |
| 48 | func (s *Set) Int16Long(name string, short rune, value int16, helpvalue ...string) *int16 { |
| 49 | s.Int16VarLong(&value, name, short, helpvalue...) |
| 50 | return &value |
| 51 | } |
| 52 | |
| 53 | func Int16Var(p *int16, name rune, helpvalue ...string) Option { |
| 54 | return CommandLine.Int16Var(p, name, helpvalue...) |
| 55 | } |
| 56 | |
| 57 | func (s *Set) Int16Var(p *int16, name rune, helpvalue ...string) Option { |
| 58 | return s.Int16VarLong(p, "", name, helpvalue...) |
| 59 | } |
| 60 | |
| 61 | func Int16VarLong(p *int16, name string, short rune, helpvalue ...string) Option { |
| 62 | return CommandLine.Int16VarLong(p, name, short, helpvalue...) |
| 63 | } |
| 64 | |
| 65 | func (s *Set) Int16VarLong(p *int16, name string, short rune, helpvalue ...string) Option { |
| 66 | return s.VarLong((*int16Value)(p), name, short, helpvalue...) |
| 67 | } |