waveterm/pkg/base/optsiter.go
2022-06-27 22:39:16 -07:00

54 lines
1.0 KiB
Go

// Copyright 2022 Dashborg Inc
//
// This Source Code Form is subject to the terms of the Mozilla Public
// License, v. 2.0. If a copy of the MPL was not distributed with this
// file, You can obtain one at https://mozilla.org/MPL/2.0/.
package base
import "strings"
type OptsIter struct {
Pos int
Opts []string
}
func MakeOptsIter(opts []string) *OptsIter {
return &OptsIter{Opts: opts}
}
func IsOption(argStr string) bool {
return strings.HasPrefix(argStr, "-") && argStr != "-" && !strings.HasPrefix(argStr, "-/")
}
func (iter *OptsIter) HasNext() bool {
return iter.Pos <= len(iter.Opts)-1
}
func (iter *OptsIter) IsNextPlain() bool {
if !iter.HasNext() {
return false
}
return !IsOption(iter.Opts[iter.Pos])
}
func (iter *OptsIter) Next() string {
if iter.Pos >= len(iter.Opts) {
return ""
}
rtn := iter.Opts[iter.Pos]
iter.Pos++
return rtn
}
func (iter *OptsIter) Current() string {
if iter.Pos == 0 {
return ""
}
return iter.Opts[iter.Pos-1]
}
func (iter *OptsIter) Rest() []string {
return iter.Opts[iter.Pos:]
}