carlos / platform Public

cli: a flag's value "--" does not end the label flags (M2 review P2)

interleavedArgs scans the words by position: a "--" where a flag would
be ends the flags, while a value flag's next word is its value, "--"
included. fs then parses the flags and values it collected.
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
claude agent, operated by paul@keymail.dev pushed by paul@keymail.dev 68d8aac67438517ce0e3f0e5298fcdfb3b345edd parent 2b146b6
2 files changed, +81 −13
  • instances_cli.go +39 −13
  • instances_cli_label_test.go +42 −0
diff --git a/instances_cli.go b/instances_cli.go
index 728db7ee4..8565f590d 100644
--- a/instances_cli.go
+++ b/instances_cli.go
@@ -1631,20 +1631,46 @@ func runInstancesLabel(args []string) {
}
// interleavedArgs parses fs over args, reading flags on both sides of each
-// positional word, and returns the positional words in order. After "--"
-// every word is positional.
+// positional word, and returns the positional words in order. A "--" where
+// a flag would be ends the flags: every word after it is positional. The
+// terminator is found by the word's position as the scan reaches it, so a
+// value flag's own value "--" (--account --) is that flag's value (M2
+// review P2). The flags and their values are then parsed by fs, which
+// reports any error as before.
func interleavedArgs(fs *flag.FlagSet, args []string) []string {
- var words []string
- for {
- fs.Parse(args)
- rest := fs.Args()
- if len(rest) == 0 {
- return words
- }
- if consumed := len(args) - len(rest); consumed > 0 && args[consumed-1] == "--" {
- return append(words, rest...)
+ var flags, words []string
+ for i := 0; i < len(args); i++ {
+ a := args[i]
+ switch {
+ case a == "--":
+ return parseInterleaved(fs, flags, append(words, args[i+1:]...))
+ case len(a) > 1 && a[0] == '-':
+ flags = append(flags, a)
+ name := strings.TrimPrefix(strings.TrimPrefix(a, "-"), "-")
+ if strings.Contains(name, "=") {
+ continue
+ }
+ if f := fs.Lookup(name); f != nil && !isBoolFlag(f) && i+1 < len(args) {
+ i++
+ flags = append(flags, args[i])
+ }
+ default:
+ words = append(words, a)
}
- words = append(words, rest[0])
- args = rest[1:]
}
+ return parseInterleaved(fs, flags, words)
+}
+
+// parseInterleaved parses the flag words interleavedArgs collected and
+// answers the positional words.
+func parseInterleaved(fs *flag.FlagSet, flags, words []string) []string {
+ fs.Parse(flags)
+ return words
+}
+
+// isBoolFlag reports whether f takes no separate value, as package flag
+// decides it.
+func isBoolFlag(f *flag.Flag) bool {
+ b, ok := f.Value.(interface{ IsBoolFlag() bool })
+ return ok && b.IsBoolFlag()
}
diff --git a/instances_cli_label_test.go b/instances_cli_label_test.go
index 10a691bfd..7d0068f6b 100644
--- a/instances_cli_label_test.go
+++ b/instances_cli_label_test.go
@@ -12,12 +12,14 @@ import (
"context"
"encoding/json"
"errors"
+ "flag"
"fmt"
"io"
"net/http"
"net/http/httptest"
"os/exec"
"path/filepath"
+ "slices"
"strings"
"sync"
"testing"
@@ -511,3 +513,43 @@ func TestInstancesLabelNoEvidenceSaysItOnce(t *testing.T) {
}
}
}
+
+// M2 review P2 (Astra, Fable P3-5): a string flag's value "--" is its
+// value, not the end of the flags; a "--" in a flag's place is.
+func TestInstancesLabelDashDashValue(t *testing.T) {
+ parse := func(args ...string) (words []string, account, box string) {
+ fs := flag.NewFlagSet("t", flag.ContinueOnError)
+ acct := fs.String("account", "", "")
+ b := fs.String("box", "", "")
+ fs.Bool("dry-run", false, "")
+ words = interleavedArgs(fs, args)
+ return words, *acct, *b
+ }
+ for _, tc := range []struct {
+ args []string
+ words []string
+ account, box string
+ }{
+ {[]string{"--account", "--", "shop.example.com", "--box", "box-1"}, []string{"shop.example.com"}, "--", "box-1"},
+ {[]string{"--account=--", "shop.example.com", "--box", "box-1"}, []string{"shop.example.com"}, "--", "box-1"},
+ {[]string{"--account", "bab", "--", "shop.example.com", "--box"}, []string{"shop.example.com", "--box"}, "bab", ""},
+ {[]string{"--dry-run", "--", "--box"}, []string{"--box"}, "", ""},
+ {[]string{"shop.example.com", "--box", "box-1", "--", "x"}, []string{"shop.example.com", "x"}, "", "box-1"},
+ } {
+ words, account, box := parse(tc.args...)
+ if !slices.Equal(words, tc.words) || account != tc.account || box != tc.box {
+ t.Errorf("%q: words %q account %q box %q; want %q %q %q", tc.args, words, account, box, tc.words, tc.account, tc.box)
+ }
+ }
+
+ // Through the verb: the account "--" reaches the path, the host is the
+ // one word, and --box after it is still a flag.
+ stub := newLabelStub(t, http.StatusOK, labelAnswerJSON(t, map[string]any{"applied": true, "label": "box-1"}))
+ out, ok := runLabelCLI(t, stub.ts.URL, "--app", "shop", "--account-id", "bab", "--account", "--", "shop.example.com", "--box", "box-1")
+ if !ok || !strings.Contains(out, labelC17) {
+ t.Fatalf("want C17 and exit 0, got ok=%v:\n%s", ok, out)
+ }
+ if path, body, _ := stub.last(); path != "/api/cli/apps/bab/shop/instances/shop.example.com/label" || body["box"] != "box-1" {
+ t.Fatalf("request %s %v", path, body)
+ }
+}