Skip to content

Commit

Permalink
Merge pull request #654 from rsteube/flag-tests
Browse files Browse the repository at this point in the history
example: added flag tests
  • Loading branch information
rsteube authored Dec 26, 2022
2 parents 76882ac + 28ca460 commit b14f0f5
Show file tree
Hide file tree
Showing 3 changed files with 80 additions and 3 deletions.
6 changes: 3 additions & 3 deletions example/cmd/flag.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ package cmd

import (
"net"
"os"
"time"

"github.com/rsteube/carapace"
Expand Down Expand Up @@ -55,8 +54,9 @@ func init() {
flagCmd.Flags().Uint("Uint", 0, "Uint")
flagCmd.Flags().UintSlice("UintSlice", []uint{}, "UintSlice")

flagCmd.Flags().Bool("optarg", false, "test optarg variant (must be second arg on command line to work)") // TODO quick&dirty toggle for now
carapace.Gen(rootCmd).PreRun(func(cmd *cobra.Command, args []string) {
if _, exists := os.LookupEnv("OPTARG"); !exists {
if len(args) < 2 || args[1] != "--optarg" {
return
}

Expand Down Expand Up @@ -105,7 +105,7 @@ func init() {
"BytesHex": carapace.ActionValues("01", "02", "03"),
"Count": carapace.ActionValues(),
"Duration": carapace.ActionValues("1h", "2m", "3s"),
"DurationSlice": carapace.ActionValues("1", "2", "3"),
"DurationSlice": carapace.ActionValues("1h", "2m", "3s"),
"Float32P": carapace.ActionValues("1", "2", "3"),
"Float32Slice": carapace.ActionValues("1", "2", "3"),
"Float64P": carapace.ActionValues("1", "2", "3"),
Expand Down
72 changes: 72 additions & 0 deletions example/main_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,78 @@ var tests = map[string]string{
`example multiparts VALUE=`: "one",
`example multiparts VALUE=one,`: "DIRECTORY",
`example multiparts VALUE=one,DIRECTORY=`: "/",

// `example flag --Bool `: "",
// `example flag --BoolSlice `: "",
`example flag --BytesBase64 `: "M",
`example flag --BytesHex `: "0",
// `example flag --Count `: "",
`example flag --Duration `: "1h",
`example flag --DurationSlice `: "1h",
`example flag --Float32P `: "1",
`example flag --Float32Slice `: "1",
`example flag --Float64P `: "1",
`example flag --Float64Slice `: "1",
`example flag --Int16 `: "1",
`example flag --Int32 `: "1",
`example flag --Int32Slice `: "1",
`example flag --Int64 `: "1",
`example flag --Int64Slice `: "1",
`example flag --Int8 `: "1",
`example flag --Int `: "1",
`example flag --IntSlice `: "1",
`example flag --IPMask `: "0",
`example flag --IP `: "0",
`example flag --IPNet `: "0",
`example flag --IPSlice `: "0",
`example flag --StringArray `: "1",
`example flag --String `: "1",
`example flag --StringSlice `: "1",
`example flag --StringToInt64 `: "a",
`example flag --StringToInt `: "a",
`example flag --StringToString `: "a",
`example flag --Uint16 `: "1",
`example flag --Uint32 `: "1",
`example flag --Uint64 `: "1",
`example flag --Uint8 `: "1",
`example flag --Uint `: "1",
`example flag --UintSlice `: "1",

// `example flag --optarg --Bool=`: "",
// `example flag --optarg --BoolSlice=`: "",
`example flag --optarg --BytesBase64=`: "M",
`example flag --optarg --BytesHex=`: "0",
// `example flag --optarg --Count=`: "",
`example flag --optarg --Duration=`: "1h",
`example flag --optarg --DurationSlice=`: "1h",
`example flag --optarg --Float32P=`: "1",
`example flag --optarg --Float32Slice=`: "1",
`example flag --optarg --Float64P=`: "1",
`example flag --optarg --Float64Slice=`: "1",
`example flag --optarg --Int16=`: "1",
`example flag --optarg --Int32=`: "1",
`example flag --optarg --Int32Slice=`: "1",
`example flag --optarg --Int64=`: "1",
`example flag --optarg --Int64Slice=`: "1",
`example flag --optarg --Int8=`: "1",
`example flag --optarg --Int=`: "1",
`example flag --optarg --IntSlice=`: "1",
`example flag --optarg --IPMask=`: "0",
`example flag --optarg --IP=`: "0",
`example flag --optarg --IPNet=`: "0",
`example flag --optarg --IPSlice=`: "0",
`example flag --optarg --StringArray=`: "1",
`example flag --optarg --String=`: "1",
`example flag --optarg --StringSlice=`: "1",
`example flag --optarg --StringToInt64=`: "a",
`example flag --optarg --StringToInt=`: "a",
`example flag --optarg --StringToString=`: "a",
`example flag --optarg --Uint16=`: "1",
`example flag --optarg --Uint32=`: "1",
`example flag --optarg --Uint64=`: "1",
`example flag --optarg --Uint8=`: "1",
`example flag --optarg --Uint=`: "1",
`example flag --optarg --UintSlice=`: "1",
}

var testsIntegratedMessage = map[string]string{
Expand Down
5 changes: 5 additions & 0 deletions internal/common/traverse.go
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,11 @@ func filterError(args []string, err error) error {
return nil
}

if re := regexp.MustCompile(`invalid argument ".*" for "(?P<flag>.*)" flag:.*`); re.MatchString(msg) && strings.SplitN(current, "=", 2)[0] == re.FindStringSubmatch(msg)[1] {
// ignore invalid argument for flag currently being completed (e.g. empty IntSlice)
return nil
}

if len(args) > 1 {
previous := args[len(args)-2]
if strings.HasPrefix(previous, "--") && msg == fmt.Sprintf("flag needs an argument: %v", previous) {
Expand Down

0 comments on commit b14f0f5

Please sign in to comment.