forked from toolshed/abra
refactor: urfave v3
This commit is contained in:
cli
app
app.gobackup.gocheck.gocmd.goconfig.gocp.godeploy.golist.gologs.gonew.gops.goremove.gorestart.gorestore.gorollback.gorun.gosecret.goservices.goundeploy.goupgrade.govolume.go
catalogue
cli.gointernal
recipe
server
updater
pkg/autocomplete
scripts/autocomplete
vendor
github.com
Azure
go-ansiterm
ProtonMail
go-crypto
openpgp
beorn7
perks
quantile
cenkalti
backoff
containers
image
docker
reference
pkg
types
cpuguy83
go-md2man
v2
davecgh
go-spew
docker
emirpasic
gods
felixge
ghodss
go-git
gcfg
go-billy
go-git
v5
plumbing
format
config
gitignore
idxfile
index
packfile
object
protocol
packp
utils
worktree_bsd.goworktree_js.goworktree_linux.goworktree_unix_other.goworktree_windows.gogogo
protobuf
google
go-cmp
shlex
uuid
hashicorp
go-cleanhttp
kballard
go-shellquote
kevinburke
ssh_config
lucasb-eyer
go-colorful
mattn
go-isatty
mgutz
ansi
miekg
pkcs11
opencontainers
pkg
pmezard
go-difflib
difflib
russross
blackfriday
sergi
sirupsen
logrus
spf13
theupdateframework
notary
urfave
cli
.flake8.gitignoreCODE_OF_CONDUCT.mdLICENSEREADME.mdapp.gocategory.gocli.gocommand.gocontext.godocs.goerrors.gofish.goflag.goflag_bool.goflag_bool_t.goflag_duration.goflag_float64.goflag_generic.goflag_int.goflag_int64.goflag_int64_slice.goflag_int_slice.goflag_string.goflag_string_slice.goflag_uint.goflag_uint64.gofuncs.gohelp.goparse.gotemplate.go
v2
.flake8README.mdapp.goargs.gocli.gocommand.gocontext.godocs.goflag-spec.yamlflag_bool.goflag_duration.goflag_float64.goflag_float64_slice.goflag_generic.goflag_int.goflag_int64.goflag_int64_slice.goflag_int_slice.goflag_path.goflag_string.goflag_string_slice.goflag_timestamp.goflag_uint.goflag_uint64.goflag_uint64_slice.goflag_uint_slice.gogodoc-current.txthelp.gomkdocs-reqs.txtsliceflag.gosort.gosuggestions.gozz_generated.flags.go
v3
.gitignore.golangci.yamlCODE_OF_CONDUCT.mdLICENSEMakefileREADME.mdargs.go
autocomplete
category.gocli.gocommand.gocompletion.goerrors.gofish.goflag.goflag_bool.goflag_bool_with_inverse.goflag_duration.goflag_ext.goflag_float.goflag_float_slice.goflag_impl.goflag_int.goflag_int_slice.goflag_map_impl.goflag_mutex.goflag_slice_base.goflag_string.goflag_string_map.goflag_string_slice.goflag_timestamp.goflag_uint.goflag_uint_slice.gofuncs.gogodoc-current.txthelp.gomkdocs-reqs.txtmkdocs.ymlparse.gosort.gostaticcheck.confsuggestions.gotemplate.govalue_source.goxeipuuv
gojsonschema
xrash
go.opentelemetry.io
proto
otlp
golang.org
x
google.golang.org
protobuf
internal
impl
gopkg.in
warnings.v0
yaml.v2
yaml.v3
gotest.tools
v3
internal
difflib
118
vendor/github.com/urfave/cli/v3/parse.go
generated
vendored
Normal file
118
vendor/github.com/urfave/cli/v3/parse.go
generated
vendored
Normal file
@ -0,0 +1,118 @@
|
||||
package cli
|
||||
|
||||
import (
|
||||
"flag"
|
||||
"strings"
|
||||
)
|
||||
|
||||
type iterativeParser interface {
|
||||
useShortOptionHandling() bool
|
||||
}
|
||||
|
||||
// To enable short-option handling (e.g., "-it" vs "-i -t") we have to
|
||||
// iteratively catch parsing errors. This way we achieve LR parsing without
|
||||
// transforming any arguments. Otherwise, there is no way we can discriminate
|
||||
// combined short options from common arguments that should be left untouched.
|
||||
// Pass `shellComplete` to continue parsing options on failure during shell
|
||||
// completion when, the user-supplied options may be incomplete.
|
||||
func parseIter(set *flag.FlagSet, ip iterativeParser, args []string, shellComplete bool) error {
|
||||
for {
|
||||
tracef("parsing args %[1]q with %[2]T (name=%[3]q)", args, set, set.Name())
|
||||
|
||||
err := set.Parse(args)
|
||||
if !ip.useShortOptionHandling() || err == nil {
|
||||
if shellComplete {
|
||||
tracef("returning nil due to shellComplete=true")
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
tracef("returning err %[1]q", err)
|
||||
|
||||
return err
|
||||
}
|
||||
|
||||
tracef("finding flag from error %[1]q", err)
|
||||
|
||||
trimmed, trimErr := flagFromError(err)
|
||||
if trimErr != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
tracef("regenerating the initial args with the split short opts")
|
||||
|
||||
argsWereSplit := false
|
||||
for i, arg := range args {
|
||||
tracef("skipping args that are not part of the error message (i=%[1]v arg=%[2]q)", i, arg)
|
||||
|
||||
if name := strings.TrimLeft(arg, "-"); name != trimmed {
|
||||
continue
|
||||
}
|
||||
|
||||
tracef("trying to split short option (arg=%[1]q)", arg)
|
||||
|
||||
shortOpts := splitShortOptions(set, arg)
|
||||
if len(shortOpts) == 1 {
|
||||
return err
|
||||
}
|
||||
|
||||
tracef(
|
||||
"swapping current argument with the split version (shortOpts=%[1]q args=%[2]q)",
|
||||
shortOpts, args,
|
||||
)
|
||||
|
||||
// do not include args that parsed correctly so far as it would
|
||||
// trigger Value.Set() on those args and would result in
|
||||
// duplicates for slice type flags
|
||||
args = append(shortOpts, args[i+1:]...)
|
||||
argsWereSplit = true
|
||||
break
|
||||
}
|
||||
|
||||
tracef("this should be an impossible to reach code path")
|
||||
// but in case the arg splitting failed to happen, this
|
||||
// will prevent infinite loops
|
||||
if !argsWereSplit {
|
||||
return err
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
const providedButNotDefinedErrMsg = "flag provided but not defined: -"
|
||||
|
||||
// flagFromError tries to parse a provided flag from an error message. If the
|
||||
// parsing fails, it returns the input error and an empty string
|
||||
func flagFromError(err error) (string, error) {
|
||||
errStr := err.Error()
|
||||
trimmed := strings.TrimPrefix(errStr, providedButNotDefinedErrMsg)
|
||||
if errStr == trimmed {
|
||||
return "", err
|
||||
}
|
||||
return trimmed, nil
|
||||
}
|
||||
|
||||
func splitShortOptions(set *flag.FlagSet, arg string) []string {
|
||||
shortFlagsExist := func(s string) bool {
|
||||
for _, c := range s[1:] {
|
||||
if f := set.Lookup(string(c)); f == nil {
|
||||
return false
|
||||
}
|
||||
}
|
||||
return true
|
||||
}
|
||||
|
||||
if !isSplittable(arg) || !shortFlagsExist(arg) {
|
||||
return []string{arg}
|
||||
}
|
||||
|
||||
separated := make([]string, 0, len(arg)-1)
|
||||
for _, flagChar := range arg[1:] {
|
||||
separated = append(separated, "-"+string(flagChar))
|
||||
}
|
||||
|
||||
return separated
|
||||
}
|
||||
|
||||
func isSplittable(flagArg string) bool {
|
||||
return strings.HasPrefix(flagArg, "-") && !strings.HasPrefix(flagArg, "--") && len(flagArg) > 2
|
||||
}
|
Reference in New Issue
Block a user