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
Interactive mode #286
Comments
Interesting idea. Thanks, @gimenete! One note on your implementation: I wouldn't call Most API methods (i.e. ones that define options, commands, or usage text) are just synchronous, chainable configuration steps. Arg parsing should be reserved for the yargs.alias('i', 'interactive').interactive('i', fn)
yargs.interactive('i', fn).alias('i', 'interactive') I really appreciate the sample code, but if we do this, we'll want to make the "interactive" logic as robust as possible, which could get complicated. I think adding interactivity is a really cool idea, but it may be better accomplished by frameworks like Vorpal. I don't know. Maybe a good candidate for yargs 4? @bcoe What do you think? |
Thanks @nexdrew for your comments The main reason to make So my idea is: in case you want to support the interactive mode call Another idea would be to create a different npm module ( |
@gimenete To get around the "validation fails -> exit process" problem, try using |
I like this idea, but one thought I have is perhaps we could pull the prompt handler into its own module -- it seems like enough logic would be added that it would start to muddle index.js a bit. One other thought, do we pull in inquirer rather than re-inventing the wheel? |
This could also be an interesting option, to avoid the async behavior: |
oh, I can try to find some free time to move my changes to another file ( |
ok, so I moved the interactive code to a separate file and I started using master...gimenete:feature/interactive-mode I've implemented support for regular inputs, choices (adding the ability to select multiple elements) and booleans. |
My 2¢: I like the idea of interactivity, but I don't like changing how yargs works (sync -> async) just to support it. Personally, I would prefer a synchronous configuration step and a separate (possibly asynchronous) terminal parsing/interactive step, if possible. I haven't had a chance to play with your branch yet. Hopefully I can make some time soon-ish. In the meantime, what do you think about making this a separate module, possibly wrapping yargs? |
Ok, so I just took a few minutes to play with your example. First impression is pretty cool! I still think we should explore other options as well and possibly clean up the API some. I'll try to do some exploration over the next few days. |
If we don't really want an async version then we will need to not use Nevertheless note that this interactive mode is totally optional. If somebody doesn't need/want an interactive mode he will keep using the Doing a separate module is another option, but I would prefer having this functionality on yargs so more people will use it, and I think it's simpler to implement inside var yargs = require('yargs')()
yargs.interactive = function (key, fn) {
yargs.boolean(key)
yargs.fail(function(fn) {
// I receive a message but I should parse it?
})
var argv = yargs.argv
// nothing is set in argv apparently but we should do:
if (!argv[key]) return fn(argv)
// how to check which arguments failed?
// interactive mode here
// fn(argv)
}
module.exports = yargs Should be possible but the main issue is that the PS: @bcoe congrats for the wedding and have a great honeymoon! |
Just for the record, I'm not against an async version of parsing for interactive mode, but I think it should be separate from configuring an option to represent that mode. That way, consumers can use the typical Perhaps we start by introducing an async version of parsing first (accepting a callback or returning a promise if no callback is given) as one PR, so we make sure we get the API right without interactive mode, and then we follow that up by adding interactive mode (which will imply use of async parsing). What do you think? |
Do you mean doing something like this? yargs
.usage('This is my awesome program\n\nUsage: $0 [options]')
.string('x', 'y')
.alias('i', 'interactive')
.interactive('i') // implement the interactive option after the async API
.parse(function(argv) {
// first implement this new async API
}) We can call it |
Well, there's already a |
haha, ok. Another suggestion for the name? I'll change my branch to match this API design. |
No great ideas. I hate to say it (because it seems like an anti-pattern), but |
I've just added the |
@gimenete haven't forgotten about this slick feature; I'm just getting back in the swing of things post vacation, and am working on catching up on several other OSS projects I maintain. I'm happy with whatever API you and @nexdrew come to for this, Andrew has smart thoughtful opinions on this sort of thing 👍 |
@gimenete I still love this idea, one thought I had as we move towards
Long story short, @gimenete I would love to get your feedback as we start chunking up work for the next major release of yargs. |
@bcoe give me a couple of days and I'll review again my branch and with the other things you are mentioning :) |
I'm using inquirer for that currently. Config of options, define the command and define the inquiries. Does that help? |
@bcoe @nexdrew I've pushed new code. Sorry for not responding earlier. See all the changes here master...gimenete:feature/interactive-mode There is one decision that I made that I don't know if you agree with: you can specify arguments both in the command line (as always) and in the interactive mode if it is present and here master...gimenete:feature/interactive-mode#diff-b72d3f57671eb1c20676ce167bdeac75R6 is where I calculate the missing options. I have found only a problem with this: I always get true/false for boolean values. I never get null or undefined so it never asks you for boolean options This should be all regarding my initial proposal. But you mention that it would be nice to have also async commands and async configuration. I don't know what you mean with async configuration but for async commands I guess you mean something like this: .options({
input: {
alias: 'in',
description: '<filename> Input file name',
requiresArg: true,
required: true,
validate: function(value, callback) { fs.exists(value, callback) }
}
}) |
@gimenete I really like this work 👍 one thought comes to mind. Now that we've split yargs into an organization, perhaps we could make interactive
Perhaps we could call the project something like CC: @SBoudrias, @gimenete, @nexdrew |
FWIW, that sounds a lot like what we're planning on doing in the next iteration of the Yeoman generator api yeoman/generator#894 |
any news here? |
Sorry, I wanted this for a project that needed many command line options, so it was interesting to be able to use an interactive mode If you didn't (want to) remember all, or you could just pass all if knew all or you were scripting the cli app. But I finally ended up creating a GUI and since then I've just used inquirer or yargs depending on the needs. I still think that it would be great to have the ability to select an interactive mode though. |
I was looking for something like this today. I built an app that logs in to your online banking to fetch your balance, and I didn't want to pass my bank password to my terminal (because it gets logged in terminal history), and I didn't want to store it on my filesystem. So I wanted the It would be great to be able to specify |
A couple of months ago, I was in need of something like this, and I created: yargs-interactive. This tool helps you to implement, using the same code, a CLI tool with interactive support, but also non-interactive and/or mixed mode. It helps my team to develop CLI libraries used by both devs and CI tools. I'd like to revive this issue because I'm all about enhancing
Before starting making changes to
Thanks! |
Single Input type is perfect. In my case I would mostly use it to ask for confirmation (Y/n questions). |
This feature is beyond the scope of yargs, I think it would be better to direct folks to use tools like inquirerthat is designed for user input. What I'd definitely be open to would be us adding a section to our docs with an example of how you might use inquirer and yargs in conjunction. |
It would be great to have support for an interactive mode. I've written some code here with an example
gimenete@0d059ee
If the idea is accepted I could finish it, make tests and create a PR.
The text was updated successfully, but these errors were encountered: