@opt/require

Plugin for @opt/parse adding type def which calls require() on the value.

Usage no npm install needed!

<script type="module">
  import optRequire from 'https://cdn.skypack.dev/@opt/require';
</script>

README

@opt/require

Build Status Dependency Status npm version Coverage Status

Plugin for @opt/parse adding type def which calls require() on the value.

It adds to nopt's typeDef a new type marked by the string require. When an option has that type it is tested as a path first, then, require() is called on it to get the result, which is placed into the parsed options.

Basically, you can do an option like: name: require and name: [require, Array] and you'll get back the result of the require operation in the name property.

See @opt/parse

See @opt/nopt

See @use/core

Install

npm install @opt/require --save

Usage

var parse = require('@opt/parse')

// make nopt the parser and apply our require plugin
parse.use('@opt/nopt', '@opt/require')

var options = {
  // the type def is marked with string 'require()'
  // this allows multiple of them, set into an array.
  plugin: [require, Array]
}

// an example args array with a plugin option to require()
// the module path must be absolute, or relative to CWD. or use ~/
var argv = [ 'node', 'some.js', '--plugin', 'some/module' ]

// then use parse as you would use `nopt`,
// plus any changes made possible by the plugins added
options = parse(options, {}, argv, 2)

// the above would produce:
options = {
  // can't write a function... so, 'theModuleFunction' means a real function
  // returned by require('some/module')
  plugin: [ theModuleFunction ]
  argv: {
    original: [ 'node', 'some.js', '--plugin', 'some/module' ]
    original: [ 'node', 'some.js', '--plugin', 'some/module' ]
    remain  : []
  }
}

MIT License