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

USAGE command name should always reflect the actual binary name #295

Open
eaigner opened this issue Apr 3, 2021 · 1 comment
Open

USAGE command name should always reflect the actual binary name #295

eaigner opened this issue Apr 3, 2021 · 1 comment
Labels
bug Something isn't working

Comments

@eaigner
Copy link

eaigner commented Apr 3, 2021

The default USAGE output derives the usage command name from the ParsableCommand type name. Now this might be correct or not. The "correct" way would be to always use the last path component of argv[0], as it would also show the correct string in case the build product name is different from the class name or the user renames the tool.

While there is _commandName I can use to override this, it is marked as for internal use only.

@frou
Copy link

frou commented Apr 12, 2021

Another common occurrence of this is running the executable via a symlink, where the symlink has a different basename to the executable it's pointing to.

I guess ProcessInfo.processInfo.processName from Foundation already has the logic for slicing up argv[0]?

@natecook1000 natecook1000 added the bug Something isn't working label Apr 25, 2021
smvz added a commit to smvz/swift-argument-parser that referenced this issue Oct 4, 2022
This adds the `shouldUseExecutableName` property, allowing the
command name to be derived from the executable's file name.

The property defaults to false, both because subcommands using
it is probably undesirable and to preserve existing behaviour
after updating the package.
dcantah added a commit to dcantah/swift-argument-parser that referenced this issue May 31, 2024
Fixes: apple#633 apple#570 apple#295

This seems like the "correct" default. If your root command had the same name
as the resulting binary you produce, this was never an issue, however if
your root command was named anything else, then the current Usage/help text was
kind of odd.

If you don't specify an explicit commandName in CommandConfiguration, we would
take the root command name and generate a command name for you out of hyphens.
So, `RootCommand` would become `root-command` and this is what would be displayed
in the usage text for `/path/to/cool-binary --help`, even though intuitively you'd
expect to see `USAGE: cool-binary`.

The current behavior was also strange for binaries that are classically invoked
via symlinks as the same thing would happen. In this case imagine the struct name
matched the actual binaries name, but because the symlink is what users actually
invoke you could end up seeing an unfamiliar name in help text regardless. Using
argv[0] solves most of these problems.

The downside here is a LOT of tests need to change. I went with foregoing the
new approach if the user explicitly provides a command name, so most of the existing
tests that check for exact string matches of help text either need to replace the
command name with the first argument of the binary that is running the test (xctest,
but it doesn't really matter what it is as I've added a helper to plop in the first
argument for all of these tests), or they need to define
CommandConfiguration(commandName: "name-we-want"). For any tests that implemented
ParsableCommand I mostly went with the latter to make as few changes as possible.

Given this seems like mostly a UX change, I hope this seems sane.

Signed-off-by: Danny Canter <danny@dcantah.dev>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

3 participants