Skip to content

Commit

Permalink
Merge remote-tracking branch 'origin/main' into JOTSR-main
Browse files Browse the repository at this point in the history
  • Loading branch information
c4spar committed Mar 18, 2024
2 parents 766634a + 6319a78 commit eb19021
Show file tree
Hide file tree
Showing 5 changed files with 49 additions and 18 deletions.
2 changes: 1 addition & 1 deletion command/_errors.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { didYouMeanCommand } from "./_utils.ts";
import type { Command } from "./command.ts";
import { getFlag } from "../flags/_utils.ts";
import { getFlag } from "./_utils.ts";
import { bold } from "@std/fmt/colors";
import { EnvVar } from "./types.ts";

Expand Down
35 changes: 34 additions & 1 deletion command/_utils.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,33 @@
import { closestString } from "https://deno.land/std@0.216.0/text/closest_string.ts";
import {
UnexpectedArgumentAfterVariadicArgumentError,
UnexpectedRequiredArgumentError,
} from "../flags/_errors.ts";
import { didYouMean } from "../flags/_utils.ts";
import { OptionType } from "@cliffy/flags";
import type { Command } from "./command.ts";
import type { Argument } from "./types.ts";

export function getFlag(name: string) {
if (name.startsWith("-")) {
return name;
}
if (name.length > 1) {
return `--${name}`;
}
return `-${name}`;
}

export function didYouMean(
message: string,
type: string,
types: Array<string>,
): string {
const match: string | undefined = types.length
? closestString(type, types)
: undefined;
return match ? `${message} "${match}"?` : "";
}

export function didYouMeanCommand(
command: string,
commands: Array<Command>,
Expand All @@ -15,6 +36,7 @@ export function didYouMeanCommand(
const commandNames = commands
.map((command) => command.getName())
.filter((command) => !excludes.includes(command));

return didYouMean(" Did you mean command", command, commandNames);
}

Expand Down Expand Up @@ -164,3 +186,14 @@ export function getDescription(
? description.trim().split("\n", 1)[0].trim()
: dedent(description);
}

/** Convert underscore case string to camel case. */
export function underscoreToCamelCase(str: string): string {
return str
.replace(/([a-z])([A-Z])/g, "$1_$2")
.toLowerCase()
.replace(
/_([a-z])/g,
(g) => g[1].toUpperCase(),
);
}
2 changes: 1 addition & 1 deletion command/command.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,12 @@ import {
UnknownTypeError,
ValidationError as FlagsValidationError,
} from "../flags/_errors.ts";
import { underscoreToCamelCase } from "../flags/_utils.ts";
import { parseFlags, type ParseFlagsContext } from "@cliffy/flags";
import {
getDescription,
parseArgumentsDefinition,
splitArguments,
underscoreToCamelCase,
} from "./_utils.ts";
import { bold, brightBlue, red } from "@std/fmt/colors";
import {
Expand Down
13 changes: 10 additions & 3 deletions command/help/_help_generator.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
import { getDefaultValue, getFlag } from "../../flags/_utils.ts";
import { Table } from "@cliffy/table";
import { dedent, getDescription, parseArgumentsDefinition } from "../_utils.ts";
import {
dedent,
getDescription,
getFlag,
parseArgumentsDefinition,
} from "../_utils.ts";
import type { Command } from "../command.ts";
import {
bold,
Expand Down Expand Up @@ -307,7 +311,10 @@ export class HelpGenerator {
option.required && hints.push(yellow(`required`));

if (typeof option.default !== "undefined") {
const defaultValue = getDefaultValue(option);
const defaultValue = typeof option.default === "function"
? option.default()
: option.default;

if (typeof defaultValue !== "undefined") {
hints.push(
bold(`Default: `) + inspect(defaultValue, this.options.colors),
Expand Down
15 changes: 3 additions & 12 deletions flags/_utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,17 +9,6 @@ export function paramCaseToCamelCase(str: string): string {
);
}

/** Convert underscore case string to camel case. */
export function underscoreToCamelCase(str: string): string {
return str
.replace(/([a-z])([A-Z])/g, "$1_$2")
.toLowerCase()
.replace(
/_([a-z])/g,
(g) => g[1].toUpperCase(),
);
}

/**
* Find option by flag, name or alias.
*
Expand Down Expand Up @@ -63,7 +52,9 @@ export function didYouMean(
type: string,
types: Array<string>,
): string {
const match: string | undefined = closestString(type, types);
const match: string | undefined = types.length
? closestString(type, types)
: undefined;
return match ? `${message} "${match}"?` : "";
}

Expand Down

0 comments on commit eb19021

Please sign in to comment.