Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
4 changed files
with
19 additions
and
25 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters