Skip to content

Commit 63881c2

Browse files
committed
fix: complain about flags only if they are provided
1 parent 7f15fb6 commit 63881c2

File tree

1 file changed

+22
-17
lines changed

1 file changed

+22
-17
lines changed

src/lib/command-framework/apify-command.ts

Lines changed: 22 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -315,8 +315,15 @@ export abstract class ApifyCommand<T extends typeof BuiltApifyCommand = typeof B
315315
return { baseFlagName, rawBaseFlagName, allMatchers: [baseFlagName, ...allMatchers] };
316316
}
317317

318-
private _commandFlagKeyToCamelCasePropertyKey(commandFlagKey: string) {
319-
return camelCaseString(kebabCaseString(camelCaseToKebabCase(commandFlagKey)).toLowerCase());
318+
private _commandFlagKeyToKebabCaseRegisteredName(commandFlagKey: string) {
319+
let flagKey = kebabCaseString(camelCaseToKebabCase(commandFlagKey)).toLowerCase();
320+
321+
if (flagKey.startsWith('no-')) {
322+
// node handles `no-` flags by negating the flag, so we need to handle that differently if we register a flag with a "no-" prefix
323+
flagKey = flagKey.slice(3);
324+
}
325+
326+
return flagKey;
320327
}
321328

322329
private _parseFlags(rawFlags: ParseResult['values']) {
@@ -339,23 +346,23 @@ export abstract class ApifyCommand<T extends typeof BuiltApifyCommand = typeof B
339346
const camelCasedName = camelCaseString(baseFlagName);
340347

341348
if (builderData.exclusive?.length) {
342-
const existingExclusiveFlags = exclusiveFlagMap.get(camelCasedName) ?? new Set();
349+
const existingExclusiveFlags = exclusiveFlagMap.get(baseFlagName) ?? new Set();
343350

344351
for (const exclusiveFlag of builderData.exclusive) {
345-
existingExclusiveFlags.add(this._commandFlagKeyToCamelCasePropertyKey(exclusiveFlag));
352+
existingExclusiveFlags.add(this._commandFlagKeyToKebabCaseRegisteredName(exclusiveFlag));
346353
}
347354

348-
exclusiveFlagMap.set(camelCasedName, existingExclusiveFlags);
355+
exclusiveFlagMap.set(baseFlagName, existingExclusiveFlags);
349356

350357
// Go through each exclusive flag for this one flag and also add it
351358
for (const exclusiveFlag of builderData.exclusive) {
352-
const exclusiveFlagCamelCasedName = this._commandFlagKeyToCamelCasePropertyKey(exclusiveFlag);
359+
const exclusiveFlagKebabCasedName = this._commandFlagKeyToKebabCaseRegisteredName(exclusiveFlag);
353360

354-
const exclusiveFlagExisting = exclusiveFlagMap.get(exclusiveFlagCamelCasedName) ?? new Set();
361+
const exclusiveFlagExisting = exclusiveFlagMap.get(exclusiveFlagKebabCasedName) ?? new Set();
355362

356-
exclusiveFlagExisting.add(camelCasedName);
363+
exclusiveFlagExisting.add(baseFlagName);
357364

358-
exclusiveFlagMap.set(exclusiveFlagCamelCasedName, exclusiveFlagExisting);
365+
exclusiveFlagMap.set(exclusiveFlagKebabCasedName, exclusiveFlagExisting);
359366
}
360367
}
361368

@@ -460,7 +467,7 @@ export abstract class ApifyCommand<T extends typeof BuiltApifyCommand = typeof B
460467
}
461468

462469
// Re-validate required (we don't have the flag and it's either required or passed in)
463-
if (!this.flags[camelCasedName] && (builderData.required || rawFlag)) {
470+
if (this.flags[camelCasedName] == null && (builderData.required || rawFlag != null)) {
464471
throw new CommandError({
465472
code: CommandErrorCode.APIFY_MISSING_FLAG,
466473
command: this.ctor,
@@ -477,22 +484,20 @@ export abstract class ApifyCommand<T extends typeof BuiltApifyCommand = typeof B
477484

478485
for (const [flagA, flags] of exclusiveFlagMap) {
479486
// If the flag is not set, or is set to null, we can skip it
480-
if (this.flags[flagA] == null) {
487+
if (rawFlags[flagA] == null) {
481488
continue;
482489
}
483490

484491
for (const flagB of flags) {
485-
if (this.flags[flagB] == null) {
492+
if (rawFlags[flagB] == null) {
486493
continue;
487494
}
488495

489496
// At this point we know both are set
490-
const flagAValue = this.flags[flagA];
491-
const flagBValue = this.flags[flagB];
492-
493-
const flagRepresentation = (flag: string, value: unknown) => {
494-
const kebabCasedFlag = kebabCaseString(camelCaseToKebabCase(flag)).toLowerCase();
497+
const flagAValue = (rawFlags[flagA] as (string | boolean)[])[0];
498+
const flagBValue = (rawFlags[flagB] as (string | boolean)[])[0];
495499

500+
const flagRepresentation = (kebabCasedFlag: string, value: unknown) => {
496501
if (typeof value === 'boolean') {
497502
return value ? `--${kebabCasedFlag}` : `--no-${kebabCasedFlag}`;
498503
}

0 commit comments

Comments
 (0)