Skip to content

Commit

Permalink
refactor(command,flags): make options.flags an array
Browse files Browse the repository at this point in the history
  • Loading branch information
c4spar committed Feb 7, 2021
1 parent 98dfe3b commit eeac740
Show file tree
Hide file tree
Showing 7 changed files with 15 additions and 30 deletions.
9 changes: 4 additions & 5 deletions command/command.ts
Expand Up @@ -546,12 +546,12 @@ export class Command<O = any, A extends Array<any> = any> {
: [];

const option: IOption = {
...opts,
name: "",
description: desc,
args,
flags: result.flags.join(", "),
flags: result.flags,
typeDefinition: result.typeDefinition,
...opts,
};

if (option.separator) {
Expand All @@ -562,7 +562,7 @@ export class Command<O = any, A extends Array<any> = any> {
}
}

for (const part of result.flags) {
for (const part of option.flags) {
const arg = part.trim();
const isLong = /^--/.test(arg);

Expand Down Expand Up @@ -788,8 +788,7 @@ export class Command<O = any, A extends Array<any> = any> {
*/
protected async execute(options: O, ...args: A): Promise<IParseResult<O, A>> {
const actionOption = this.findActionFlag(options);

if (actionOption && actionOption.action) {
if (actionOption?.action) {
await actionOption.action.call(this, options, ...args);
return { options, args, cmd: this, literal: this.literalArgs };
}
Expand Down
7 changes: 1 addition & 6 deletions command/completions/_bash_completions_generator.ts
Expand Up @@ -134,11 +134,7 @@ ${childCommandCompletions}`;

private getFlags(command: Command): string[] {
return command.getOptions(false)
.map((option) =>
option.flags
.split(",")
.map((flag) => flag.trim())
)
.map((option) => option.flags)
.flat();
}

Expand All @@ -152,7 +148,6 @@ ${childCommandCompletions}`;
opts += 'case "${prev}" in';
for (const option of options) {
const flags: string = option.flags
.split(",")
.map((flag) => flag.trim())
.join("|");

Expand Down
5 changes: 2 additions & 3 deletions command/completions/_fish_completions_generator.ts
Expand Up @@ -93,11 +93,10 @@ ${this.generateCompletions(this.cmd).trim()}
}

private completeOption(command: Command, option: IOption) {
const flags = option.flags.split(/[, ] */g);
const shortOption: string | undefined = flags
const shortOption: string | undefined = option.flags
.find((flag) => flag.length === 2)
?.replace(/^(-)+/, "");
const longOption: string | undefined = flags
const longOption: string | undefined = option.flags
.find((flag) => flag.length > 2)
?.replace(/^(-)+/, "");

Expand Down
10 changes: 4 additions & 6 deletions command/completions/_zsh_completions_generator.ts
Expand Up @@ -206,13 +206,11 @@ function _${replaceSpecialChars(path)}() {` +
private generateOptions(command: Command, path: string) {
const options: string[] = [];
const cmdArgs: string[] = path.split(" ");
const baseName: string = cmdArgs.shift() as string;
const _baseName: string = cmdArgs.shift() as string;
const completionsPath: string = cmdArgs.join(" ");

const excludedFlags: string[] = command.getOptions(false)
.map((option) =>
option.standalone ? option.flags.split(/[, ] */g) : false
)
.map((option) => option.standalone ? option.flags : false)
.flat()
.filter((flag) => typeof flag === "string") as string[];

Expand All @@ -228,7 +226,7 @@ function _${replaceSpecialChars(path)}() {` +
completionsPath: string,
excludedOptions: string[],
): string {
const flags = option.flags.split(/[, ] */g);
const flags = option.flags;
let excludedFlags = option.conflicts?.length
? [
...excludedOptions,
Expand All @@ -237,7 +235,7 @@ function _${replaceSpecialChars(path)}() {` +
: excludedOptions;
excludedFlags = option.collect ? excludedFlags : [
...excludedFlags,
...option.flags.split(/[, ] */g),
...flags,
];

let args = "";
Expand Down
4 changes: 2 additions & 2 deletions command/help/_help_generator.ts
Expand Up @@ -101,7 +101,7 @@ export class HelpGenerator {
return this.label("Options") +
Table.from([
...options.map((option: IOption) => [
option.flags.split(/,? +/g).map((flag) => blue(flag)).join(", "),
option.flags.map((flag) => blue(flag)).join(", "),
highlightArguments(
option.typeDefinition || "",
this.options.types,
Expand All @@ -121,7 +121,7 @@ export class HelpGenerator {
return this.label("Options") +
Table.from([
...options.map((option: IOption) => [
option.flags.split(/,? +/g).map((flag) => blue(flag)).join(", "),
option.flags.map((flag) => blue(flag)).join(", "),
red(bold("-")) + " " +
option.description.split("\n").shift() as string,
this.generateHints(option),
Expand Down
2 changes: 1 addition & 1 deletion command/types.ts
Expand Up @@ -70,7 +70,7 @@ export interface ICommandOption<O = any, A extends Array<any> = any>
export interface IOption<O = any, A extends Array<any> = any>
extends ICommandOption<O, A>, IFlagOptions {
description: string;
flags: string;
flags: Array<string>;
typeDefinition?: string;
args: IArgument[];
}
Expand Down
8 changes: 1 addition & 7 deletions flags/_utils.ts
Expand Up @@ -37,13 +37,7 @@ export function didYouMeanOption(
): string {
const optionNames = options
.map((option) => [option.name, ...(option.aliases ?? [])])
.reduce(
(prev, cur) => {
prev.push(...cur);
return prev;
},
[],
)
.flat()
.map((option) => getFlag(option));
return didYouMean(" Did you mean option", getFlag(option), optionNames);
}
Expand Down

0 comments on commit eeac740

Please sign in to comment.