package
Collect user input from cli flags, interactive prompts, and js input.
npm install optaGitHub-flavored package documentation.
Across all versions.
Download trend unavailable.
Runtime metadata.
Tagged topics.
We all have choices to make
Collect user input from cli flags, interactive prompts, and JS input.
$ npm i optaconst opta = require('opta')
// Create an opta instance
const opts = opta({
options: {
time: {
type: 'number'
},
hello: {
default: 'hello'
},
world: {
default: 'world'
}
}
})
// .cli() returns an instance of yargs,
// configured with the options
const yargsInstance = opts.cli()
// Will set the appropriate values as
// configured on the yargs instance
yargsInstance(process.argv.slice(2))
One of the primary focuses of opta is to enable easy composition of inputs
across projects. If a project exports it's opta instance, another can simply
combine their options parameters like this:
const opta = require('opta')
// Project One exports a key `opts` which is
// an instance of `opta`, along with it's other functionality
const projectOne = require('project-one')
// My Project which wants to expose functionality from
// Project One can simply merge `opts.options` into the
// options passed to my instance
const myOpts = opta({
options: {
...projectOne.opts.options,
myOptions: true,
// You can also simply override the options descriptor
// from `projectOne.opts`. In this example we disable
// the prompt but leave the rest alone
projectOneOpt: {
...projectOne.opts.options.projectOneOpt,
prompt: false
}
}
Testing user input can be difficult. This package exports a test helper which can mimic different behaviors of users interacting with prompts without dealing with the complicated work to actually handle user input. Here is an example:
const test = require('node:test');
const assert = require('node:assert')
const opta = require('opta')
const optaUtils = require('opta/utils')
test('some test', async (t) => {
const opts = opta({
options: {
foo: true,
bar: true
},
// Create a promptModule mock
promptModule: optaUtils.test.promptModule({
// Will assert that two prompts were displayed
assertCount: