Skip to content

Commit

Permalink
update validate example
Browse files Browse the repository at this point in the history
  • Loading branch information
75lb committed Jan 31, 2017
1 parent 2d70566 commit 8b98b9d
Show file tree
Hide file tree
Showing 4 changed files with 19 additions and 25 deletions.
2 changes: 2 additions & 0 deletions example/mocha.js
Expand Up @@ -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 () {
Expand Down
37 changes: 15 additions & 22 deletions 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)
3 changes: 1 addition & 2 deletions package.json
Expand Up @@ -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",
Expand Down
2 changes: 1 addition & 1 deletion test/unknown-options.js
Expand Up @@ -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 })
Expand Down

0 comments on commit 8b98b9d

Please sign in to comment.