diff --git a/command/_errors.ts b/command/_errors.ts index 6a20a93f..3ca6f9cf 100644 --- a/command/_errors.ts +++ b/command/_errors.ts @@ -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 "./deps.ts"; import { EnvVar } from "./types.ts"; diff --git a/command/_utils.ts b/command/_utils.ts index 1bab54f4..db14310e 100644 --- a/command/_utils.ts +++ b/command/_utils.ts @@ -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 "../flags/deprecated.ts"; 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 { + const match: string | undefined = types.length + ? closestString(type, types) + : undefined; + return match ? `${message} "${match}"?` : ""; +} + export function didYouMeanCommand( command: string, commands: Array, @@ -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); } @@ -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(), + ); +} diff --git a/command/command.ts b/command/command.ts index 96bd3a4a..984feed8 100644 --- a/command/command.ts +++ b/command/command.ts @@ -3,13 +3,13 @@ import { UnknownTypeError, ValidationError as FlagsValidationError, } from "../flags/_errors.ts"; -import { underscoreToCamelCase } from "../flags/_utils.ts"; import { parseFlags } from "../flags/flags.ts"; import type { ParseFlagsContext } from "../flags/types.ts"; import { getDescription, parseArgumentsDefinition, splitArguments, + underscoreToCamelCase, } from "./_utils.ts"; import { bold, brightBlue, red } from "./deps.ts"; import { diff --git a/command/help/_help_generator.ts b/command/help/_help_generator.ts index 2596e5c8..12f878e4 100644 --- a/command/help/_help_generator.ts +++ b/command/help/_help_generator.ts @@ -1,6 +1,10 @@ -import { getDefaultValue, getFlag } from "../../flags/_utils.ts"; import { Table } from "../../table/table.ts"; -import { dedent, getDescription, parseArgumentsDefinition } from "../_utils.ts"; +import { + dedent, + getDescription, + getFlag, + parseArgumentsDefinition, +} from "../_utils.ts"; import type { Command } from "../command.ts"; import { bold, @@ -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), diff --git a/flags/_utils.ts b/flags/_utils.ts index 5625c300..c975d0c8 100644 --- a/flags/_utils.ts +++ b/flags/_utils.ts @@ -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. * @@ -63,7 +52,9 @@ export function didYouMean( type: string, types: Array, ): string { - const match: string | undefined = closestString(type, types); + const match: string | undefined = types.length + ? closestString(type, types) + : undefined; return match ? `${message} "${match}"?` : ""; }