How to use dashdash - 10 common examples

To help you get started, we’ve selected a few dashdash examples, based on popular ways it is used in public projects.

Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.

github joyent / smartos-live / src / fw / node_modules / cmdln / index.js View on Github external
args = argv.slice(3);
    }

    if (typeof (handler.main) === 'function') {
        // This is likely a `Cmdln` subclass instance, i.e. a subcli.
        (function callCmdlnHandler(subcmd, opts, args, cb) {
            var argv = ['', ''].concat(args);
            handler.main(argv, cb);
        }).call(this, subcmd, opts, args, finish);
    } else {
        // This is a vanilla `do_SUBCMD` function on the Cmdln class.

        // Skip optional processing if given `opts` -- i.e. it was already done.
        if (argv && handler.options) {
            try {
                var parser = new dashdash.Parser({
                    options: handler.options,
                    interspersed: (handler.interspersedOptions !== undefined
                        ? handler.interspersedOptions : true),
                    allowUnknown: (handler.allowUnknownOptions !== undefined
                        ? handler.allowUnknownOptions : false)
                });
                opts = parser.parse(argv, 3);
            } catch (e) {
                finish(new OptionError(e));
                return;
            }
            args = opts._args;
            debug('-- parse %j opts: %j', subcmd, opts);
        }
        handler.call(this, subcmd, opts, args, finish);
    }
github joyent / smartos-live / src / fw / node_modules / cmdln / index.js View on Github external
this.helpSubcmds = config.helpSubcmds || null;
    if (!this.helpOpts.indent)
        this.helpOpts.indent = space(4);
    else if (typeof (this.helpOpts.indent) === 'number')
        this.helpOpts.indent = space(this.helpOpts.indent);
    if (!this.helpOpts.groupIndent) {
        var gilen = Math.round(this.helpOpts.indent.length / 2);
        this.helpOpts.groupIndent = space(gilen);
    } else if (typeof (this.helpOpts.groupIndent) === 'number') {
        this.helpOpts.groupIndent = space(this.helpOpts.groupIndent);
    }
    if (!this.helpOpts.maxCol) this.helpOpts.maxCol = 80;
    if (!this.helpOpts.minHelpCol) this.helpOpts.minHelpCol = 20;
    if (!this.helpOpts.maxHelpCol) this.helpOpts.maxHelpCol = 40;

    this.optParser = new dashdash.Parser(
        {options: this.options, interspersed: false});

    // Find the tree of constructors (typically just this and the Cmdln
    // super class) on who's prototype to look for "do_*" and "help_*"
    // methods.
    var prototypes = [];
    var ctor = this.constructor;
    while (ctor) {
        prototypes.push(ctor.prototype);
        ctor = ctor.super_; // presuming `util.inherits` usage
    }
    prototypes.reverse();

    // Load subcmds (do_* methods) and aliases (`do_*.aliases`).
    var enumOrder = [];
    this._handlerFromName = {};
github joyent / smartos-live / src / img / node_modules / cmdln / lib / cmdln.js View on Github external
this.helpSubcmds = config.helpSubcmds || null;
    if (!this.helpOpts.indent)
        this.helpOpts.indent = space(4);
    else if (typeof (this.helpOpts.indent) === 'number')
        this.helpOpts.indent = space(this.helpOpts.indent);
    if (!this.helpOpts.groupIndent) {
        var gilen = Math.round(this.helpOpts.indent.length / 2);
        this.helpOpts.groupIndent = space(gilen);
    } else if (typeof (this.helpOpts.groupIndent) === 'number') {
        this.helpOpts.groupIndent = space(this.helpOpts.groupIndent);
    }
    if (!this.helpOpts.maxCol) this.helpOpts.maxCol = 80;
    if (!this.helpOpts.minHelpCol) this.helpOpts.minHelpCol = 20;
    if (!this.helpOpts.maxHelpCol) this.helpOpts.maxHelpCol = 40;

    this.optParser = new dashdash.Parser(
        {options: this.options, interspersed: false});

    // Find the tree of constructors (typically just this and the Cmdln
    // super class) on who's prototype to look for "do_*" and "help_*"
    // methods.
    var prototypes = [];
    var ctor = this.constructor;
    while (ctor) {
        prototypes.push(ctor.prototype);
        ctor = ctor.super_; // presuming `util.inherits` usage
    }
    prototypes.reverse();

    // Load subcmds (do_* methods) and aliases (`do_*.aliases`).
    var enumOrder = [];
    this._handlerFromName = {};
github joyent / smartos-live / src / fw / node_modules / cmdln / index.js View on Github external
Cmdln.prototype.bashCompletionSpec = function bashCompletionSpec(opts) {
    var self = this;
    if (!opts) {
        opts = {};
    }
    assert.object(opts, 'opts');
    assert.optionalString(opts.context, 'opts.context');
    assert.optionalBool(opts.includeHidden, 'opts.includeHidden');

    var spec = [];
    var context = opts.context || '';
    var includeHidden = (opts.includeHidden === undefined
        ? false : opts.includeHidden);

    // Top-level.
    spec.push(dashdash.bashCompletionSpecFromOptions({
        options: self.options,
        context: context,
        includeHidden: includeHidden
    }));

    var aliases = [];
    var allAliases = [];
    Object.keys(this._nameFromAlias).sort().forEach(function (alias) {
        if (alias === '?') {
            // '?' as a Bash completion is painful. Also, '?' as a default
            // alias for 'help' should die.
            return;
        }

        var name = self._nameFromAlias[alias];
        var handler = self._handlerFromName[name];
github joyent / smartos-live / src / fw / node_modules / cmdln / index.js View on Github external
var handler = self._handlerFromName[name];

        if (typeof (handler.bashCompletionSpec) === 'function') {
            // This is a `Cmdln` subclass, i.e. a sub-CLI.
            var subspec = handler.bashCompletionSpec({context: context_});
            if (subspec) {
                spec.push(subspec);
            }
        } else {
            if (handler.completionArgtypes) {
                assert.arrayOfString(handler.completionArgtypes,
                    'do_' + name + '.completionArgtypes');
                spec.push(format('local cmd%s_argtypes="%s"',
                    context_, handler.completionArgtypes.join(' ')));
            }
            spec.push(dashdash.bashCompletionSpecFromOptions({
                options: handler.options || [],
                context: context_,
                includeHidden: includeHidden
            }));
        }
    });
github joyent / smartos-live / src / fw / node_modules / cmdln / index.js View on Github external
assert.object(opts, 'opts');
    assert.optionalString(opts.specExtra, 'opts.specExtra');

    // Gather template data.
    var data = {
        name: this.name,
        date: new Date(),
        spec: this.bashCompletionSpec()
    };
    if (opts.specExtra) {
        data.spec += '\n\n' + opts.specExtra;
    }

    // Render template.
    var template = fs.readFileSync(
        dashdash.BASH_COMPLETION_TEMPLATE_PATH, 'utf8');
    return renderTemplate(template, data);
};
github joyent / smartos-live / src / fw / node_modules / cmdln / index.js View on Github external
OptionError.prototype.cmdlnErrHelpFromErr = function optionErrHelpFromErr(err) {
    if (!err || !err._cmdlnInst) {
        return '';
    }

    var errHelp = '';

    var options = (err._cmdlnHandler || err._cmdlnInst).options;
    if (options) {
        var lines = [];
        var line = 'usage: ' + nameFromErr(err);
        for (var i = 0; i < options.length; i++) {
            var synopsis = dashdash.synopsisFromOpt(options[i]);
            if (!synopsis) {
                continue;
            } else if (line.length === 0) {
                line += '    ' + synopsis;
            } else if (line.length + synopsis.length + 1 > 80) {
                lines.push(line);
                line = '    ' + synopsis;
            } else {
                line += ' ' + synopsis;
            }
        }
        lines.push(line + ' ...');  // The "..." for the args.
        errHelp = lines.join('\n');
    }
github leapdao / solEVM-enforcer / challenger / cliArgs.js View on Github external
names: ['delay'],
    type: 'number',
    env: 'DELAY',
    help: 'Events handling delay (for temp setup)',
    default: 0,
  },
  {
    names: ['stupid'],
    type: 'bool',
    env: 'STUPID',
    help: 'Enables wrong results producing',
    default: false,
  },
];

const parser = dashdash.createParser({ options });

/**
 * @type {{
 *  ethProvider: string;
 *  walletPriv: string;
 *  enforcerAddr: string;
 *  delay: number;
 *  stupid: boolean;
 * }}
 */
const cliArgs = parser.parse(process.argv);

if (cliArgs.help) {
  console.log('Usage:');
  console.log(parser.help({ includeEnv: true }).trimRight());
  process.exit(0);
github sumanjs / suman / lib / runner-helpers / run-child.js View on Github external
var su = require("suman-utils");
var _suman = global.__suman = (global.__suman || {});
require('../helpers/add-suman-global-properties');
var constants = require('../../config/suman-constants').constants;
var fatalRequestReply = require('../helpers/general').fatalRequestReply;
if (process.env.NPM_COLORS === 'no') {
    process.argv.push('--no-color');
    console.log(' => Suman child process setting itself to be color-free (--no-colors)');
}
var sumanOpts = _suman.sumanOpts = _suman.sumanOpts || JSON.parse(process.env.SUMAN_OPTS);
var suman_options_1 = require("../parse-cmd-line-opts/suman-options");
var childArgs = sumanOpts.child_arg || [];
if (childArgs.length) {
    childArgs.unshift('foo');
    childArgs.unshift('baz');
    var opts = void 0, parser = dashdash.createParser({ options: suman_options_1.options });
    try {
        opts = parser.parse(childArgs);
    }
    catch (err) {
        console.error(chalk.red(' => Suman command line options error:'), err.message);
        console.error(' => Try "suman --help" or visit sumanjs.org');
        process.exit(constants.EXIT_CODES.BAD_COMMAND_LINE_OPTION);
    }
    sumanOpts = _suman.sumanOpts = Object.assign(sumanOpts, opts);
}
var usingRunner = _suman.usingRunner = true;
var projectRoot = _suman.projectRoot = process.env.SUMAN_PROJECT_ROOT;
process.send = process.send || function (data) {
    console.error(chalk.magenta('Suman warning:'));
    console.error('process.send() was not originally defined in this process.');
    console.error('(Perhaps we are using Istanbul?), we are logging the first argument to process.send() here => ');
github ThoughtWorksStudios / bobcat / index.js View on Github external
const Interpreter = require("./interpreter"),
  Scopes = require("./scope"),
  fs = require("fs"),
  dashdash = require("dashdash");

function die(message) {
  console.warn(message);
  process.exit(1);
}

var optParser = dashdash.createParser({
  options: [
    {
      names:["help", "h"],
      type: "bool",
      help: "Print usage and exit"
    },
    {
      names:["output", "o"],
      type: "string",
      help: "Output file",
      helpArg: "FILE",
      default: "entities.json"
    },
    {
      names: ["check", "c"],
      type: "bool",

dashdash

A light, featureful and explicit option parsing library.

MIT
Latest version published 4 years ago

Package Health Score

67 / 100
Full package analysis