-
Notifications
You must be signed in to change notification settings - Fork 248
[run] improve UX by not requiring '--' #1326
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Conversation
internal/boxcli/run.go
Outdated
// are 2 or fewer arguments, we also don't need to do anything because there | ||
// are no flags after a non-run non-flag arg. | ||
// IMPROVEMENT: technically users can pass a flag before the subcommand "run" | ||
if len(args) <= 2 || args[0] != "run" { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Can we move this clause up first, given that it is only applicable to run
? Something like:
if len(args) <= 2 || args[0] != "run" || slices.Contains(args, "--") {
return args
}
internal/boxcli/run.go
Outdated
} | ||
} | ||
|
||
return append(args[:i+1], append([]string{"--"}, args[i+1:]...)...) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think this logic make sense. Can we simplify a bit? Took me a while to understand what is going on. like:
for i < len(args) {
arg := args[i]
if !strings.HasPrefix(arg, "-") {
// finally found it!
return append(args[:i+1], append([]string{"--"}, args[i+1:]...)...)
}
if strings.Contains(arg, "=") {
i++
continue
}
var flag *pflag.Flag
if strings.HasPrefix(arg, "--") {
flag = runFlags.Lookup(strings.TrimLeft(arg, "-"))
} else {
flag = runFlags.ShorthandLookup(strings.TrimLeft(arg, "-"))
}
if flag != nil {
if flag.NoOptDefVal == "" {
i := i+2
} else {
i++
}
}
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yeah. I would also add a comment in english stating what the loop is doing.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Something like: "adds a --
after all flags that come before the command that will be run. Thus, it transforms something like run --flag1 val1 command --flag2 val2
into run --flag1 val1 -- command --flag2 val2
".
That would save a code reader quite a bit of time I think.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Approving to unblock!
internal/boxcli/run.go
Outdated
} | ||
} | ||
|
||
return append(args[:i+1], append([]string{"--"}, args[i+1:]...)...) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Something like: "adds a --
after all flags that come before the command that will be run. Thus, it transforms something like run --flag1 val1 command --flag2 val2
into run --flag1 val1 -- command --flag2 val2
".
That would save a code reader quite a bit of time I think.
// if the first argument is not "run", we don't need to do anything. If there | ||
// are 2 or fewer arguments, we also don't need to do anything because there | ||
// are no flags after a non-run non-flag arg. | ||
// IMPROVEMENT: technically users can pass a flag before the subcommand "run" |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
what happens if they do?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It will fail if you don't include --
and have non-devbox flags in there, e.g.
devbox -c path/to/conf run python --version
Will say --version
is not a valid flag.
This can be fixed, it just requires finding the run
command and ensuring everything before it is a flag and not a command.
Just FYI the comment re |
thanks for the clarification @jimmidyson! We chatted about that as well and I think @LucilleH has some ideas to make the action better. Not sure if it helps your particular use case, but if you do:
|
The latest updates on your projects. Learn more about Vercel for Git ↗︎
|
Summary
Inspired by @jimmidyson's awesome post I thought we could remove the need for
--
every time you want to run something with devbox that has flags.This change assumes that when a user runs
devbox run
the first non-flag arg after "run" is part of the command or the script name the user wants to run. This relies onrun
never having subcommands (which I think will be true forever). So we transform:devbox run python --version
into
devbox run -- python --version
If command already has
--
then we do nothing.This change is fully backward compatible.
@LucilleH I'm somewhat sure I fixed the issues from demo. PTAL
How was it tested?