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

[Discussion] Replacing "$0" with the executing file's base name #983

Merged
merged 1 commit into from Oct 17, 2017
Merged

[Discussion] Replacing "$0" with the executing file's base name #983

merged 1 commit into from Oct 17, 2017

Conversation

lostintangent
Copy link
Contributor

This PR changes the behavior of the $0 token (when used within the contents of the usage, epilog and/or example methods), so that it displays the executing file's basename, as opposed to the full path. This has the affect of providing default help text that is closer to what the end-user would actually run, since they would likely be running the CLI on their path (or via an package.json script), as opposed to using the CLI's entrypoint script path.

This is a breaking change, but since #975 introduces a breaking change for the usage method, I figured I'd put this PR out for discussion's sake. I updated all of the tests to ensure they still pass, but there may be other scenarios that this change would break, so I'd love some feedback.

I'm also not entirely sure this is the right behavior (e.h. as opposed to introducing a new token ala $$0 or something, etc.), and I'm sure this conversation has been had a bunch of times already, so I'd love feedback and/or pointers to an existing/better solution for this. Thanks!


Before this change (using a Yargs-based CLI named kyte):

screen shot 2017-10-15 at 1 42 25 pm

After this change:

screen shot 2017-10-15 at 6 16 02 pm

CC @bcoe

@bcoe
Copy link
Member

bcoe commented Oct 16, 2017

@lostintangent I like this idea. I might be tempted to apply the path.basename logic in yargs.js when we initially populate $0, so that we don't need to apply the replacement multiple times.

What does the output look like when you're running a command line script that's not installed globally, e.g., node ./foo.js --help?

@lostintangent
Copy link
Contributor Author

It would display the file name (without the path), like so:

screen shot 2017-10-15 at 6 57 38 pm

@lostintangent
Copy link
Contributor Author

If you’re cool with this change, I can make the refactoring you recommended and update the PR.

@bcoe
Copy link
Member

bcoe commented Oct 16, 2017

@lostintangent I like this change, I think it's much more readable.

@bcoe
Copy link
Member

bcoe commented Oct 17, 2017

@lostintangent I think switching to basename is a great idea; let's work on getting this landed so that I can rebase it with #975, and land a couple more tweaks.

@bcoe
Copy link
Member

bcoe commented Oct 17, 2017

@lostintangent having thought about this a bit, maybe you're right to perform this change in the view logic, rather than in yargs.js -- given that we populate $0 on the argv object that yargs parses, I'm a bit concerned that switching to basename might break quite a few folks -- having said this, I'm fine with switching to basename in the usage.js since it will make the help output much cleaner.

@lostintangent
Copy link
Contributor Author

@bcoe Great timing! I finally got a minute to work on this again :) So is this PR good as-is?

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

Successfully merging this pull request may close these issues.

None yet

2 participants