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 support for default commands to yargs #467
Comments
Hey @m-mujica, unfortunately, there currently is no official way how to do this in yargs. What do folks think about adding this kind of default command that applies if an unknown command or none is called? |
I like this idea. Seems like it would help simplify some "control flow" logic with commands, so you don't have to use some sort of custom state to keep track of if a command was executed or not. |
Workaround import yargsParser from 'yargs-parser'
const args = process.argv
const parsed = yargsParser(args)
const useDefaultCommand = parsed._.length === 2
const defaultCommand = 'publish' // <-- set name of your command here
if (useDefaultCommand && !parsed.help) {
args.splice(2, 0, defaultCommand)
}
const argv = yargs.parse(args) Its good because you can use all your options for your command, without having to add them to the top level. Probably need to tweak the help to print your default command info. EDIT Added |
@vjpr brings up a good point.
|
@timoxley closed #719 in favor of this; I continue to wish we had a simple method like |
Another related ticket: #728 |
@ahmadnassri I closed your issue in favor of this one, I would love to figure out what the ideal API would be for handling a default or "top level" command. |
@bcoe thx, I did come across this one, I figured it to be non-related. but if you see them as such, I'll keep watching here. |
Okay, I'd like to work on this feature to get it landed possibly in v7. My current proposal would be to make default commands happen like this:
This way, you have minimal work to set things up and can even still explicitly call the command. We can also avoid making folks duplicate commands like that, in case they want an actual name and default behaviour. What does everyone think? |
@m-mujica @maxrimue @nexdrew, I'm 100% in agreement that having a way to handle default commands in Where I'm not 100% in agreement, is whether we should use the @nexdrew or @m-mujica perhaps you could create a straw-man pull request for this feature, just to continue the discussion? |
@m-mujica @vjpr @ahmadnassri, hey! update ... we're planning on releasing @nexdrew's implementation of default commands in the next release. They work essentially like @m-mujica describes: https://github.com/yargs/yargs#default-commands I would love your help testing the new feature, before we push it out to more people:
|
my mistake, still in next branch. -- try @next and found an issue, when set a command name to |
@atian25 Concerning this:
We're aware of this issue and will have the fix in soon. Let us know if you find any other oddities or problems. Thanks! |
Hey guys, is there any way to do something like:
yargs.command("*", [module])
similar to commander
I found https://www.npmjs.com/package/yargs-default-command but does not seem to work with 4.x
Thanks,
The text was updated successfully, but these errors were encountered: