github.com/hasnat/dolt/go@v0.0.0-20210628190320-9eb5d843fbb7/libraries/utils/argparser/results.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  	"math"
    19  	"strconv"
    20  
    21  	"github.com/dolthub/dolt/go/libraries/utils/set"
    22  )
    23  
    24  type ArgParseResults struct {
    25  	options map[string]string
    26  	args    []string
    27  	parser  *ArgParser
    28  }
    29  
    30  func (res *ArgParseResults) Equals(other *ArgParseResults) bool {
    31  	if len(res.args) != len(other.args) || len(res.options) != len(res.options) {
    32  		return false
    33  	}
    34  
    35  	for i, arg := range res.args {
    36  		if other.args[i] != arg {
    37  			return false
    38  		}
    39  	}
    40  
    41  	for k, v := range res.options {
    42  		if otherVal, ok := other.options[k]; !ok || v != otherVal {
    43  			return false
    44  		}
    45  	}
    46  
    47  	return true
    48  }
    49  
    50  func (res *ArgParseResults) Contains(name string) bool {
    51  	_, ok := res.options[name]
    52  	return ok
    53  }
    54  
    55  func (res *ArgParseResults) ContainsArg(name string) bool {
    56  	for _, val := range res.args {
    57  		if val == name {
    58  			return true
    59  		}
    60  	}
    61  	return false
    62  }
    63  
    64  func (res *ArgParseResults) ContainsAll(names ...string) bool {
    65  	for _, name := range names {
    66  		if _, ok := res.options[name]; !ok {
    67  			return false
    68  		}
    69  	}
    70  
    71  	return true
    72  }
    73  
    74  func (res *ArgParseResults) ContainsAny(names ...string) bool {
    75  	for _, name := range names {
    76  		if _, ok := res.options[name]; ok {
    77  			return true
    78  		}
    79  	}
    80  
    81  	return false
    82  }
    83  
    84  func (res *ArgParseResults) ContainsMany(names ...string) []string {
    85  	var contains []string
    86  	for _, name := range names {
    87  		if _, ok := res.options[name]; ok {
    88  			contains = append(contains, name)
    89  		}
    90  	}
    91  	return contains
    92  }
    93  
    94  func (res *ArgParseResults) GetValue(name string) (string, bool) {
    95  	val, ok := res.options[name]
    96  	return val, ok
    97  }
    98  
    99  func (res *ArgParseResults) GetValues(names ...string) map[string]string {
   100  	vals := make(map[string]string)
   101  
   102  	for _, name := range names {
   103  		if val, ok := res.options[name]; ok {
   104  			vals[name] = val
   105  		}
   106  	}
   107  
   108  	return vals
   109  }
   110  
   111  func (res *ArgParseResults) MustGetValue(name string) string {
   112  	val, ok := res.options[name]
   113  
   114  	if !ok {
   115  		panic("Value not available.")
   116  	}
   117  
   118  	return val
   119  }
   120  
   121  func (res *ArgParseResults) GetValueOrDefault(name, defVal string) string {
   122  	val, ok := res.options[name]
   123  
   124  	if ok {
   125  		return val
   126  	}
   127  
   128  	return defVal
   129  }
   130  
   131  func (res *ArgParseResults) GetInt(name string) (int, bool) {
   132  	val, ok := res.options[name]
   133  
   134  	if !ok {
   135  		return math.MinInt32, false
   136  	}
   137  
   138  	intVal, err := strconv.ParseInt(val, 10, 32)
   139  	if err != nil {
   140  		return math.MinInt32, false
   141  	}
   142  
   143  	return int(intVal), true
   144  }
   145  
   146  func (res *ArgParseResults) GetUint(name string) (uint64, bool) {
   147  	val, ok := res.options[name]
   148  
   149  	if !ok {
   150  		return math.MaxUint64, false
   151  	}
   152  
   153  	uintVal, err := strconv.ParseUint(val, 10, 64)
   154  	if err != nil {
   155  		return math.MaxUint64, false
   156  	}
   157  
   158  	return uintVal, true
   159  }
   160  
   161  func (res *ArgParseResults) GetIntOrDefault(name string, defVal int) int {
   162  	n, ok := res.GetInt(name)
   163  
   164  	if ok {
   165  		return n
   166  	}
   167  
   168  	return defVal
   169  }
   170  
   171  func (res *ArgParseResults) Args() []string {
   172  	return res.args
   173  }
   174  
   175  func (res *ArgParseResults) NArg() int {
   176  	return len(res.args)
   177  }
   178  
   179  func (res *ArgParseResults) Arg(idx int) string {
   180  	return res.args[idx]
   181  }
   182  
   183  func (res *ArgParseResults) AnyFlagsEqualTo(val bool) *set.StrSet {
   184  	results := make([]string, 0, len(res.parser.Supported))
   185  	for _, opt := range res.parser.Supported {
   186  		if opt.OptType == OptionalFlag {
   187  			name := opt.Name
   188  			_, ok := res.options[name]
   189  
   190  			if ok == val {
   191  				results = append(results, name)
   192  			}
   193  		}
   194  	}
   195  
   196  	return set.NewStrSet(results)
   197  }
   198  
   199  func (res *ArgParseResults) FlagsEqualTo(names []string, val bool) *set.StrSet {
   200  	results := make([]string, 0, len(res.parser.Supported))
   201  	for _, name := range names {
   202  		opt, ok := res.parser.NameOrAbbrevToOpt[name]
   203  		if ok && opt.OptType == OptionalFlag {
   204  			_, ok := res.options[name]
   205  
   206  			if ok == val {
   207  				results = append(results, name)
   208  			}
   209  		}
   210  	}
   211  
   212  	return set.NewStrSet(results)
   213  }