Skip to content

Add only command to std-rfc/iter#16015

Merged
fdncred merged 4 commits intonushell:mainfrom
132ikl:only
Jun 23, 2025
Merged

Add only command to std-rfc/iter#16015
fdncred merged 4 commits intonushell:mainfrom
132ikl:only

Conversation

@132ikl
Copy link
Copy Markdown
Member

@132ikl 132ikl commented Jun 20, 2025

Description

This PR adds the only command to std-rfc/iter, which is a command I wrote a while ago that I've found so useful that I think it could have a place in the standard library. It acts similarly to get 0, but ensures that the value actually exists, and there aren't additional values. I find this most useful when chained with where, when you want to be certain that no additional elements are accidentally selected when you only mean to get a single element.

I'll copy the help page here for additional explanation:

Get the only element of a list or table, ensuring it exists and there are no extra elements.

Similar to first with no arguments, but errors if there are no additional
items when there should only be one item. This can help avoid issues when more
than one row than expected matches some criteria.

This command is useful when chained with where to ensure that only one row
meets the given condition.

If a cell path is provided as an argument, it will be accessed after the first
element. For example, only foo is roughly equivalent to get 0.foo, with
the guarantee that there are no additional elements.

Note that this command currently collects streams.

Examples:

Get the only item in a list, ensuring it exists and there's no additional items

[5] | only
# => 5

Get the name column of the only row in a table

[{name: foo, id: 5}] | only name
# => foo

Get the modification time of the file named foo.txt

ls | where name == "foo.txt" | only modified

Here's some additional examples showing the errors:
image

image

Most of the time I chain this with a simple where, but here's a couple other real world examples of how I've used this:

With parse, which outputs a table:

let commit = $selection | parse "{start}.g{commit}-{end}" | only commit

Ensuring that only one row in a table has a name that ends with a certain suffix:

$devices | where ($chosen_name ends-with $it.name) | only

Unfortunately to get these nice errors I had to collect the stream (and I think the errors are more useful for this). This should be to be mitigated with (something like) #16014.

Putting this in std/iter might be pushing it, but it seems just close enough that I can't really justify putting it in a different/new module.

User-Facing Changes

  • Adds the only command to std-rfc/iter, which can be used to ensure that a table or list only has a single element.

Tests + Formatting

Added a few tests for only including error cases

After Submitting

N/A

@github-actions github-actions bot added the A:std-library Defining and improving the standard library written in Nu label Jun 20, 2025
132ikl and others added 2 commits June 23, 2025 12:55
Co-authored-by: Bahex <Bahex@users.noreply.github.com>
@fdncred fdncred merged commit 0b202d5 into nushell:main Jun 23, 2025
16 checks passed
@fdncred
Copy link
Copy Markdown
Contributor

fdncred commented Jun 23, 2025

Thanks. Looks fun!

@github-actions github-actions bot added this to the v0.106.0 milestone Jun 23, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

A:std-library Defining and improving the standard library written in Nu

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants