Repository navigation
Description
Related problem
Suppose we have a command that wraps another command where we want to forward --push and --release boolean flags to the inner command called build-imp.
def build-imp [--push, --release] {
# This isn't a real example. Here we would actually do smth useful like building a docker image
# optionally in release mode and optionally pushing it to the remote registry
echo $"Doing a build with push: ($push) and release: ($release)"
}At the time of this writing, it isn't possible to do this naturally. The following doesn't work, because nu complains that there are excessive positional parameters $push and $release:
def build [--push, --release] {
build-imp --push $push --release $release
}Error message
Error: nu::parser::extra_positional (link)
× Extra positional argument.
╭─[entry #6:1:1]
1 │ def build [--push, --release] {
2 │ build-imp --push $push --release $release
· ──┬──
· ╰── extra positional argument
3 │ }
╰────
help: Usage: build-imp {flags}This also doesn't work, although it compiles. Any invocation of build (even with all flags unset) results in $push and $release arguments inside build-imp set to true.
def build [--push, --release] {
build-imp --push=$push --release=$release
}Demo of the variant higher
❯ build
Doing a build with push: true and release: true
❯ build --push
Doing a build with push: true and release: true
❯ build --push --release
Doing a build with push: true and release: true
❯ build --release
Doing a build with push: true and release: trueWorkarounds
Combinatorial explosion
Handle all possible combinations of all flags with an if-else chain. This solution preserves the desired command signature but requires a huge amount of boilerplate. This solution suffers from copy-paste exponentially (pow(2, number_of_flags)) with the total number of flags to forward. The situation is worsened if there are other arguments to pass other than the flags that will also need to be copy-pasted
def build [--push, --release] {
if $push and $release {
build-imp --push --release
} else if $push and not $release {
build-imp --push
} else if not $push and $release {
build-imp --release
} else {
build-imp
}
}A variation of the solution that uses a bit nicer grouping of combinations with bitflags
def build [--push, --release] {
let invocations = [
{ build-imp }
{ build-imp --push }
{ build-imp --release }
{ build-imp --release --push }
]
do ($invocations | get (($release | into int) bit-shl 1 bit-or ($push | into int)))
}Use non-boolean type for flags
UPD: there was found a better version of this workaround that allows you to use booleans, but doesn't enable a bare
--flagsyntax: #7260 (comment)
The downside of this approach is that we make the API uglier, although the boilerplate doesn't grow exponentially here. We can define the underlying command with int arguments, but also make a wrapper that provides the same nice boolean flag API and just converts its arguments to integers.
def build-imp-int [--push: int, --release: int] {
let $push = $push == 1
let $release = $release == 1
# ...
}
# If this function needs to be reused in a bunch of places where boolean flags API suffices,
# then we may define a simple wrapper around it that takes boolean flags and forwards
# them to the inner function with `int` flags, but that's also boilerplate, so not ideal:
def build-imp [--push, --release] {
build-imp-int --push ($push | into int) --release ($release | into int)
}Describe the solution you'd like
The easiest solution that may probably be implemented quickly is to allow using --flag=boolean_expression syntax for boolean flags. The equals sign (=) between the argument name and the boolean expression disambiguates that the argument following the boolean flag isn't a positional parameter.
Right now we can pass literally any expression using --flag=expression, but that results in the boolean $flag variable to be set to true unconditionally. Maybe this was the intended solution of this problem at all, but it doesn't work today. Maybe it's a bug then?
Describe alternatives you've considered
A more general solution to the problem may also encompass solving the problem of passing optional flags to external commands.
For example, today we can't do this:
let push = if condition { "--push" } else { null }
docker buildx build $push ...args...Because nutshell will complain that it can't convert null to string. If we were to use an empty string instead of null nushell would forward it to the external binary as a separate empty string positional parameter. So instead we have to do this today:
let args = [...args...]
let args = if condition { $args | prepend "--push" } else { $args }
docker buildx build $argsThis works because an array is spread to positional parameters when invoking an external binary from nushell, but it also looks like some kind of inconvenient dark magic.
A more general problem is omitting named parameters to commands. I think it deserves a special syntax like this:
docker buildx build (--push $push)? (--named-arg-with-value $value)?With the question mark operator appended nushell would omit the parameter if the value is false or null for example. This is quite a naive first-comes-to-mind solution. I bet there could be some limitations with this, so feel free to share better proposals.
Activity
melMass commentedon Nov 28, 2022
Do you have other use cases for flags that accepts booleans or is it just to solve passing them to external commands?
The alternatives part I believe this is doable with run-external, I haven't tried myself but I had the exact same case to solve a few weeks ago and someone mentioned run-external on discord recently I think it fits this usecase.
Veetaha commentedon Nov 28, 2022
I don't think
run-externalimproves the experience for external commands. How would one use it to pass the--pushflag based on the boolean value stored in$pushvariable? I don't see other approaches than specified in alternatives (i.e. make the array of args and extend it dynamically).melMass commentedon Nov 28, 2022
Maybe I was not clear but I meant that this shoud be doable in
run-externalObviously not ideal I agree
melMass commentedon Nov 28, 2022
Oh my bad, while first reading the issue I skipped exactly this part of your issue (where you suggest the same solution without
run-external)!Veetaha commentedon Nov 28, 2022
@melMass
Regarding the uses case for non-external commands. I have a development script that has commands:
"main docker build" [--push, --release]- build the app's docker image"main deploy" [--release]- deploy the app and automatically build it; it has to inherit the same--releaseflag asbuildcommandI need to forward the
releaseflag fromdeploycommand intobuildcommand, and now it looks quite clumsy due to the limitation described in this issue.theAkito commentedon Apr 18, 2023
Related to #7033 due to quote & the whole issue in its entirety.
This whole forced to String conversion problem is a huge super-topic. That it is really hard to dynamically generate command chains is one of its biggest effects.
amtoine commentedon Apr 18, 2023
@Veetaha
does the following solve your issue? 😋
which gives
cumber commentedon May 20, 2023
@amtoine Honestly I kind of expected something like
def build [--push]to be syntactic sugar fordef build [--push : bool = false], andbuild --pushto be syntactic sugar forbuild --push=true. It's really surprising thatbuild --push=falseneither passes$pushasfalsenor throws a syntax error!amtoine commentedon May 20, 2023
the
build --push=falsepart looks a bit odd 🤔however, the rest looks sensible to me
def foo [--bar] { if $bar { print "bar!" } else { print "no bar..." } }does give
which is expected, isn't it? 😋
melMass commentedon May 20, 2023
I think the OP wants to conditionally set flags for a wrapper. Hence the need for a value flag (--b:bool). If I understand right this is to wrap externals.
Although there is no equal in nu arg so you must do
--b trueAfaik there is no fix for all that yet
Veetaha commentedon Aug 25, 2023
The suggestion from @amtoine regarding the
def build-imp [--push: bool = false, --release: bool = false]slightly improves the situation, but then you can no longer call thebuild-impthis way:You can only call it this way now
amtoine commentedon Aug 25, 2023
this is very true @Veetaha 👍
i use this trick inside modules, when the
build-imp-like command wouldn't leak to the outside, which is fine when it does not impact the user API imo 😋but yeah for interactive usage, that might not be ideal 😕
crabdancing commentedon Sep 14, 2023
I ran into this too! Glad to see there's already an issue on it. This isn't a general solution, but it's what I came up with to soften the pain of this:
Unfortunately, it doesn't work on Nushell's custom commands
12 remaining items
crabdancing commentedon Nov 14, 2023
Is the reason this was done purely a matter of syntactic uniformity? It seems the community is leaning in the direction of special behavior for boolean flags, anyway. (Which could be later made uniform by having flags accept either
--my-flag my-valueor--my-flag=my-value.) The ecosystem is small enough still that a breaking change isn't that big a deal (At least, IMO!) and is worth setting a standard that will cause the least sadness. :Pflying-sheep commentedon Nov 14, 2023
That would be a great solution if we lived in a vacuum (i.e. it was a great solution for React when they were free to design JSX however they liked:
<Comp prop />is the same as<Comp prop={true} />, so you can pass a variable when deriving this prop value and have concise syntax when not.)But as I said above: It doesn’t help in the slightest with 3rd party commands. There I still have to do stuff like
theAkito commentedon Nov 14, 2023
Already pointed as well, that this is actually a bigger, essential issue, which crosses a lot of Nushell surface.
This specific super-issue is already tracked in #7033.
WindSoilder commentedon Nov 14, 2023
Thanks for all your comments! Let me try to summarize the path we're trying to move:
switch
For the given example:
It needs to support:
foo(print false),foo --xyz(print true),foo --xyz=false(print false),foo --xyz=true(print true), and we can only set it withtrueorfalse, something like this will result to error:foo --xyz=1It doesn't support:
foo --xyz false,foo --xyz true(For the reason please check the comment: #7260 (comment))flag which takes boolean value
For the given example:
I'd like to make it as normal as other flags which takes a value for consistency (I have the same institution to #7260 (comment)). Which is handled by #10456
theAkito commentedon Nov 14, 2023
The default will be set to
falsethough, right?WindSoilder commentedon Nov 14, 2023
Yup, if we call
foodirectly, the value of$xyzisfalse, I've updated the comment.cumber commentedon Nov 14, 2023
Thanks @WindSoilder, that sounds exactly right.
I assume you intended this, but just to be explicit: it should also support
foo --xyz=<EXPR>, where<EXPR>stands for any expression that could be used as a flag argument (provided it results in a Boolean value of course). Most usefully the expression could be a variable reference, e.g.foo --xyz=$abc.enable to pass switch values dynamically (#11057)
differentiating between `--x` and `--x: bool` (nushell#10456)
enable to pass switch values dynamically (nushell#11057)
enable to pass switch values dynamically (nushell#11057)
wthueb commentedon Jan 18, 2026
#11057 doesn't close this issue for external commands. You still need to do the if/else/match thing as of 0.109.1, unless I'm wildly missing something. This seems like a big deal and comes up for me a lot, usually with flags like
--verbose (-v)and wanting to forward them to external commands called. Most I've come across don't support the--flag=falsesyntax.flying-sheep commentedon Jan 18, 2026
Probably not possible. There are two conventions here; a)
--flag/--no-flagand b) only--flag, which behave differently:--flag/--no-flag--flag--flag--no-flagFootnotes
this is the issue: a command which has a
--flag/--no-flagpair as convention might default to--no-flag↩