Skip to content

Commit

Permalink
fix: remove standardized id from usage string
Browse files Browse the repository at this point in the history
  • Loading branch information
mdonnalley committed Nov 13, 2023
1 parent d2c1696 commit cd746a6
Show file tree
Hide file tree
Showing 7 changed files with 23 additions and 18 deletions.
3 changes: 2 additions & 1 deletion src/command.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import {ux} from './cli-ux'
import {Config} from './config'
import * as Errors from './errors'
import {PrettyPrintableError} from './errors'
import {formatCommandDeprecationWarning, formatFlagDeprecationWarning, normalizeArgv, toConfiguredId} from './help/util'
import {formatCommandDeprecationWarning, formatFlagDeprecationWarning, normalizeArgv} from './help/util'
import {PJSON} from './interfaces'
import {LoadOptions} from './interfaces/config'
import {CommandError} from './interfaces/errors'
Expand All @@ -28,6 +28,7 @@ import {Plugin} from './interfaces/plugin'
import * as Parser from './parser'
import {aggregateFlags} from './util/aggregate-flags'
import {requireJson} from './util/fs'
import {toConfiguredId} from './util/ids'
import {uniq} from './util/util'

const pjson = requireJson<PJSON>(__dirname, '..', 'package.json')
Expand Down
8 changes: 6 additions & 2 deletions src/help/command.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import stripAnsi from 'strip-ansi'
import {Command} from '../command'
import * as Interfaces from '../interfaces'
import {ensureArgObject} from '../util/ensure-arg-object'
import {toStandardizedId} from '../util/ids'
import {colorize} from '../util/theme'
import {castArray, compact, sortBy} from '../util/util'
import {DocOpts} from './docopts'
Expand Down Expand Up @@ -308,17 +309,20 @@ export class CommandHelp extends HelpFormatter {
}

protected usage(): string {
const {usage} = this.command
const {id, usage} = this.command
const standardId = toStandardizedId(id, this.config)
const body = (usage ? castArray(usage) : [this.defaultUsage()])
.map((u) => {
const allowedSpacing = this.opts.maxWidth - this.indentSpacing

const dollarSign = colorize(this.config?.theme?.dollarSign, '$')
const bin = colorize(this.config?.theme?.bin, this.config.bin)

const command = colorize(this.config?.theme?.command, '<%= command.id %>')

const commandDescription = colorize(
this.config?.theme?.sectionDescription,
u.replace('<%= command.id %>', '').trim(),
u.replace('<%= command.id %>', '').replace(standardId, '').trim(),
)

const line = `${dollarSign} ${bin} ${command} ${commandDescription}`.trim()
Expand Down
3 changes: 2 additions & 1 deletion src/help/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,13 @@ import {error} from '../errors'
import * as Interfaces from '../interfaces'
import {load} from '../module-loader'
import {cacheDefaultValue} from '../util/cache-default-value'
import {toConfiguredId} from '../util/ids'
import {colorize} from '../util/theme'
import {compact, sortBy, uniqBy} from '../util/util'
import {CommandHelp} from './command'
import {HelpFormatter} from './formatter'
import RootHelp from './root'
import {formatCommandDeprecationWarning, getHelpFlagAdditions, standardizeIDFromArgv, toConfiguredId} from './util'
import {formatCommandDeprecationWarning, getHelpFlagAdditions, standardizeIDFromArgv} from './util'

export {CommandHelp} from './command'
export {getHelpFlagAdditions, normalizeArgv, standardizeIDFromArgv} from './util'
Expand Down
10 changes: 1 addition & 9 deletions src/help/util.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import * as ejs from 'ejs'

import {collectUsableIds} from '../config/util'
import {Deprecation, Config as IConfig} from '../interfaces'
import {toStandardizedId} from '../util/ids'

export function template(context: any): (t: string) => string {
function render(t: string): string {
Expand Down Expand Up @@ -52,15 +53,6 @@ function collateSpacedCmdIDFromArgs(argv: string[], config: IConfig): string[] {
return argv // ID is argv[0]
}

export function toStandardizedId(commandID: string, config: IConfig): string {
return commandID.replaceAll(new RegExp(config.topicSeparator, 'g'), ':')
}

export function toConfiguredId(commandID: string, config: IConfig): string {
const defaultTopicSeparator = ':'
return commandID.replaceAll(new RegExp(defaultTopicSeparator, 'g'), config.topicSeparator || defaultTopicSeparator)
}

export function standardizeIDFromArgv(argv: string[], config: IConfig): string[] {
if (argv.length === 0) return argv
if (config.topicSeparator === ' ') argv = collateSpacedCmdIDFromArgs(argv, config)
Expand Down
5 changes: 2 additions & 3 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,7 @@ checkCWD()
export * as Args from './args'
export * as ux from './cli-ux'
export {flush} from './cli-ux/flush'
// Remove these in the next major version
export {stderr, stdout} from './cli-ux/stream'
export {stderr, stdout} from './cli-ux/stream' // Remove these in the next major version
export {Command} from './command'
export {Config, Plugin} from './config'
export * as Errors from './errors'
Expand All @@ -25,11 +24,11 @@ export {execute} from './execute'
export * as Flags from './flags'
export {CommandHelp, Help, HelpBase, loadHelpClass} from './help'
export {HelpSection, HelpSectionKeyValueTable, HelpSectionRenderer} from './help/formatter'
export {toConfiguredId, toStandardizedId} from './help/util'
export * as Interfaces from './interfaces'
export {Hook} from './interfaces/hooks'
export {run} from './main'
export * as ModuleLoader from './module-loader'
export * as Parser from './parser'
export {Performance} from './performance'
export {Settings, settings} from './settings'
export {toConfiguredId, toStandardizedId} from './util/ids'
10 changes: 10 additions & 0 deletions src/util/ids.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
import {Config} from '../interfaces'

export function toStandardizedId(commandID: string, config: Config): string {
return commandID.replaceAll(new RegExp(config.topicSeparator, 'g'), ':')
}

export function toConfiguredId(commandID: string, config: Config): string {
const defaultTopicSeparator = ':'
return commandID.replaceAll(new RegExp(defaultTopicSeparator, 'g'), config.topicSeparator || defaultTopicSeparator)
}
2 changes: 0 additions & 2 deletions test/util/theme.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,6 @@ describe('theme parsing', () => {
flagOptions: '#FFFFFF',
flagRequired: '#FFFFFF',
flagSeparator: '#FFFFFF',
flagType: '#FFFFFF',
sectionDescription: '#FFFFFF',
sectionHeader: '#FFFFFF',
topic: '#FFFFFF',
Expand Down Expand Up @@ -75,7 +74,6 @@ describe('theme parsing', () => {
flagOptions: 'cyan',
flagRequired: 'cyan',
flagSeparator: 'cyan',
flagType: 'cyan',
sectionDescription: 'cyan',
sectionHeader: 'cyan',
topic: 'cyan',
Expand Down

0 comments on commit cd746a6

Please sign in to comment.