github.com/whatap/golib@v0.0.22/util/shellarg/ShellArg.go (about)

     1  package shellarg
     2  
     3  import (
     4  	"strings"
     5  
     6  	"github.com/whatap/golib/util/castutil"
     7  	"github.com/whatap/golib/util/hmap"
     8  )
     9  
    10  type ShellArg struct {
    11  	parameter  *hmap.StringKeyLinkedMap
    12  	parameter2 *hmap.StringKeyLinkedMap
    13  	Tags       *hmap.StringKeyLinkedMap
    14  }
    15  
    16  func HasNextValue(i int, args []string) bool {
    17  	return i+1 < len(args) && strings.HasPrefix(args[i+1], "-") == false
    18  }
    19  
    20  func NewShellArg(args []string) *ShellArg {
    21  	this := new(ShellArg)
    22  	this.Tags = hmap.NewStringKeyLinkedMap()
    23  	this.parameter = hmap.NewStringKeyLinkedMap()
    24  	this.parameter2 = hmap.NewStringKeyLinkedMap()
    25  
    26  	max := len(args)
    27  	i := 0
    28  	for i < max {
    29  		if strings.HasPrefix(args[i], "-tag.") {
    30  			this.Tags.Put(args[i], args[i][5:])
    31  		}
    32  		if HasNextValue(i, args) {
    33  			key := args[i]
    34  			this.parameter.Put(key, args[i+1])
    35  			i++
    36  			if HasNextValue(i, args) {
    37  				this.parameter2.Put(key, args[i+1])
    38  				i++
    39  			}
    40  		} else {
    41  			this.parameter.Put(args[i], "")
    42  		}
    43  		i++
    44  	}
    45  	return this
    46  }
    47  
    48  func (this *ShellArg) HasKey(key string) bool {
    49  	return this.parameter.ContainsKey(key)
    50  }
    51  
    52  func (this *ShellArg) Keys() hmap.StringEnumer {
    53  	return this.parameter.Keys()
    54  }
    55  
    56  func (this *ShellArg) Get(key string, defaultValue string) string {
    57  	s := this.parameter.Get(key)
    58  	if s == nil {
    59  		return defaultValue
    60  	} else {
    61  		return s.(string)
    62  	}
    63  }
    64  
    65  func (this *ShellArg) GetInt(key string, defaultValue int32) int32 {
    66  	s := this.parameter.Get(key)
    67  	if s == nil {
    68  		return defaultValue
    69  	} else {
    70  		return castutil.CInt(s)
    71  	}
    72  }
    73  func (this *ShellArg) GetLong(key string, defaultValue int64) int64 {
    74  	s := this.parameter.Get(key)
    75  	if s == nil {
    76  		return defaultValue
    77  	} else {
    78  		return castutil.CLong(s)
    79  	}
    80  }
    81  func (this *ShellArg) GetBoolean(key string, defaultValue bool) bool {
    82  	s := this.parameter.Get(key)
    83  	if s == nil {
    84  		return defaultValue
    85  	} else {
    86  		return castutil.CBool(s)
    87  	}
    88  }
    89  
    90  func (this *ShellArg) Get2(key string) string {
    91  	return this.parameter2.Get(key).(string)
    92  }
    93  
    94  func (this *ShellArg) Put(key string, value string) {
    95  	this.parameter.Put(key, value)
    96  }