High-performance browser automation bridge and multi-instance orchestrator with advanced stealth injection and real-time dashboard.
test(cli): negative-positional persistent-flag rows are order-dependent (PIN-427)
Luigi Agosti8f1dac0b5a2f
diff --git a/cmd/pinchtab/negative_positionals.go b/cmd/pinchtab/negative_positionals.goindex 67d7b559..f045ba55 100644--- a/cmd/pinchtab/negative_positionals.go+++ b/cmd/pinchtab/negative_positionals.go@@ -112,12 +112,7 @@ func consumesNextToken(cmd *cobra.Command, arg string) bool {if attached {return false}- // PersistentFlags too: a value-flag written BEFORE the subcommand (the remote- // form `pinchtab --server <url> set geo …`) is classified on the root, whose- // persistent flags (--server, --agent-id) are not yet merged into Flags() at- // this pre-Execute stage, so without this the URL reads as a positional and the- // subcommand path is lost.- return takesValue(cmd.Flags().Lookup(name), cmd.InheritedFlags().Lookup(name), cmd.PersistentFlags().Lookup(name))+ return takesValue(findFlag(cmd, func(set *pflag.FlagSet) *pflag.Flag { return set.Lookup(name) }))}// A shorthand bundle: pflag walks the characters and gives the REST of the token to@@ -131,7 +126,7 @@ func consumesNextToken(cmd *cobra.Command, arg string) bool {// non-ASCII byte is not a flag this walk can reason about.return false}- if !takesValue(cmd.Flags().ShorthandLookup(c), cmd.InheritedFlags().ShorthandLookup(c), cmd.PersistentFlags().ShorthandLookup(c)) {+ if !takesValue(findFlag(cmd, func(set *pflag.FlagSet) *pflag.Flag { return set.ShorthandLookup(c) })) {continue}return i == len(shorthands)-1@@ -139,12 +134,22 @@ func consumesNextToken(cmd *cobra.Command, arg string) bool {return false}-func takesValue(candidates ...*pflag.Flag) bool {- for _, flag := range candidates {- if flag != nil {- return flag.NoOptDefVal == ""+func findFlag(cmd *cobra.Command, lookup func(*pflag.FlagSet) *pflag.Flag) *pflag.Flag {+ if flag := lookup(cmd.Flags()); flag != nil {+ return flag+ }+ for owner := cmd; owner != nil; owner = owner.Parent() {+ if flag := lookup(owner.PersistentFlags()); flag != nil {+ return flag}}+ return nil+}++func takesValue(flag *pflag.Flag) bool {+ if flag != nil {+ return flag.NoOptDefVal == ""+ }// An unknown flag is about to be refused by cobra whatever we do here; assuming it// takes no value keeps the following token a positional, so the refusal names the// flag rather than an argument count.diff --git a/cmd/pinchtab/negative_positionals_test.go b/cmd/pinchtab/negative_positionals_test.goindex 51bfbf46..2ff38639 100644--- a/cmd/pinchtab/negative_positionals_test.go+++ b/cmd/pinchtab/negative_positionals_test.go@@ -52,11 +52,6 @@ func TestNegativeArgumentsAreMovedBehindTheFlags(t *testing.T) {want: []string{"set", "geo", "--json", "--", "-33.8", "151.2"},},{- // PIN-419: a value-taking PERSISTENT flag (--server) before the subcommand.- // It is classified on the root, whose persistent flags are not yet merged- // into Flags() at rewrite time, so on HEAD --server does not consume the URL:- // the URL becomes a positional, the set-geo path is lost, and cobra reports- // the URL as an "unknown command".name: "a persistent --server before the subcommand keeps its value and the path",args: []string{"--server", "http://127.0.0.1:19227", "set", "geo", "51.5", "-0.12"},want: []string{"set", "geo", "--server", "http://127.0.0.1:19227", "--", "51.5", "-0.12"},@@ -76,6 +71,51 @@ func TestNegativeArgumentsAreMovedBehindTheFlags(t *testing.T) {}}+func TestPersistentFlagValuesStayWithTheirFlagOnAnUnmergedTree(t *testing.T) {+ for _, tc := range []struct {+ name string+ args []string+ want []string+ }{+ {+ name: "a persistent --server before the subcommand",+ args: []string{"--server", "http://127.0.0.1:19227", "move", "-5", "-5"},+ want: []string{"move", "--server", "http://127.0.0.1:19227", "--", "-5", "-5"},+ },+ {+ name: "a persistent --agent-id before the subcommand",+ args: []string{"--agent-id", "agent-7", "move", "-5", "-5"},+ want: []string{"move", "--agent-id", "agent-7", "--", "-5", "-5"},+ },+ {+ name: "a persistent shorthand before the subcommand",+ args: []string{"-p", "work", "move", "-5"},+ want: []string{"move", "-p", "work", "--", "-5"},+ },+ {+ name: "a persistent flag after the subcommand resolves through the ancestor",+ args: []string{"move", "-5", "--server", "http://127.0.0.1:19227"},+ want: []string{"move", "--server", "http://127.0.0.1:19227", "--", "-5"},+ },+ } {+ t.Run(tc.name, func(t *testing.T) {+ got := rewriteNegativeNumberArgs(unmergedPersistentFlagTree(), tc.args)+ if strings.Join(got, " ") != strings.Join(tc.want, " ") {+ t.Errorf("rewrite = %v, want %v", got, tc.want)+ }+ })+ }+}++func unmergedPersistentFlagTree() *cobra.Command {+ root := &cobra.Command{Use: "root"}+ root.PersistentFlags().String("server", "", "")+ root.PersistentFlags().String("agent-id", "", "")+ root.PersistentFlags().StringP("profile", "p", "", "")+ root.AddCommand(&cobra.Command{Use: "move", Run: func(*cobra.Command, []string) {}})+ return root+}+// The safety property that makes a rewrite this broad acceptable: an invocation with no// negative-number positional comes back byte-identical, so nothing else in the CLI can// change shape because of this.