-
Notifications
You must be signed in to change notification settings - Fork 109
/
validate.js
28 lines (23 loc) · 844 Bytes
/
validate.js
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
/*
command-line-args parses the command line but does not validate what was collected.
This example demonstrates how the values collected can be validated.
*/
'use strict'
const commandLineArgs = require('../')
const fs = require('fs')
const optionDefinitions = [
{ name: 'help', alias: 'h', type: Boolean },
{ name: 'files', type: String, multiple: true, defaultOption: true },
{ name: 'log-level', type: String }
]
const options = commandLineArgs(optionDefinitions)
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)