Skip to content

Commit

Permalink
fix(cli): yargs strict no longer works as expected
Browse files Browse the repository at this point in the history
We need to test for unknown options but allow unknown arguments

See yargs/yargs#1701 and yargs/yargs#1710
  • Loading branch information
fcastilloec committed Jan 27, 2021
1 parent 986c16d commit b274519
Showing 1 changed file with 8 additions and 2 deletions.
10 changes: 8 additions & 2 deletions src/cli-logic.js
@@ -1,19 +1,20 @@
#!/usr/bin/env node
const yargs = require('yargs');
const fs = require('fs-extra');
const _ = require('lodash');
const { fromResults } = require('./lib/json2xls');
const { logicOperations } = require('./lib/logicOperations');
const { changeFileExtension } = require('./lib/utils');

const { argv } = yargs
.version(require('../package').version)
.wrap(null)
.strict()
.alias('help', 'h')
.group(['help', 'version'], 'Global options')
.parserConfiguration({
'duplicate-arguments-array': false,
'boolean-negation': false,
'strip-aliased': true,
})
.usage('Usage: $0 [command] <options>')
.command({
Expand Down Expand Up @@ -58,7 +59,11 @@ const { argv } = yargs
default: false,
type: 'boolean',
})
.check((arg) => {
.check((arg, options) => {
const unknownOptions = _.difference(Object.keys(arg), Object.keys(options).concat(['_', '$0']));
if (unknownOptions.length) {
throw new Error(`Unrecognized ${unknownOptions.join(', ')} option`);
}
if (!(arg.merge || arg.and || arg.or || arg.not)) {
throw new Error('At least one command is needed');
}
Expand Down Expand Up @@ -87,6 +92,7 @@ const { argv } = yargs
desc: 'Converts a JSON into xls',
builder: (args) => {
args
.strict()
.positional('jsonFile', {
describe: 'The JSON file to convert',
type: 'string',
Expand Down

0 comments on commit b274519

Please sign in to comment.