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

Add an -Until script block parameter to Select-Object #21610

Open
MatejKafka opened this issue May 6, 2024 · 4 comments
Open

Add an -Until script block parameter to Select-Object #21610

MatejKafka opened this issue May 6, 2024 · 4 comments
Labels
Issue-Enhancement the issue is more of a feature request than a bug Needs-Triage The issue is new and needs to be triaged by a work group. WG-Cmdlets general cmdlet issues WG-NeedsReview Needs a review by the labeled Working Group

Comments

@MatejKafka
Copy link

MatejKafka commented May 6, 2024

Summary of the new feature / enhancement

As a user, I want to stop processing input after encountering a specific value in the pipeline.

A simple example:

$NumbersUnder50 = 0..1000 | select -Until {$_ -eq 50} # stop after reaching 50

A more realistic example:

# list GitHub releases until a specific version
# with -FollowRelLink, this may result in saving a lot of HTTP requests under the hood, since otherwise all releases are listed
Invoke-RestMethod -FollowRelLink "https://api.github.com/repos/PowerShell/PowerShell/releases" `
    | % {$_} | select -Until {$_.tag_name -eq "v7.2.0"}

Since StopUpstreamCommandsException , which is internally used by Select-Object to implement its early-exit behavior is internal, I don't believe there's currently any way to implement this without reflecting PowerShell internals. Using something like ... | % {if (...) {break}; $_} may seem like an attractive option, but as documented, it terminates much more (the whole runspace) than a user would naively expect.

A more general solution could be to make StopUpstreamCommandsException public, so that users can write their own cmdlets with support for early exit, which is one of the possible solutions for #3821. However, even if that gets implemented, I believe the -Until parameter could still be valuable for simpler use cases.

Proposed technical implementation details (optional)

Add an -Until script block parameter to Select-Object, probably in a new parameter set, run the passed script block for each pipeline item and throw StopUpstreamCommandsException when the script block returns a truthy value.

I can open a PR if the feature is accepted. (although I'd kinda appreciate if someone found time to review my other PRs first, nudge nudge wink wink :) )

@MatejKafka MatejKafka added Issue-Enhancement the issue is more of a feature request than a bug Needs-Triage The issue is new and needs to be triaged by a work group. labels May 6, 2024
@SteveL-MSFT
Copy link
Member

I wonder if it would be more generally useful to have a Stop-Pipeline cmdlet that emits the internal exception?

@SteveL-MSFT SteveL-MSFT added WG-NeedsReview Needs a review by the labeled Working Group WG-Cmdlets general cmdlet issues labels May 6, 2024
@MatejKafka
Copy link
Author

@SteveL-MSFT It would definitely be more generally useful. However, there's already an open issue for that, and there doesn't seem to be will or time to design and implement that feature, and this might be a stepping stone that's easier to decide on and implement, and should be a useful addition on its own.

As an aside, I'd preferably make the exception public instead of creating a Stop-Pipeline cmdlet, so that it can be easily invoked from compiled cmdlets, not just from scripts.

@SteveL-MSFT
Copy link
Member

@MatejKafka can you link to the other issue on this? I've queued this up for the WG-Cmdlets to discuss so a conclusion will be made

@mklement0
Copy link
Contributor

@SteveL-MSFT, said issue is already linked to from the initial post:

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Issue-Enhancement the issue is more of a feature request than a bug Needs-Triage The issue is new and needs to be triaged by a work group. WG-Cmdlets general cmdlet issues WG-NeedsReview Needs a review by the labeled Working Group
Projects
None yet
Development

No branches or pull requests

3 participants