github.com/dolthub/dolt/go@v0.40.5-0.20240520175717-68db7794bea6/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  	"strings"
    21  
    22  	"github.com/dolthub/dolt/go/libraries/utils/set"
    23  )
    24  
    25  const (
    26  	NO_POSITIONAL_ARGS = -1
    27  )
    28  
    29  type ArgParseResults struct {
    30  	options map[string]string
    31  	Args    []string
    32  	parser  *ArgParser
    33  
    34  	PositionalArgsSeparatorIndex int
    35  }
    36  
    37  // Equals res and other are only considered equal if the order and contents of their arguments
    38  // are the same.
    39  func (res *ArgParseResults) Equals(other *ArgParseResults) bool {
    40  	if len(res.Args) != len(other.Args) || len(res.options) != len(other.options) {
    41  		return false
    42  	}
    43  
    44  	for i, arg := range res.Args {
    45  		if other.Args[i] != arg {
    46  			return false
    47  		}
    48  	}
    49  
    50  	for k, v := range res.options {
    51  		if otherVal, ok := other.options[k]; !ok || v != otherVal {
    52  			return false
    53  		}
    54  	}
    55  
    56  	return true
    57  }
    58  
    59  // NewEmptyResults creates a new ArgParseResults object with no arguments or options. Mostly useful for testing.
    60  func NewEmptyResults() *ArgParseResults {
    61  	return &ArgParseResults{options: make(map[string]string), Args: make([]string, 0)}
    62  }
    63  
    64  func (res *ArgParseResults) Contains(name string) bool {
    65  	_, ok := res.options[name]
    66  	return ok
    67  }
    68  
    69  func (res *ArgParseResults) ContainsArg(name string) bool {
    70  	for _, val := range res.Args {
    71  		if val == name {
    72  			return true
    73  		}
    74  	}
    75  	return false
    76  }
    77  
    78  func (res *ArgParseResults) ContainsAll(names ...string) bool {
    79  	for _, name := range names {
    80  		if _, ok := res.options[name]; !ok {
    81  			return false
    82  		}
    83  	}
    84  
    85  	return true
    86  }
    87  
    88  func (res *ArgParseResults) ContainsAny(names ...string) bool {
    89  	for _, name := range names {
    90  		if _, ok := res.options[name]; ok {
    91  			return true
    92  		}
    93  	}
    94  
    95  	return false
    96  }
    97  
    98  func (res *ArgParseResults) ContainsMany(names ...string) []string {
    99  	var contains []string
   100  	for _, name := range names {
   101  		if _, ok := res.options[name]; ok {
   102  			contains = append(contains, name)
   103  		}
   104  	}
   105  	return contains
   106  }
   107  
   108  func (res *ArgParseResults) GetValue(name string) (string, bool) {
   109  	val, ok := res.options[name]
   110  	return val, ok
   111  }
   112  
   113  func (res *ArgParseResults) GetValueList(name string) ([]string, bool) {
   114  	val, ok := res.options[name]
   115  	return strings.Split(val, ","), ok
   116  }
   117  
   118  func (res *ArgParseResults) GetValues(names ...string) map[string]string {
   119  	vals := make(map[string]string)
   120  
   121  	for _, name := range names {
   122  		if val, ok := res.options[name]; ok {
   123  			vals[name] = val
   124  		}
   125  	}
   126  
   127  	return vals
   128  }
   129  
   130  // DropValue removes the value for the given name from the results. A new ArgParseResults object is returned without the
   131  // names value. If the value is not present in the results then the original results object is returned.
   132  func (res *ArgParseResults) DropValue(name string) *ArgParseResults {
   133  	if _, ok := res.options[name]; !ok {
   134  		return res
   135  	}
   136  
   137  	newNamedArgs := make(map[string]string, len(res.options)-1)
   138  	for flag, val := range res.options {
   139  		if flag != name {
   140  			newNamedArgs[flag] = val
   141  		}
   142  	}
   143  
   144  	return &ArgParseResults{newNamedArgs, res.Args, res.parser, NO_POSITIONAL_ARGS}
   145  }
   146  
   147  func (res *ArgParseResults) MustGetValue(name string) string {
   148  	val, ok := res.options[name]
   149  
   150  	if !ok {
   151  		panic("Value not available.")
   152  	}
   153  
   154  	return val
   155  }
   156  
   157  func (res *ArgParseResults) GetValueOrDefault(name, defVal string) string {
   158  	val, ok := res.options[name]
   159  
   160  	if ok {
   161  		return val
   162  	}
   163  
   164  	return defVal
   165  }
   166  
   167  func (res *ArgParseResults) GetInt(name string) (int, bool) {
   168  	val, ok := res.options[name]
   169  
   170  	if !ok {
   171  		return math.MinInt32, false
   172  	}
   173  
   174  	intVal, err := strconv.ParseInt(val, 10, 32)
   175  	if err != nil {
   176  		return math.MinInt32, false
   177  	}
   178  
   179  	return int(intVal), true
   180  }
   181  
   182  func (res *ArgParseResults) GetUint(name string) (uint64, bool) {
   183  	val, ok := res.options[name]
   184  
   185  	if !ok {
   186  		return math.MaxUint64, false
   187  	}
   188  
   189  	uintVal, err := strconv.ParseUint(val, 10, 64)
   190  	if err != nil {
   191  		return math.MaxUint64, false
   192  	}
   193  
   194  	return uintVal, true
   195  }
   196  
   197  func (res *ArgParseResults) GetIntOrDefault(name string, defVal int) int {
   198  	n, ok := res.GetInt(name)
   199  
   200  	if ok {
   201  		return n
   202  	}
   203  
   204  	return defVal
   205  }
   206  
   207  func (res *ArgParseResults) NArg() int {
   208  	return len(res.Args)
   209  }
   210  
   211  func (res *ArgParseResults) Arg(idx int) string {
   212  	return res.Args[idx]
   213  }
   214  
   215  func (res *ArgParseResults) AnyFlagsEqualTo(val bool) *set.StrSet {
   216  	results := make([]string, 0, len(res.parser.Supported))
   217  	for _, opt := range res.parser.Supported {
   218  		if opt.OptType == OptionalFlag {
   219  			name := opt.Name
   220  			_, ok := res.options[name]
   221  
   222  			if ok == val {
   223  				results = append(results, name)
   224  			}
   225  		}
   226  	}
   227  
   228  	return set.NewStrSet(results)
   229  }
   230  
   231  func (res *ArgParseResults) FlagsEqualTo(names []string, val bool) *set.StrSet {
   232  	results := make([]string, 0, len(res.parser.Supported))
   233  	for _, name := range names {
   234  		opt, ok := res.parser.nameOrAbbrevToOpt[name]
   235  		if ok && opt.OptType == OptionalFlag {
   236  			_, ok := res.options[name]
   237  
   238  			if ok == val {
   239  				results = append(results, name)
   240  			}
   241  		}
   242  	}
   243  
   244  	return set.NewStrSet(results)
   245  }