fix(CLI): Ensure to recognize "-v" param as boolean in all cases

This commit is contained in:
Mariusz Nowak 2021-02-18 09:41:44 +01:00 committed by Mariusz Nowak
parent 496d3574c6
commit 82b95fc492
2 changed files with 20 additions and 7 deletions

View File

@ -6,7 +6,7 @@ const memoizee = require('memoizee');
const parseArgs = require('./parse-args');
const baseArgsSchema = {
boolean: new Set(['version', 'help', 'help-interactive']),
boolean: new Set(['version', 'help', 'help-interactive', 'v']),
string: new Set(['config']),
alias: new Map([
['c', 'config'],
@ -18,7 +18,7 @@ module.exports = memoizee(() => {
const options = parseArgs(args, baseArgsSchema);
const commands = options._;
delete options._;
if (!commands.length && options.v === true && !options.version) {
if (!commands.length && options.v && !options.version) {
// Ideally we should output version info in whatever context "--version" or "-v" params
// are used. Still "-v" is defined also as a "--verbose" alias for some commands.
// Support for "--verbose" is expected to go away with

View File

@ -21,7 +21,6 @@ describe('test/unit/lib/cli/resolve-input.test.js', () => {
'h',
'--config',
'conf',
'-v',
'elo',
'other',
],
@ -31,7 +30,7 @@ describe('test/unit/lib/cli/resolve-input.test.js', () => {
});
it('should resolve commands', async () => {
expect(data.commands).to.deep.equal(['cmd1', 'cmd2', 'ver', 'h', 'other']);
expect(data.commands).to.deep.equal(['cmd1', 'cmd2', 'ver', 'h', 'elo', 'other']);
});
it('should recognize --version as boolean', async () => {
@ -46,8 +45,22 @@ describe('test/unit/lib/cli/resolve-input.test.js', () => {
expect(data.options.config).to.equal('conf');
});
it('should not recognize -v with command', async () => {
expect(data.options.v).to.equal('elo');
describe('"-v" handling', () => {
before(() => {
resolveInput.clear();
data = overrideArgv(
{
args: ['serverless', 'cmd1', 'cmd2', '-v', 'ver', 'other'],
},
() => resolveInput()
);
});
it('should not recognize as version alias', async () => {
expect(data.options).to.not.have.property('version');
});
it('should recognize as boolean', async () => {
expect(data.options.v).to.equal(true);
});
});
});
describe('when no commands', () => {
@ -66,7 +79,7 @@ describe('test/unit/lib/cli/resolve-input.test.js', () => {
expect(data.commands).to.deep.equal([]);
});
it('should recognize -v alias', async () => {
it('should recognize -v as --version alias', async () => {
expect(data.options.version).to.equal(true);
});