diff --git a/example/mocha.js b/example/mocha.js index c959d0d..dcd452f 100644 --- a/example/mocha.js +++ b/example/mocha.js @@ -2,6 +2,8 @@ const assert = require('assert') const commandLineArgs = require('../') +/* enable partial parsing to prevent exceptions being thrown +if the user sets undefined, mocha-specific options (e.g. --no-colors) */ const options = commandLineArgs({ name: 'value', type: Number }, { partial: true }) describe('Array', function () { diff --git a/example/validate.js b/example/validate.js index 6bdc460..af06c56 100644 --- a/example/validate.js +++ b/example/validate.js @@ -1,35 +1,28 @@ /* command-line-args parses the command line but does not validate what was collected. - This is one method of testing the values received suit your taste. + This is an example of how values collected can be validated. */ 'use strict' -var commandLineArgs = require('../') -var testValue = require('test-value') -var fs = require('fs') +const commandLineArgs = require('../') +const fs = require('fs') -var optionDefinitions = [ - { name: 'help', type: Boolean }, +const optionDefinitions = [ + { name: 'help', alias: 'h', type: Boolean }, { name: 'files', type: String, multiple: true, defaultOption: true }, { name: 'log-level', type: String } ] -var options = commandLineArgs(optionDefinitions) +const options = commandLineArgs(optionDefinitions) -/* all supplied files should exist and --log-level should be one from the list */ -var correctUsageForm1 = { - files: function (files) { - return files && files.length && files.every(fs.existsSync) - }, - 'log-level': [ 'info', 'warn', 'error', undefined ] -} - -/* passing a single --help flag is also valid */ -var correctUsageForm2 = { - help: true -} - -/* test the options for usage forms 1 or 2 */ -var valid = testValue(options, [ correctUsageForm1, correctUsageForm2 ]) +const valid = + options.help || + ( + /* all supplied files should exist and --log-level should be one from the list */ + options.files && + options.files.length && + options.files.every(fs.existsSync) && + [ 'info', 'warn', 'error', undefined ].includes(options['log-level']) + ) console.log('your options are', valid ? 'valid' : 'invalid', options) diff --git a/package.json b/package.json index a87c73d..4b03f9f 100644 --- a/package.json +++ b/package.json @@ -28,8 +28,7 @@ "devDependencies": { "coveralls": "^2.11.15", "jsdoc-to-markdown": "^2.0.1", - "test-runner": "^0.3.0", - "test-value": "^2.1.0" + "test-runner": "^0.3.0" }, "dependencies": { "array-back": "^1.0.4", diff --git a/test/unknown-options.js b/test/unknown-options.js index e945a4e..abfc8ba 100644 --- a/test/unknown-options.js +++ b/test/unknown-options.js @@ -33,7 +33,7 @@ runner.test('unknown option: defaultOption 2', function () { const definitions = [ { name: 'files', type: String, defaultOption: true, multiple: true }, { name: 'one', type: Boolean }, - { name: 'two', alias: 't', defaultValue: 2 }, + { name: 'two', alias: 't', defaultValue: 2 } ] const argv = [ 'file1', '--one', 'file2', '-t', '--two=3', 'file3', '-ab' ] const options = commandLineArgs(definitions, { argv, partial: true })