-
-
Notifications
You must be signed in to change notification settings - Fork 11
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- support "attached" value flags like `-v=arg` and `-varg` - use shorthand for nospace in shorthand series Some logs are wrong and there are likely some edge cases failing. But tests are fine and works well so far so issues will be fixed in subsequent PRs.
- Loading branch information
Showing
9 changed files
with
314 additions
and
99 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
package cmd | ||
|
||
import ( | ||
"github.com/rsteube/carapace" | ||
"github.com/spf13/cobra" | ||
) | ||
|
||
var chainCmd = &cobra.Command{ | ||
Use: "chain", | ||
Short: "shorthand chain", | ||
Run: func(cmd *cobra.Command, args []string) {}, | ||
DisableFlagParsing: true, | ||
} | ||
|
||
func init() { | ||
carapace.Gen(chainCmd).Standalone() | ||
|
||
rootCmd.AddCommand(chainCmd) | ||
|
||
carapace.Gen(chainCmd).PositionalAnyCompletion( | ||
carapace.ActionCallback(func(c carapace.Context) carapace.Action { | ||
cmd := &cobra.Command{} | ||
carapace.Gen(cmd).Standalone() | ||
|
||
cmd.Flags().CountP("count", "c", "") | ||
cmd.Flags().BoolP("bool", "b", false, "") | ||
cmd.Flags().StringP("value", "v", "", "") | ||
cmd.Flags().StringP("optarg", "o", "", "") | ||
|
||
cmd.Flag("optarg").NoOptDefVal = " " | ||
|
||
carapace.Gen(cmd).FlagCompletion(carapace.ActionMap{ | ||
"value": carapace.ActionValues("val1", "val2"), | ||
"optarg": carapace.ActionValues("opt1", "opt2"), | ||
}) | ||
|
||
carapace.Gen(cmd).PositionalCompletion( | ||
carapace.ActionValues("p1", "positional1"), | ||
) | ||
|
||
return carapace.ActionExecute(cmd) | ||
}), | ||
) | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,80 @@ | ||
package cmd | ||
|
||
import ( | ||
"testing" | ||
|
||
"github.com/rsteube/carapace" | ||
"github.com/rsteube/carapace/pkg/sandbox" | ||
"github.com/rsteube/carapace/pkg/style" | ||
) | ||
|
||
func TestShorthandChain(t *testing.T) { | ||
sandbox.Package(t, "github.com/rsteube/carapace/example")(func(s *sandbox.Sandbox) { | ||
s.Run("chain", "-b"). | ||
Expect(carapace.ActionStyledValues( | ||
"c", style.Default, | ||
"o", style.Yellow, | ||
"v", style.Blue, | ||
).Prefix("-b"). | ||
NoSpace('c', 'o'). | ||
Tag("flags")) | ||
|
||
s.Run("chain", "-bc"). | ||
Expect(carapace.ActionStyledValues( | ||
"c", style.Default, | ||
"o", style.Yellow, | ||
"v", style.Blue, | ||
).Prefix("-bc"). | ||
NoSpace('c', 'o'). | ||
Tag("flags")) | ||
|
||
s.Run("chain", "-bcc"). | ||
Expect(carapace.ActionStyledValues( | ||
"c", style.Default, | ||
"o", style.Yellow, | ||
"v", style.Blue, | ||
).Prefix("-bcc"). | ||
NoSpace('c', 'o'). | ||
Tag("flags")) | ||
|
||
s.Run("chain", "-bcco"). | ||
Expect(carapace.ActionStyledValues( | ||
"c", style.Default, | ||
"v", style.Blue, | ||
).Prefix("-bcco"). | ||
NoSpace('c'). | ||
Tag("flags")) | ||
|
||
s.Run("chain", "-bcco", ""). | ||
Expect(carapace.ActionValues( | ||
"p1", | ||
"positional1", | ||
)) | ||
|
||
s.Run("chain", "-bcco="). | ||
Expect(carapace.ActionValues( | ||
"opt1", | ||
"opt2", | ||
).Prefix("-bcco=")) | ||
|
||
s.Run("chain", "-bccv", ""). | ||
Expect(carapace.ActionValues( | ||
"val1", | ||
"val2", | ||
)) | ||
|
||
s.Run("chain", "-bccv="). | ||
Expect(carapace.ActionValues( | ||
"val1", | ||
"val2", | ||
).Prefix("-bccv=")) | ||
|
||
s.Run("chain", "-bccv", "val1", "-c"). | ||
Expect(carapace.ActionStyledValues( | ||
"c", style.Default, | ||
"o", style.Yellow, | ||
).Prefix("-c"). | ||
NoSpace('c', 'o'). | ||
Tag("flags")) | ||
}) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
package pflagfork | ||
|
||
import ( | ||
"reflect" | ||
"testing" | ||
|
||
"github.com/spf13/pflag" | ||
) | ||
|
||
func TestLookupPosixShorthandArg(t *testing.T) { | ||
_test := func(arg, name, prefix string, args ...string) { | ||
t.Run(arg, func(t *testing.T) { | ||
if args == nil { | ||
args = []string{} | ||
} | ||
|
||
fs := &FlagSet{pflag.NewFlagSet("test", pflag.PanicOnError)} | ||
|
||
fs.BoolP("bool", "b", false, "") | ||
fs.CountP("count", "c", "") | ||
fs.StringP("string", "s", "", "") | ||
|
||
f := fs.lookupPosixShorthandArg(arg) | ||
if f == nil || f.Name != name { | ||
t.Fatalf("should be " + name) | ||
} | ||
|
||
if f.Prefix != prefix { | ||
t.Fatalf("prefix doesnt match actual: %#v, expected: %#v", f.Prefix, prefix) | ||
} | ||
|
||
if !reflect.DeepEqual(f.Args, args) { | ||
t.Fatalf("args dont match %v: actual: %#v expected: %#v", arg, f.Args, args) | ||
} | ||
|
||
}) | ||
} | ||
|
||
_test("-b=", "bool", "-b=", "") | ||
_test("-b=t", "bool", "-b=", "t") | ||
_test("-b=true", "bool", "-b=", "true") | ||
_test("-ccb", "bool", "-ccb") | ||
_test("-ccb=", "bool", "-ccb=", "") | ||
_test("-ccb=t", "bool", "-ccb=", "t") | ||
_test("-ccb=true", "bool", "-ccb=", "true") | ||
_test("-ccbs=val1", "string", "-ccbs=", "val1") | ||
_test("-ccbsval1", "string", "-ccbs", "val1") | ||
} |
Oops, something went wrong.