forked from sap-tutorials/Tutorials
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathvalidate.js
More file actions
35 lines (28 loc) · 1.02 KB
/
validate.js
File metadata and controls
35 lines (28 loc) · 1.02 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
/*
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.
*/
'use strict'
var commandLineArgs = require('../')
var testValue = require('test-value')
var fs = require('fs')
var optionDefinitions = [
{ name: 'help', type: Boolean },
{ name: 'files', type: String, multiple: true, defaultOption: true },
{ name: 'log-level', type: String }
]
var 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 ])
console.log('your options are', valid ? 'valid' : 'invalid', options)