Skip to content
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

Standardize boolean parameters #59

Open
chris-peterson opened this issue Jan 17, 2023 · 4 comments
Open

Standardize boolean parameters #59

chris-peterson opened this issue Jan 17, 2023 · 4 comments
Milestone

Comments

@chris-peterson
Copy link
Owner

chris-peterson commented Jan 17, 2023

[bool] has some annoying behaviors, e.g. you can't pass bare strings true or false

Using this pattern makes consumption easier

        [Parameter()]
        [ValidateSet($null, 'true', 'false')]
        [object]
@chris-peterson chris-peterson added this to the 2.0 milestone Jan 17, 2023
@gaelcolas
Copy link

I think that should be a nullable bool tbh.

[nullable[bool]]

You shouldn't pass strings to that...

@chris-peterson
Copy link
Owner Author

The [ValidateSet] + string approach is certainly not ideal.

But if you use [nullable[bool]], you can't provide a string literal, e.g.

<CmdLet> -BooleanProperty false

results in ⬇️

Cannot convert value "System.String" to type "System.Nullable`1[System.Boolean]".
Boolean parameters accept only Boolean values and numbers, such as $True, $False, 1 or 0.

Seems like an oversight in Powershell; if you're going to say integers are valid, why not (unambiguous) strings?

@gaelcolas
Copy link

But that's expected, because we want a [bool] (we just have to add a $).
Why using 'false'? Is it purely for "shell" usage?

@chris-peterson
Copy link
Owner Author

indeed -- I like supporting string literals as often the cmdlets are invoked from CI jobs. in some cases, the fact that the underlying code is powershell is completely hidden

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants