How to use the dashdash.Parser function in dashdash

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 any-json / any-json / lib / run.ts View on Github external
function getHelpMessage() {
    const help = new dashdash.Parser(
      {
        options: generalOptions
          .concat(converstionOptions)
          .concat({ group: "combine (additional options)" })
          .concat(combineOptions)
      }
    ).help();
    return `usage: any-json [command] FILE [options] [OUT_FILE]

any-json can be used to convert (almost) anything to JSON.

This version supports:
    cson, csv, hjson, ini, json, json5, toml, yaml

This version has is beta support for:
    xls, xlsx
github any-json / any-json / lib / run.ts View on Github external
if (options._args.length > 2) {
        throw "too many arguments";
      }

      const fileName = options._args[0] as string;
      const format = options.input_format || getFormatFromFileName(fileName);

      const fileContents = await readFile(fileName, anyjson.getEncoding(format))

      const parsed = await anyjson.decode(fileContents, format)

      const outputFileName = options._args[1]
      return await convert(parsed, options, outputFileName);
    }
    case "combine": {
      const parser = new dashdash.Parser(combineConfiguration)

      const options = parser.parse({ argv, slice: 3 });

      const items = await Promise.all(
        options._args.map(async fileName => {
          const format = options.input_format || getFormatFromFileName(fileName);
          const fileContents = await readFile(fileName, anyjson.getEncoding(format)) as string
          return await anyjson.decode(fileContents, format)
        })
      )

      const outputFileName = options.out;
      return await convert(items, options, outputFileName);
    }
    case "split": {
      const parser = dashdash.createParser(convertConfiguration);
github joyent / smartos-live / src / fw / node_modules / cmdln / index.js View on Github external
Cmdln.prototype._renderHelp = function _renderHelp(template, handler, alias) {
    assert.string(template, 'template');
    assert.optionalString(alias, 'alias');

    var help = this._renderTemplate(template, alias);
    if (~help.indexOf('{{usage}}')) {
        var synopses = this.synopsesFromSubcmd(alias || handler);
        if (synopses.length) {
            help = help.replace('{{usage}}',
                'Usage:\n' + indent(synopses.join('\n')));
        }
    }
    if (~help.indexOf('{{options}}') && handler.options) {
        var parser = new dashdash.Parser({options: handler.options});
        var helpOpts = (handler.helpOpts
            ? objMerge(this.helpOpts, handler.helpOpts)
            : this.helpOpts);
        help = help.replace('{{options}}',
            'Options:\n' + parser.help(helpOpts));
    }
    help = help.trimRight();
    return help;
};
github joyent / smartos-live / src / img / node_modules / cmdln / lib / cmdln.js View on Github external
Cmdln.prototype.helpFromSubcmd = function helpFromSubcmd(alias) {
    var handler = this.handlerFromSubcmd(alias);
    if (!handler) {
        throw new UnknownCommandError(alias);
    }

    if (!handler.help) {
        return;
    } else if (typeof (handler.help) === 'function') {
        return handler.help;
    } else {
        var help = handler.help;
        help = help.replace(/{{name}}/g, this.name);
        help = help.replace(/{{cmd}}/g, alias);
        if (~help.indexOf('{{options}}') && handler.options) {
            var parser = new dashdash.Parser({options: handler.options});
            var helpOpts = (handler.helpOpts
                ? objMerge(this.helpOpts, handler.helpOpts) : this.helpOpts);
            help = help.replace('{{options}}',
                'Options:\n' + parser.help(helpOpts));
        }
        help = help.trimRight();
        return help;
    }
};

dashdash

A light, featureful and explicit option parsing library.

MIT
Latest version published 4 years ago

Package Health Score

67 / 100
Full package analysis