minimist-subcommand
A simple sub-command parser for minimist
Installation
npm install minimist-subcommand
Examples
Basic
basic.js
:
const parseArgs = require('minimist');
const {parseCommands} = require('minimist-subcommand');
const commandDefinition = {
commands: {
foo: null,
bar: null
}
};
const parsedCommandsAndArgv = parseCommands(commandDefinition, process.argv.slice(2));
const 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
:
const parseArgs = require('minimist');
const {parseCommands} = require('minimist-subcommand');
const commandDefinition = {
commands: {
singleton: null,
married: {
commands: {
child: {
commands: {
grandchild: null
}
}
}
}
}
};
const parsedCommandsAndArgv = parseCommands(commandDefinition, process.argv.slice(2));
const 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
:
const parseArgs = require('minimist');
const {parseCommands} = require('minimist-subcommand');
const commandDefinition = {
default: 'bar',
commands: {
foo: null,
bar: null
}
};
const parsedCommandsAndArgv = parseCommands(commandDefinition, process.argv.slice(2));
const 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
.
const COMMAND_JSON_SCHEMA = require('minimist-subcommand').COMMAND_JSON_SCHEMA;
const commandDefinition = {
commands: {
foo: null,
bar: null
}
};
someJsonSchemaLibrary.validate(COMMAND_JSON_SCHEMA, commandDefinition);