Skip to content

Commit 958a3f2

Browse files
authored
fix: handle unknown flags with random characters in name correctly (#920)
1 parent cfe88e0 commit 958a3f2

File tree

1 file changed

+8
-3
lines changed

1 file changed

+8
-3
lines changed

src/lib/command-framework/CommandError.ts

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,9 @@ export interface FlagData {
4242
ambiguousFlag?: string;
4343
ambiguousMessage?: string;
4444
unknownOptionSuggestion?: string;
45+
shortForm?: boolean;
4546
}
47+
4648
export interface CommandErrorOptions {
4749
code: CommandErrorCode;
4850
command: typeof BuiltApifyCommand;
@@ -89,7 +91,7 @@ export class CommandError extends Error {
8991
}
9092

9193
case CommandErrorCode.NODEJS_ERR_PARSE_ARGS_UNKNOWN_OPTION: {
92-
const match = /Unknown option '--(?<optionName>[a-zA-Z0-9]+)'\.(?<nodeSuggestion>.*)/gi.exec(
94+
const match = /Unknown option '-(?<longForm>-)?(?<optionName>.+)'\.(?<nodeSuggestion>.*)/gi.exec(
9395
this.message,
9496
);
9597

@@ -103,6 +105,7 @@ export class CommandError extends Error {
103105
name: match.groups!.optionName,
104106
expectsValue: false,
105107
unknownOptionSuggestion: match.groups!.nodeSuggestion,
108+
shortForm: !match.groups!.longForm,
106109
};
107110
}
108111

@@ -128,7 +131,9 @@ export class CommandError extends Error {
128131
});
129132

130133
return [
131-
chalk.gray(`Unknown flag provided: ${chalk.white.bold(`--${flagData.name}`)}`),
134+
chalk.gray(
135+
`Unknown flag provided: ${chalk.white.bold(flagData.shortForm ? `-${flagData.name}` : `--${flagData.name}`)}`,
136+
),
132137
flagData.unknownOptionSuggestion
133138
? chalk.gray(` ${flagData.unknownOptionSuggestion.trim()}`)
134139
: null,
@@ -228,7 +233,7 @@ export class CommandError extends Error {
228233
}
229234

230235
static buildMessageFromFlagData(flagData: FlagData): string {
231-
const base = [`Flag ${chalk.white.bold(`--${flagData.name}`)}`];
236+
const base = [`Flag ${chalk.white.bold(flagData.shortForm ? `-${flagData.name}` : `--${flagData.name}`)}`];
232237

233238
if (flagData.ambiguousFlag) {
234239
base.push(`is ambiguous (meaning the provided value could be interpreted as a flag too).`);

0 commit comments

Comments
 (0)