give a useful error for "garble build -tiny"

We've had confused users a handful of times by now.
And it's reasonable to expect flags to be after the command,
as that's how flags work for cmd/go itself.

I don't think we want to mix our flags with Go's,
or start accepting flags in either place.
Both seem like worse solutions long-term, as they can add confusion.

However, we can quickly give a useful hint when a flag is misplaced.
That should get new users unblocked without asking for help.

We use a regular expression for this purpose,
because it doesn't seem like a FlagSet supports what we need;
to detect whether an argument is one of our flags,
without actually applying its value to the flagset.
Our flagset would also error on Go's flags, which we don't want.
pull/438/head
Daniel Martí 3 years ago committed by Andrew LeFevre
parent 5abd3c468d
commit 4e97811a62

@ -65,6 +65,8 @@ func init() {
flagSet.Var(&flagSeed, "seed", "Provide a base64-encoded seed, e.g. -seed=o9WDTZ4CN4w\nFor a random seed, provide -seed=random")
}
var rxGarbleFlag = regexp.MustCompile(`-(literals|tiny|debug|debugdir|seed)($|=)`)
type seedFlag struct {
random bool
bytes []byte
@ -416,6 +418,11 @@ This command wraps "go %s". Below is its help:
%s`[1:], command, command, out)
return nil, errJustExit(2)
}
for _, flag := range flags {
if rxGarbleFlag.MatchString(flag) {
return nil, fmt.Errorf("garble flags must precede command, like: garble %s build ./pkg", flag)
}
}
// Here is the only place we initialize the cache.
// The sub-processes will parse it from a shared gob file.

@ -61,6 +61,18 @@ stderr 'unknown command'
stderr 'usage: go build' # TODO: is this confusing?
! stdout .
! garble build -tiny
stderr 'must precede command, like: garble -tiny build \./pkg'
! stdout .
! garble build -literals
stderr 'must precede command, like: garble -literals build \./pkg'
! stdout .
! garble build -seed=random
stderr 'must precede command, like: garble -seed=random build \./pkg'
! stdout .
[!windows] ! garble /does/not/exist/compile
[windows] ! garble C:\does\not\exist\compile
stderr 'not running "garble \[command\]"'

Loading…
Cancel
Save