| 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 "fmtscip-go gomod github.com/golang/go/src go1.13 fmt/" |
| 8 | |
| 9 | // An Error is returned by Getopt when it encounters an error. |
| 10 | type Errorscip-go gomod github.com/pborman/getopt . `github.com/pborman/getopt`/Error# struct { |
| 11 | ErrorCodescip-go gomod github.com/pborman/getopt . `github.com/pborman/getopt`/Error#ErrorCode. // General reason of failure. |
| 12 | Errscip-go gomod github.com/pborman/getopt . `github.com/pborman/getopt`/Error#Err. error // The actual error. |
| 13 | Parameterscip-go gomod github.com/pborman/getopt . `github.com/pborman/getopt`/Error#Parameter. string // Parameter passed to option, if any |
| 14 | Namescip-go gomod github.com/pborman/getopt . `github.com/pborman/getopt`/Error#Name. string // Option that cause error, if any |
| 15 | } |
| 16 | |
| 17 | // Error returns the error message, implementing the error interface. |
| 18 | func (i *Errorscip-go gomod github.com/pborman/getopt . `github.com/pborman/getopt`/Error#) Errorscip-go gomod github.com/pborman/getopt . `github.com/pborman/getopt`/Error#Error().() string { return i.Errscip-go gomod github.com/pborman/getopt . `github.com/pborman/getopt`/Error#Err..Error() } |
| 19 | |
| 20 | // An ErrorCode indicates what sort of error was encountered. |
| 21 | type ErrorCodescip-go gomod github.com/pborman/getopt . `github.com/pborman/getopt`/ErrorCode# int |
| 22 | |
| 23 | const ( |
| 24 | NoErrorscip-go gomod github.com/pborman/getopt . `github.com/pborman/getopt`/NoError. = ErrorCodescip-go gomod github.com/pborman/getopt . `github.com/pborman/getopt`/ErrorCode#(iota) |
| 25 | UnknownOptionscip-go gomod github.com/pborman/getopt . `github.com/pborman/getopt`/UnknownOption. // an invalid option was encountered |
| 26 | MissingParameterscip-go gomod github.com/pborman/getopt . `github.com/pborman/getopt`/MissingParameter. // the options parameter is missing |
| 27 | ExtraParameterscip-go gomod github.com/pborman/getopt . `github.com/pborman/getopt`/ExtraParameter. // a value was set to a long flag |
| 28 | Invalidscip-go gomod github.com/pborman/getopt . `github.com/pborman/getopt`/Invalid. // attempt to set an invalid value |
| 29 | ) |
| 30 | |
| 31 | func (e ErrorCodescip-go gomod github.com/pborman/getopt . `github.com/pborman/getopt`/ErrorCode#) Stringscip-go gomod github.com/pborman/getopt . `github.com/pborman/getopt`/ErrorCode#String().() string { |
| 32 | switch e { |
| 33 | case UnknownOptionscip-go gomod github.com/pborman/getopt . `github.com/pborman/getopt`/UnknownOption.: |
| 34 | return "unknown option" |
| 35 | case MissingParameterscip-go gomod github.com/pborman/getopt . `github.com/pborman/getopt`/MissingParameter.: |
| 36 | return "missing argument" |
| 37 | case ExtraParameterscip-go gomod github.com/pborman/getopt . `github.com/pborman/getopt`/ExtraParameter.: |
| 38 | return "unexpected value" |
| 39 | case Invalidscip-go gomod github.com/pborman/getopt . `github.com/pborman/getopt`/Invalid.: |
| 40 | return "error setting value" |
| 41 | } |
| 42 | return "unknown error" |
| 43 | } |
| 44 | |
| 45 | // unknownOption returns an Error indicating an unknown option was |
| 46 | // encountered. |
| 47 | func unknownOptionscip-go gomod github.com/pborman/getopt . `github.com/pborman/getopt`/unknownOption().(name interface{}) *Errorscip-go gomod github.com/pborman/getopt . `github.com/pborman/getopt`/Error# { |
| 48 | i := &Errorscip-go gomod github.com/pborman/getopt . `github.com/pborman/getopt`/Error#{ErrorCodescip-go gomod github.com/pborman/getopt . `github.com/pborman/getopt`/Error#ErrorCode.: UnknownOptionscip-go gomod github.com/pborman/getopt . `github.com/pborman/getopt`/UnknownOption.} |
| 49 | switch n := name.(type) { |
| 50 | case rune: |
| 51 | if n == '-' { |
| 52 | i.Namescip-go gomod github.com/pborman/getopt . `github.com/pborman/getopt`/Error#Name. = "-" |
| 53 | } else { |
| 54 | i.Namescip-go gomod github.com/pborman/getopt . `github.com/pborman/getopt`/Error#Name. = "-" + string(n) |
| 55 | } |
| 56 | case string: |
| 57 | i.Namescip-go gomod github.com/pborman/getopt . `github.com/pborman/getopt`/Error#Name. = "--" + n |
| 58 | } |
| 59 | i.Errscip-go gomod github.com/pborman/getopt . `github.com/pborman/getopt`/Error#Err. = fmtscip-go gomod github.com/golang/go/src go1.13 fmt/.Errorfscip-go gomod github.com/golang/go/src go1.13 fmt/Errorf().("unknown option: %s", i.Namescip-go gomod github.com/pborman/getopt . `github.com/pborman/getopt`/Error#Name.) |
| 60 | return i |
| 61 | } |
| 62 | |
| 63 | // missingArg returns an Error inidicating option o was not passed |
| 64 | // a required parameter. |
| 65 | func missingArgscip-go gomod github.com/pborman/getopt . `github.com/pborman/getopt`/missingArg().(o Optionscip-go gomod github.com/pborman/getopt . `github.com/pborman/getopt`/Option#) *Errorscip-go gomod github.com/pborman/getopt . `github.com/pborman/getopt`/Error# { |
| 66 | return &Errorscip-go gomod github.com/pborman/getopt . `github.com/pborman/getopt`/Error#{ |
| 67 | ErrorCodescip-go gomod github.com/pborman/getopt . `github.com/pborman/getopt`/Error#ErrorCode.: MissingParameterscip-go gomod github.com/pborman/getopt . `github.com/pborman/getopt`/MissingParameter., |
| 68 | Namescip-go gomod github.com/pborman/getopt . `github.com/pborman/getopt`/Error#Name.: o.Namescip-go gomod github.com/pborman/getopt . `github.com/pborman/getopt`/Option#Name.(), |
| 69 | Errscip-go gomod github.com/pborman/getopt . `github.com/pborman/getopt`/Error#Err.: fmtscip-go gomod github.com/golang/go/src go1.13 fmt/.Errorfscip-go gomod github.com/golang/go/src go1.13 fmt/Errorf().("missing parameter for %s", o.Namescip-go gomod github.com/pborman/getopt . `github.com/pborman/getopt`/Option#Name.()), |
| 70 | } |
| 71 | } |
| 72 | |
| 73 | // extraArg returns an Error inidicating option o was passed the |
| 74 | // unexpected parameter value. |
| 75 | func extraArgscip-go gomod github.com/pborman/getopt . `github.com/pborman/getopt`/extraArg().(o Optionscip-go gomod github.com/pborman/getopt . `github.com/pborman/getopt`/Option#, value string) *Errorscip-go gomod github.com/pborman/getopt . `github.com/pborman/getopt`/Error# { |
| 76 | return &Errorscip-go gomod github.com/pborman/getopt . `github.com/pborman/getopt`/Error#{ |
| 77 | ErrorCodescip-go gomod github.com/pborman/getopt . `github.com/pborman/getopt`/Error#ErrorCode.: ExtraParameterscip-go gomod github.com/pborman/getopt . `github.com/pborman/getopt`/ExtraParameter., |
| 78 | Namescip-go gomod github.com/pborman/getopt . `github.com/pborman/getopt`/Error#Name.: o.Namescip-go gomod github.com/pborman/getopt . `github.com/pborman/getopt`/Option#Name.(), |
| 79 | Parameterscip-go gomod github.com/pborman/getopt . `github.com/pborman/getopt`/Error#Parameter.: value, |
| 80 | Errscip-go gomod github.com/pborman/getopt . `github.com/pborman/getopt`/Error#Err.: fmtscip-go gomod github.com/golang/go/src go1.13 fmt/.Errorfscip-go gomod github.com/golang/go/src go1.13 fmt/Errorf().("unexpected parameter passed to %s: %q", o.Namescip-go gomod github.com/pborman/getopt . `github.com/pborman/getopt`/Option#Name.(), value), |
| 81 | } |
| 82 | } |
| 83 | |
| 84 | // setError returns an Error inidicating option o and the specified |
| 85 | // error while setting it to value. |
| 86 | func setErrorscip-go gomod github.com/pborman/getopt . `github.com/pborman/getopt`/setError().(o Optionscip-go gomod github.com/pborman/getopt . `github.com/pborman/getopt`/Option#, value string, err error) *Errorscip-go gomod github.com/pborman/getopt . `github.com/pborman/getopt`/Error# { |
| 87 | return &Errorscip-go gomod github.com/pborman/getopt . `github.com/pborman/getopt`/Error#{ |
| 88 | ErrorCodescip-go gomod github.com/pborman/getopt . `github.com/pborman/getopt`/Error#ErrorCode.: Invalidscip-go gomod github.com/pborman/getopt . `github.com/pborman/getopt`/Invalid., |
| 89 | Namescip-go gomod github.com/pborman/getopt . `github.com/pborman/getopt`/Error#Name.: o.Namescip-go gomod github.com/pborman/getopt . `github.com/pborman/getopt`/Option#Name.(), |
| 90 | Parameterscip-go gomod github.com/pborman/getopt . `github.com/pborman/getopt`/Error#Parameter.: value, |
| 91 | Errscip-go gomod github.com/pborman/getopt . `github.com/pborman/getopt`/Error#Err.: err, |
| 92 | } |
| 93 | } |