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

add support for default commands to yargs #467

Closed
m-mujica opened this issue Apr 5, 2016 · 14 comments
Closed

add support for default commands to yargs #467

m-mujica opened this issue Apr 5, 2016 · 14 comments

Comments

@m-mujica
Copy link

m-mujica commented Apr 5, 2016

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,

@m-mujica m-mujica changed the title Is there a way to provided a default command in 4.x? Is there a way to provide a default command in 4.x? Apr 5, 2016
@maxrimue
Copy link
Member

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?

@nexdrew
Copy link
Member

nexdrew commented Apr 25, 2016

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.

@vjpr
Copy link

vjpr commented May 16, 2016

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 !parsed.help to the default command check.

@bcoe
Copy link
Member

bcoe commented May 16, 2016

@nexdrew @m-mujica I like the idea of following prior art, and using * 👍

@nexdrew
Copy link
Member

nexdrew commented May 16, 2016

@vjpr brings up a good point.

  1. Do we treat the default command just like a hidden command, so it can still apply config for options, validation, etc?
  2. Should it affect usage/help content at all?
  3. Can I mark a regular named command as the default?

@bcoe bcoe changed the title Is there a way to provide a default command in 4.x? add support for default commands to yargs Oct 16, 2016
@bcoe
Copy link
Member

bcoe commented Dec 1, 2016

@timoxley closed #719 in favor of this; I continue to wish we had a simple method like defaultCommand() that would allow a default command that fires if no positional arguments are provided; @nexdrew has some really neat ideas around this feature too, surrounding how default commands relate to command aliases.

@bcoe
Copy link
Member

bcoe commented Dec 29, 2016

Another related ticket: #728

@bcoe
Copy link
Member

bcoe commented Dec 29, 2016

@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.

@ahmadnassri
Copy link

@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.

@maxrimue
Copy link
Member

maxrimue commented Jan 4, 2017

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:

  • Any existing command with name and handler can get a default: true in its builder
  • Command gets called either by name or if no command is supplied
  • Help output adds a bold [default] to said command

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?

@bcoe
Copy link
Member

bcoe commented Jan 20, 2017

@m-mujica @maxrimue @nexdrew, I'm 100% in agreement that having a way to handle default commands in 7.x of yargs should be a goal -- it will make program flow-control much more consistent.

Where I'm not 100% in agreement, is whether we should use the * syntax, or simply introduce a new API method like .defaultCommand('batman'); I could be swayed in either direction.

@nexdrew or @m-mujica perhaps you could create a straw-man pull request for this feature, just to continue the discussion?

@bcoe
Copy link
Member

bcoe commented Feb 27, 2017

@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:

npm cache clear; npm i yargs@next.

@bcoe bcoe closed this as completed Feb 27, 2017
@atian25
Copy link

atian25 commented Mar 2, 2017

@bcoe

seem this feature is undocument at http://yargs.js.org/docs

my mistake, still in next branch.

--

try @next and found an issue, when set a command name to ['start', '*'], then exec my-bin --help will not print other commands help

@atian25 atian25 mentioned this issue Mar 2, 2017
@nexdrew
Copy link
Member

nexdrew commented Mar 2, 2017

@atian25 Concerning this:

try yargs@next and found an issue, when set a command name to ['start', '*'], then exec my-bin --help will not print other commands help

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!

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

No branches or pull requests

7 participants