Skip to content

Add path basename --no-extension#10521

Merged
mqudsi merged 3 commits into
fish-shell:masterfrom
mqudsi:path_bname_no_ext
May 27, 2024
Merged

Add path basename --no-extension#10521
mqudsi merged 3 commits into
fish-shell:masterfrom
mqudsi:path_bname_no_ext

Conversation

@mqudsi
Copy link
Copy Markdown
Contributor

@mqudsi mqudsi commented May 24, 2024

Add path basename --no-extension

This makes path basename a more useful replacement for the stock basename
command, which can be used with -s .ext to trim .ext from the base name.

Previously, this would have required the equivalent of

path change-extension "" (path basename $path)

but now it can be just

path basename -E $path

TODOs:

  • Changes to fish usage are reflected in user documentation/manpages.
  • Tests have been added for regressions fixed
  • User-visible changes noted in CHANGELOG.rst

mqudsi added 3 commits May 24, 2024 17:53
This makes `path basename` a more useful replacement for the stock `basename`
command, which can be used with `-s .ext` to trim `.ext` from the base name.

Previously, this would have required the equivalent of

    path change-extension "" (path basename $path)

but now it can be just

    path basename -E $path
@faho
Copy link
Copy Markdown
Member

faho commented May 25, 2024

LGTM.

A more general solution would be a path print that you can give the parts you want (path print --no-extension --no-dirname), but this one is probably the most used combination, and it would be easy to allow nonsensical combinations (how do you print the dirname and the extension without the basename?).

@mqudsi
Copy link
Copy Markdown
Contributor Author

mqudsi commented May 25, 2024

Only question I had was the short flag. I used the capitalized E in the sense that some utils use a capitalized version of a short flag to indicate the opposite (so no Extension). Other options were lowercase e, x, or something else.

Another thought is that in the future it might make sense to add -s .ext to trim specific suffix like the standard basename so maybe -S for automatic version of that.

Also if the long flag should be --no-ext or if that's something we should silently support but not advertise.

@faho
Copy link
Copy Markdown
Member

faho commented May 25, 2024

I used the capitalized E in the sense that some utils use a capitalized version of a short flag to indicate the opposite (so no Extension). Other options were lowercase e, x, or something else.

-E seems fine to me. We don't currently have either -e or -E here. The closest we have is --end/--entire in string, both of which have a -e short flag.

-x has the issue that it's "executable" in path filter. Of course (like we see in string above), short flags can have multiple meanings, but if we wanted to add --no-extension to path filter we couldn't use -x there.

So -E is immediately fine, no collisions, and it also seems okay looking at future additions.

Also if the long flag should be --no-ext or if that's something we should silently support but not advertise.

Because of our getopt, this would currently allow --no-ext. But I'm not a fan of having the canonical version be abbreviated like that given that we have change-extension.

Another thought is that in the future it might make sense to add -s .ext to trim specific suffix like the standard basename so maybe -S for automatic version of that.

I don't believe I've ever needed the specific behavior of basename -s. I have needed "give me the extension", and I have needed "make whatever the extension is this instead", but never "remove '.xyz' if it ends in that".

And if I did I'd either match all .xyz files and work on them, or I'd use string to remove the .xyz extension specifically.

@mqudsi
Copy link
Copy Markdown
Contributor Author

mqudsi commented May 25, 2024

The only reason for basename -s is to handle cases like .tar.gz where a naive basename -E would only strip one layer of extensions. But it's rare enough (you'd have to be doing that specific extension and only that specific extension) that I would just use string replace.

@mqudsi mqudsi merged commit f84b199 into fish-shell:master May 27, 2024
@zanchey zanchey added this to the fish next-3.x milestone May 27, 2024
mqudsi added a commit that referenced this pull request May 28, 2024
@github-actions github-actions Bot locked as resolved and limited conversation to collaborators Jun 2, 2025
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants