Skip to content

Commit

Permalink
fix(CLI): Do not crash on help request
Browse files Browse the repository at this point in the history
  • Loading branch information
medikoo committed Dec 13, 2021
1 parent d74adc3 commit f6feb0b
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 0 deletions.
10 changes: 10 additions & 0 deletions lib/cli/parse-args.js
Expand Up @@ -14,6 +14,7 @@ module.exports = (
) => {
const result = Object.create(null);
result._ = [];
const isHelpRequest = args.includes('-h') || args.includes('--help');
for (let i = 0, arg; (arg = args[i]); ++i) {
if (arg === '--') {
result._.push(...args.slice(i + 1));
Expand All @@ -32,6 +33,7 @@ module.exports = (
if (aliasNames) {
if (aliasNames.length > 1) {
if (value != null) {
if (isHelpRequest) return result;
throw new ServerlessError(
`Unexpected value for "-${aliasNames}"`,
'UNEXPECTED_CLI_PARAM_VALUE'
Expand All @@ -40,6 +42,7 @@ module.exports = (
for (const aliasName of aliasNames) {
paramName = alias.get(aliasName) || aliasName;
if (result[paramName] != null) {
if (isHelpRequest) return result;
throw new ServerlessError(
`Unexpected multiple "--${paramName}" (aliased by "-${aliasName}") values`,
'UNEXPECTED_CLI_PARAM_MULTIPLE_VALUE'
Expand All @@ -56,6 +59,7 @@ module.exports = (

if (string.has(paramName) || multiple.has(paramName)) {
if (isBoolean) {
if (isHelpRequest) return result;
throw new ServerlessError(`Unexpected boolean "--${paramName}"`, 'MISSING_CLI_PARAM_VALUE');
}
if (multiple.has(paramName)) {
Expand All @@ -64,6 +68,7 @@ module.exports = (
continue;
}
if (result[paramName] !== undefined) {
if (isHelpRequest) return result;
throw new ServerlessError(
`Unexpected multiple "--${paramName}"`,
'UNEXPECTED_CLI_PARAM_MULTIPLE_VALUE'
Expand All @@ -76,12 +81,14 @@ module.exports = (
if (paramName.startsWith('no-') && (boolean.has(paramName.slice(3)) || isBoolean)) {
paramName = paramName.slice(3);
if (value != null) {
if (isHelpRequest) return result;
throw new ServerlessError(
`Unexpected value for "--${paramName}"`,
'UNEXPECTED_CLI_PARAM_VALUE'
);
}
if (result[paramName] !== undefined) {
if (isHelpRequest) return result;
throw new ServerlessError(
`Unexpected multiple "--${paramName}"`,
'UNEXPECTED_CLI_PARAM_MULTIPLE_VALUE'
Expand All @@ -93,12 +100,14 @@ module.exports = (

if (boolean.has(paramName) || isBoolean) {
if (value != null) {
if (isHelpRequest) return result;
throw new ServerlessError(
`Unexpected value for "--${paramName}"`,
'UNEXPECTED_CLI_PARAM_VALUE'
);
}
if (result[paramName] !== undefined) {
if (isHelpRequest) return result;
throw new ServerlessError(
`Unexpected multiple "--${paramName}"`,
'UNEXPECTED_CLI_PARAM_MULTIPLE_VALUE'
Expand All @@ -112,6 +121,7 @@ module.exports = (
if (Array.isArray(result[paramName])) {
result[paramName].push(value == null ? args[++i] : value || null);
} else if (typeof result[paramName] === 'boolean') {
if (isHelpRequest) return result;
throw new ServerlessError(
`Unexpected multiple "--${paramName}"`,
'UNEXPECTED_CLI_PARAM_MULTIPLE_VALUE'
Expand Down
5 changes: 5 additions & 0 deletions test/unit/lib/cli/parse-args.test.js
Expand Up @@ -139,6 +139,11 @@ describe('test/unit/lib/cli/parse-args.test.js', () => {
expect(parsedArgs).to.deep.equal({});
});

it('should not throw if -h or --help param', () => {
parseArgs(['-ab=foo', '--help'], {});
parseArgs(['--boolean=value', '-h'], { boolean: new Set(['boolean']) });
});

it('should reject value for mutliple boolean properties alias', () =>
expect(() => parseArgs(['-ab=foo'], {}))
.to.throw(ServerlessError)
Expand Down

0 comments on commit f6feb0b

Please sign in to comment.