-
Notifications
You must be signed in to change notification settings - Fork 257
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
How to get variadic arguments #340
Comments
Variadics aren't currently supported.... But in theory they could be. Right now you'd have to do something like
|
Thank you for your reply. I hope variadic to be supported. |
@mondy I don't think variadics play nice with specifying multiple build targets. For example, if your mage file had I feel like this ambiguity in behavior would cause a lot of confusion. |
Still, the feature is quite useful. Consider wrapping some docker/compose call: func (Docker) Compose(args []string) error {
compose := sh.RunCmd("docker", "compose")
return compose(args...)
} it could be an option to make unknown args/targets available over context, for instance: func (Docker) Compose(ctx context.Context) error {
compose := sh.RunCmd("docker", "compose")
argsArr := mg.CtxArgs(ctx)
return compose(argsArr...)
} |
Just randomly browsing issues and wanted to throw an idea in on this. What you could do is allow variadic targets to be separated with with So, for example, given targets func Run(arguments ...string) error {/**/}
func Stuff() error {/**/} The behavior would be as follows:
|
IMHO, that is why variadic arguments typically have to be the last argument in a function. We can translate this that a target with variadic arguments can only be called if it is the last target. Or combine with the suggestion from @flowchartsman and add separators that are only needed when needing to fix ambiguity. |
I think it would be nice if Mage offered a flag for key/value pairs, like this:
The key/value pairs could be accessed via an implicit variable or a function, like this:
Or this:
The -p flag, or something similar, is used by many build tools. |
I saw #402 adds variadic support, although the documentation seems missing. I'll try the feature for our use case. |
Sorry about the missing documentation. Could you point me to it so I can amend it? |
Okay. It seems I jumped the gun in my enthousiasm. :) It adds the support to mg.F, but it seems that doesn't automatically translate in variadic support for targets. Sorry for claiming missing documentation for a feature that is not there! //go:build mage
package main
// "github.com/magefile/mage/sh"
// Runs go mod download and then installs the binary.
func Build(foo ...string) error {
for i := 0; i < len(foo); i++ {
println(foo[i])
}
return nil
} Running
|
Would this idea address support for optional arguments too? |
I read the Arguments section of the https://magefile.org/targets/.
I wanted to do something like the following, but I couldn't.
When executed, the output will be as follows
Is there a way to get variadic arguments?
The text was updated successfully, but these errors were encountered: