nanovazquez / yargs-interactive Goto Github PK
View Code? Open in Web Editor NEWInteractive support for yargs
License: MIT License
Interactive support for yargs
License: MIT License
^ like what title says.
I'm trying to enter interactive mode, but whatever I do, i can't. Am I missing something here?
i saved the file like so in test.js
const yargsInteractive = require("yargs-interactive");
const options = {
interactive: { default: true },
name: { type: "input", default: "A robot", describe: "Enter your name" },
likesPizza: {
type: "confirm",
default: false,
describe: "Do you like pizza?",
},
};
yargsInteractive()
.usage("$0 <command> [args]")
.interactive(options)
.then((result) => {
// Your business logic goes here.
// Get the arguments from the result
// e.g. myCli(result.name);
console.log(
`\nResult is:\n` +
`- Name: ${result.name}\n` +
`- Likes pizza: ${result.likesPizza}\n`
);
});
then i run
node test.js --interactive
outputs immediately without prompts:
Result is:
- Name: A robot
- Likes pizza: false
Hi, thanks for the awesome package, I want to use a community prompt - https://github.com/th0r/inquirer-sortable-checkbox, but could not figure out how.
I don't see how to use this package with commands. E.g. ...
yargsInteractive()
.command('do-it', 'Does things.', o => o
.options(myOptions))
.strict()
.demandCommand()
.help()
.argv;
I can only call .interactive(...)
and pass in options directly from the yargsInteractive()
return. I can't seem to configure an interactive command.
Do list
types work for this? I'm playing around and trying the following as an option
Favorite: {
describe: "What is your favorite color?",
prompt: "always",
type: "list",
choices: [
"Blue",
"Red",
"Green",
"Yellow",
"Orange"
],
},
But I just get an error saying You must provide a
choices parameter
.
@nanovazquez I don't know how familiar you are with Typescript, but I opened a PR at DefinitelyTyped to have typings for yargs-interactive. I need to update it now that options
is choices
. But I'm thinking the interfaces should probably extend from inquirer at some point. I'm just not sure what all is passed from yargs-interactive to inquirer here. Any input on that PR would be helpful.
I just implemented this code with a single param of type checkbox
and it works perfectly fine on my Windows machine (using Git Bash with powerline), but on my Linux hosting machine (Shell: zsh v5.4.2 / bash v4.4.20 | Distro: Ubuntu 18.04.3 LTS) it completely breaks. Linux at the top.
And this is the list I'm feeding the checkbox command as choices:
The sourecode is on GitHub: https://github.com/Favna/ribbon/
Or the specific commit which adds the file where I use yargs interactive: favna/ribbon@fcaa7f8
Steps to replicate
--name='Johh' --interactive
args (also shown in README)Expected behaviour
I will not be prompted for name
, as it has been supplied as an arg
Actual behaviour
I am prompted for name
Potential fix
I think I've fixed the problem locally with these changes:
--- a/node_modules/yargs-interactive/src/yargs-interactive.js
+++ b/node_modules/yargs-interactive/src/yargs-interactive.js
@@ -29,10 +29,15 @@ let yargsInteractive = (processArgs = process.argv.slice(2), cwd) => {
// Remove options with prompt value set to 'never'
// and options with prompt value set to 'if-empty' but no default value or value set via parameter
- const interactiveOptions = filterObject(mergedOptions, (item, key) => (
- item.prompt !== 'never'
- && (item.prompt !== 'if-empty' || isEmpty(item.default) || isEmpty(argv[key]))
- ));
+ const interactiveOptions = filterObject(mergedOptions, (item, key) => {
+ if (item.prompt === 'never') {
+ return false;
+ }
+ if (item.prompt === 'always') {
+ return true;
+ }
+ return isEmpty(item.default) && isEmpty(argv[key]);
+ });
// Check if we should get the values from the interactive mode
return argv.interactive
Inquirer allows the choices
property to be an array or a function that returns an array (in order to make the choices dynamic based on previously answered questions). But it seems like yargs expects the choices
property to be a string or array?
Is there a usage where prompts can be shown only for options not set via command line?
A declarative, efficient, and flexible JavaScript library for building user interfaces.
๐ Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. ๐๐๐
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google โค๏ธ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.