From 1b756af1e92a5688fa474e0ada82affe081b12cb Mon Sep 17 00:00:00 2001 From: 3y3 <3y3@ya.ru> Date: Tue, 3 Dec 2024 00:49:02 +0300 Subject: [PATCH] fix: Support legasy camelCase args --- src/config/index.ts | 75 +++++++++++++++++++++++++++++++++------------ 1 file changed, 55 insertions(+), 20 deletions(-) diff --git a/src/config/index.ts b/src/config/index.ts index 987c65c1..0fde8e52 100644 --- a/src/config/index.ts +++ b/src/config/index.ts @@ -334,26 +334,13 @@ export class Command extends BaseCommand { addOption(o: Option | ExtendedOption): this { super.addOption(o); - if (o.isBoolean() && (o as ExtendedOption)[OptionSource]) { - const original = (o as ExtendedOption)[OptionSource]; - const flags = original.flags - // replace short flags with void - .replace(/(^|\s+|,)-\w+\s*($|,?\s*)/g, '') - // add negation to long options - .replace(/--/g, '--no-'); - - const negated = { - ...original, - flags, - desc: 'auto negation', - hidden: true, - }; - - delete negated.default; - delete negated.defaultInfo; - delete negated.deprecated; - - super.addOption(option(negated)); + const camelcase = camelCaseOption(o as ExtendedOption); + if (camelcase) { + super.addOption(camelcase); + } + + if (o.isBoolean()) { + super.addOption(negatedOption(o as ExtendedOption)); } if (o.description) { @@ -367,3 +354,51 @@ export class Command extends BaseCommand { return new Help(); } } + +function camelCaseOption(o: ExtendedOption) { + const original = (o as ExtendedOption)[OptionSource]; + const flags = original.flags + // replace short flags with void + .replace(/(^|\s+|,)-\w+\s*($|,?\s*)/g, '') + // convert to camel case (ignoring option first letter) + .replace(/([^-])-([a-z])/g, (_, $1, $2) => $1 + $2.toUpperCase()); + + if (original.flags.includes(flags)) { + return null; + } + + const camelcase = { + ...original, + flags, + desc: 'auto camelcase', + deprecated: 'Use "kebab case" variant of this option.', + hidden: true, + }; + + delete camelcase.default; + delete camelcase.defaultInfo; + + return option(camelcase); +} + +function negatedOption(o: ExtendedOption) { + const original = (o as ExtendedOption)[OptionSource]; + const flags = original.flags + // replace short flags with void + .replace(/(^|\s+|,)-\w+\s*($|,?\s*)/g, '') + // add negation to long options + .replace(/--/g, '--no-'); + + const negated = { + ...original, + flags, + desc: 'auto negation', + hidden: true, + }; + + delete negated.default; + delete negated.defaultInfo; + delete negated.deprecated; + + return option(negated); +}