Skip to content

Commit

Permalink
fix: Support legasy camelCase args
Browse files Browse the repository at this point in the history
  • Loading branch information
3y3 committed Dec 2, 2024
1 parent b44a743 commit 1b756af
Showing 1 changed file with 55 additions and 20 deletions.
75 changes: 55 additions & 20 deletions src/config/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand All @@ -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);
}

0 comments on commit 1b756af

Please sign in to comment.