Compare commits

..

5 Commits

Author SHA1 Message Date
Jeff Carr 8235b935a3 need these flags 2025-09-18 16:03:21 -05:00
Jeff Carr b72816f193 add function for autocomplete 2025-09-18 15:27:27 -05:00
Jeff Carr 8ecad2c6af go test ? 2025-08-29 10:39:34 -05:00
Jeff Carr 8b32c8d316 change namespace 2025-08-29 07:28:16 -05:00
Jeff Carr 6cc03a124c allows GO libraries to have common args 2025-08-29 07:09:32 -05:00
4 changed files with 32 additions and 11 deletions

View File

@ -1,6 +1,4 @@
all:
@echo
@echo
all: goimports vet
clean:
rm -f go.*
@ -10,6 +8,9 @@ redomod:
GO111MODULE= go mod init
GO111MODULE= go mod tidy
test:
go test
vet:
@GO111MODULE=off go vet
@echo this go binary package builds okay

8
go.mod
View File

@ -1,14 +1,14 @@
module go.wit.com/dev/alexflint/arg
go 1.24.1
require (
github.com/alexflint/go-scalar v1.2.0
github.com/stretchr/testify v1.11.1
github.com/stretchr/testify v1.7.0
)
require (
github.com/davecgh/go-spew v1.1.1 // indirect
github.com/pmezard/go-difflib v1.0.0 // indirect
gopkg.in/yaml.v3 v3.0.1 // indirect
gopkg.in/yaml.v3 v3.0.0 // indirect
)
go 1.18

11
go.sum
View File

@ -1,13 +1,16 @@
github.com/alexflint/go-scalar v1.2.0 h1:WR7JPKkeNpnYIOfHRa7ivM21aWAdHD0gEWHCx+WQBRw=
github.com/alexflint/go-scalar v1.2.0/go.mod h1:LoFvNMqS1CPrMVltza4LvnGKhaSpc3oyLEBUZVhhS2o=
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
github.com/stretchr/testify v1.2.2/go.mod h1:a8OnRcib4nhh0OaRAV+Yts87kKdq0PP7pXfy6kDkUVs=
github.com/stretchr/testify v1.11.1 h1:7s2iGBzp5EwR7/aIZr8ao5+dra3wiQyKjjFuvgVKu7U=
github.com/stretchr/testify v1.11.1/go.mod h1:wZwfW3scLgRK+23gO65QZefKpKQRnfz6sD981Nm4B6U=
github.com/stretchr/testify v1.7.0 h1:nwc3DEeHmmLAfoZucVR881uASk0Mfjw8xYJ99tb5CcY=
github.com/stretchr/testify v1.7.0/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405 h1:yhCVgyC4o1eVCa2tZl7eS0r+SDo693bJlVdllGtEeKM=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA=
gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
gopkg.in/yaml.v3 v3.0.0 h1:hjy8E9ON/egN1tAYqKb61G10WtihqetD4sz2H+8nIeA=
gopkg.in/yaml.v3 v3.0.0/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=

View File

@ -98,6 +98,7 @@ func Register(dest ...interface{}) {
// MustParse processes command line arguments and exits upon failure
func MustParse(dest ...interface{}) *Parser {
register = append(register, dest...)
return mustParse(Config{Exit: mustParseExit, Out: mustParseOut}, register...)
}
@ -123,8 +124,24 @@ func Parse(dest ...interface{}) error {
return p.Parse(flags())
}
// pass in a "pretend" os.Args. Used for bash autocomplete
func ParseFlags(flags []string, dest ...interface{}) (*Parser, error) {
p, err := NewParser(Config{}, dest...)
if err != nil {
return p, err
}
overrideFlags = append(overrideFlags, flags...)
err = p.Parse(flags)
return p, err
}
var overrideFlags []string
// flags gets all command line arguments other than the first (program name)
func flags() []string {
if len(overrideFlags) > 0 {
return overrideFlags
}
if len(os.Args) == 0 { // os.Args could be empty
return nil
}