minimist-options
Pretty options for minimist
Last updated 8 years ago by vdemedes .
MIT · Repository · Bugs · Original npm · Tarball · package.json
$ gnpm install minimist-options 
SYNC missed versions from official npm registry.

minimist-options test

Write options for minimist and yargs in a comfortable way. Supports string, boolean, number and array options.

Installation

$ npm install --save minimist-options

Usage

const buildOptions = require('minimist-options');
const minimist = require('minimist');

const options = buildOptions({
	name: {
		type: 'string',
		alias: 'n',
		default: 'john'
	},

	force: {
		type: 'boolean',
		alias: ['f', 'o'],
		default: false
	},

	score: {
		type: 'number',
		alias: 's',
		default: 0
	},

	arr: {
		type: 'array',
		alias: 'a',
		default: []
	},

	strings: {
		type: 'string-array',
		alias: 's',
		default: ['a', 'b']
	},

	booleans: {
		type: 'boolean-array',
		alias: 'b',
		default: [true, false]
	},

	numbers: {
		type: 'number-array',
		alias: 'n',
		default: [0, 1]
	},

	published: 'boolean',

	// Special option for positional arguments (`_` in minimist)
	arguments: 'string'
});

const args = minimist(process.argv.slice(2), options);

instead of:

const minimist = require('minimist');

const options = {
	string: ['name', '_'],
	number: ['score'],
	array: [
		'arr',
		{key: 'strings', string: true},
		{key: 'booleans', boolean: true},
		{key: 'numbers', number: true}
	],
	boolean: ['force', 'published'],
	alias: {
		n: 'name',
		f: 'force',
		s: 'score',
		a: 'arr'
	},
	default: {
		name: 'john',
		f: false,
		score: 0,
		arr: []
	}
};

const args = minimist(process.argv.slice(2), options);

Array options

The array types are only supported by yargs.

minimist does not explicitly support array type options. If you set an option multiple times, it will indeed yield an array of values. However, if you only set it once, it will simply give the value as is, without wrapping it in an array. Thus, effectively ignoring {type: 'array'}.

{type: 'array'} is shorthand for {type: 'string-array'}. To have values coerced to boolean or number, use boolean-array or number-array, respectively.

License

MIT © Vadim Demedes

Current Tags

  • 4.1.0                                ...           latest (5 years ago)

12 Versions

  • 4.1.0                                ...           5 years ago
  • 4.0.2                                ...           5 years ago
  • 4.0.1                                ...           6 years ago
  • 4.0.0                                ...           6 years ago
  • 3.0.2                                ...           7 years ago
  • 3.0.1                                ...           8 years ago
  • 3.0.0                                ...           8 years ago
  • 2.1.0                                ...           8 years ago
  • 2.0.2                                ...           8 years ago
  • 2.0.1                                ...           8 years ago
  • 2.0.0                                ...           8 years ago
  • 1.0.0                                ...           9 years ago
Maintainers (1)
Downloads
Today 0
This Week 0
This Month 0
Last Day 0
Last Week 0
Last Month 0
Dependencies (2)
Dev Dependencies (2)
Dependents (1)

Copyright 2013 - present © cnpmjs.org | Home |