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

Default command #301

Closed
sondr3 opened this issue Nov 20, 2015 · 10 comments
Closed

Default command #301

sondr3 opened this issue Nov 20, 2015 · 10 comments

Comments

@sondr3
Copy link

sondr3 commented Nov 20, 2015

I've looked through most of the issues but I haven't seen anything like this, I want my command line program to run a command when you just type the name of the program. Right now it just defaults to nothing, and I haven't figured out how to make it do something without writing any commands. I thought about making a hidden default command, but I couldn't figure out how to make it run when you don't type any commands. Any help would be greatly appreciated 😄

@nexdrew
Copy link
Member

nexdrew commented Nov 22, 2015

@sondr3 Hey thanks for the question! Sorry for the delayed response.

Can you elaborate a bit more on what you're trying to do? Is one of the following accurate?

  1. Your program has multiple commands and you want to use one as the default so that it runs when no command is specified.
  2. Your program has no "commands" but rather is designed to do one thing (possibly with many options).

If the first, yargs does not have direct support for this out-of-the-box, but it should be rather simple to implement on your own (e.g. if argv._[0] is not valid, run your default command function yourself).

If the second, yargs is fully synchronous, which makes it easy to run whatever code you want after args are parsed and validated via the .argv terminating accessor property. If a help option, version option, or invalid option was given on the command line, yargs will, by default, show the help/usage content and exit your program.

Does this help?

@sondr3
Copy link
Author

sondr3 commented Nov 22, 2015

Yeah, I figured out how to do the first one on my own after a while, just thought that maybe there was a default flag or something you could add to a command or something. Thanks for the help though!

@sondr3 sondr3 closed this as completed Nov 22, 2015
@th0r
Copy link

th0r commented Sep 24, 2016

I have the save question.
My usecase: I have, say, 2 commands: check and reset.
The main command is check and it would be nice to execute it if user omits command:

// There two lines should do the same
$ program check --opt1 --opt2
$ program --opt1 --opt2

Actually, there is an ambiguity if check can take arguments:

$ program cmd-or-arg --opt1 --opt2

In this case it's not clear what cmd-or-arg is - is it an argument to default check command or another command.

But in my case check can't take positional arguments and it would be nice to have a possibility to omit it because it will be used in 99% of cases.

it should be rather simple to implement on your own (e.g. if argv._[0] is not valid, run your default command function yourself).

@nexdrew The problem here is to validate options that check can take. Is there any way to do it programmatically?

Yeah, I figured out how to do the first one on my own after a while

@sondr3 Could you share your final solution?

@th0r
Copy link

th0r commented Sep 28, 2016

@sondr3 @nexdrew Guys?

@nexdrew
Copy link
Member

nexdrew commented Sep 28, 2016

@th0r You bring up a good point, but I think I can come up with a workaround. I'll take a closer look later today.

Also, know that my next two self-appointed tasks for yargs is to build in support for (1) command aliases and (2) specifying a default command. Once those are in place, yargs should be able to handle your use-case out of the box.

Thanks for your feedback and patience. I'll get back to this soon.

@th0r
Copy link

th0r commented Sep 28, 2016

my next two self-appointed tasks for yargs is to build in support for (1) command aliases and (2) specifying a default command

Great news and thanks for your work!

@sondr3
Copy link
Author

sondr3 commented Sep 28, 2016

@th0r sorry, forgot to reply, I can't even remember what it was for nor the solution I came up with. 😕

@StreetStrider
Copy link

StreetStrider commented Jan 27, 2017

I think default command is a good solution, because it allows to put all logic in commands. It seemes that there're a two ways to use such thinks like yargs:
The first one: just simply call .argv and use returned singletone-ish object afterwards. It is simple and sane. However, it does not scale well and besides I can use just minimist for such simple cases.
The second one: you start using subcommands. You put your logic into handlers. At that phase there's an issue that you will get executed code both in command and after it. That is annoying.

Possible solutions:

  1. Put process.exit() at the end of each command and use code after as a default case. Works, but dirty.
  2. Bring into some global variable, like is_default = true and set it to false in every command. Dirty as well.
  3. Create a default command, which will be used if no command passed. This is clear, becaue all code will live in handlers and can be splitted into files, and because it mimics usual behavior, when you got some default-like command (list something or show help), which can be called explicitly (via corresponding command) or implicitly (as default).

@StreetStrider
Copy link

BTW, commander looks like dead. It has a feature that allows to mark command as default, but it kinda not working at all for 1.5 year due to bug. tj/commander.js#469 no one cares

@atian25
Copy link

atian25 commented Mar 2, 2017

any news at this ? @nexdrew

land at #467 (comment)

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

5 participants