Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

minimist-subcommand

Package Overview
Dependencies
Maintainers
1
Versions
3
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

minimist-subcommand

A simple sub-command parser for minimist

  • 2.0.0
  • Source
  • npm
  • Socket score

Version published
Weekly downloads
2.6K
decreased by-33.04%
Maintainers
1
Weekly downloads
 
Created
Source

minimist-subcommand

npm version Build Status

A simple sub-command parser for minimist

Installation

npm install --save minimist-subcommand

Examples

Basic

basic.js:

var parseArgs = require('minimist');
var parseCommands = require('minimist-subcommand');

// parse sub-commands
var commandDefinition = {
  commands: {
    foo: null,
    bar: null
  }
};
var parsedCommandsAndArgv = parseCommands(commandDefinition, process.argv.slice(2));

// pass parsed argv to minimist
var options = parseArgs(parsedCommandsAndArgv.argv);

console.log('sub-command:', parsedCommandsAndArgv.commands);
console.log('parsed options by minimist:', options);
$ node ./basic.js foo arg -a -b val -c
sub-command: [ 'foo' ]
parsed options by minimist: { _: [ 'arg' ], a: true, b: 'val', c: true }
$ node ./basic.js arg -a
sub-command: []
parsed options by minimist: { _: [ 'arg' ], a: true }
$ node ./basic.js bar foo
sub-command: [ 'bar' ]
parsed options by minimist: { _: [ 'foo' ] }

Nested commands

nested-commands.js:

var parseArgs = require('minimist');
var parseCommands = require('minimist-subcommand');

// parse sub-commands
var commandDefinition = {
  commands: {
    singleton: null,
    married: {
      commands: {
        child: {
          commands: {
            grandchild: null
          }
        }
      }
    }
  }
};
var parsedCommandsAndArgv = parseCommands(commandDefinition, process.argv.slice(2));

// pass parsed argv to minimist
var options = parseArgs(parsedCommandsAndArgv.argv);

console.log('commands:', parsedCommandsAndArgv.commands);
console.log('parsed options by minimist:', options);
$ node ./nested-commands.js married child grandchild arg -a
commands: [ 'married', 'child', 'grandchild' ]
parsed options by minimist: { _: [ 'arg' ], a: true }
$ node ./nested-commands.js singleton child grandchild
commands: [ 'singleton' ]
parsed options by minimist: { _: [ 'child', 'grandchild' ] }

Use "default" option

use-default-option.js:

var parseArgs = require('minimist');
var parseCommands = require('minimist-subcommand');

// parse sub-commands
var commandDefinition = {
  default: 'bar',
  commands: {
    foo: null,
    bar: null
  }
};
var parsedCommandsAndArgv = parseCommands(commandDefinition, process.argv.slice(2));

// pass parsed argv to minimist
var options = parseArgs(parsedCommandsAndArgv.argv);

console.log('sub-command:', parsedCommandsAndArgv.commands);
console.log('parsed options by minimist:', options);
$ node ./use-default-option.js arg -a
sub-command: [ 'bar' ]
parsed options by minimist: { _: [ 'arg' ], a: true }

Command's Schema

If you want to check schema of commandDefinition, please use COMMAND_JSON_SCHEMA.

var COMMAND_JSON_SCHEMA = require('minimist-subcommand').COMMAND_JSON_SCHEMA;

var commandDefinition = {
  commands: {
    foo: null,
    bar: null
  }
};

// I will leave it to the judgment of the user.
someJsonSchemaLibrary.validate(COMMAND_JSON_SCHEMA, commandDefinition);

Keywords

FAQs

Package last updated on 04 Apr 2018

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc