github.com/hasnat/dolt/go@v0.0.0-20210628190320-9eb5d843fbb7/libraries/utils/argparser/option.go (about) 1 // Copyright 2019 Dolthub, Inc. 2 // 3 // Licensed under the Apache License, Version 2.0 (the "License"); 4 // you may not use this file except in compliance with the License. 5 // You may obtain a copy of the License at 6 // 7 // http://www.apache.org/licenses/LICENSE-2.0 8 // 9 // Unless required by applicable law or agreed to in writing, software 10 // distributed under the License is distributed on an "AS IS" BASIS, 11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 // See the License for the specific language governing permissions and 13 // limitations under the License. 14 15 package argparser 16 17 import ( 18 "errors" 19 "strconv" 20 ) 21 22 type OptionType int 23 24 const ( 25 OptionalFlag OptionType = iota 26 OptionalValue 27 ) 28 29 type ValidationFunc func(string) error 30 31 // Convenience validation function that asserts that an arg is an integer 32 func isIntStr(str string) error { 33 _, err := strconv.ParseInt(str, 10, 32) 34 if err != nil { 35 return errors.New("error: \"" + str + "\" is not a valid int.") 36 } 37 38 return nil 39 } 40 41 // Convenience validation function that asserts that an arg is an unsigned integer 42 func isUintStr(str string) error { 43 _, err := strconv.ParseUint(str, 10, 64) 44 if err != nil { 45 return errors.New("error: \"" + str + "\" is not a valid uint.") 46 } 47 48 return nil 49 } 50 51 // An Option encapsulates all the information necessary to represent and parse a command line argument. 52 type Option struct { 53 // Long name for this Option, specified on the command line with --Name. Required. 54 Name string 55 // Abbreviated name for this Option, specified on the command line with -Abbrev. Optional. 56 Abbrev string 57 // Brief description of the Option. 58 ValDesc string 59 // The type of this option, either a flag or a value. 60 OptType OptionType 61 // Longer help text for the option. 62 Desc string 63 // Function to validate an Option after parsing, returning any error. 64 Validator ValidationFunc 65 }