Skip to content
This repository has been archived by the owner on Aug 22, 2023. It is now read-only.

Commit

Permalink
feat: Add 'helpLabel' flag option (#49)
Browse files Browse the repository at this point in the history
This feature adds a new flag option (helpLabel) which takes the place of the
flag label when displaying flag usage.
  • Loading branch information
childish-sambino authored and RasPhilCo committed Apr 29, 2019
1 parent dc641a0 commit 769ec91
Show file tree
Hide file tree
Showing 3 changed files with 12 additions and 2 deletions.
1 change: 1 addition & 0 deletions src/flags.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ export type IFlagBase<T, I> = {
name: string
char?: AlphabetLowercase | AlphabetUppercase
description?: string
helpLabel?: string
hidden?: boolean
required?: boolean
dependsOn?: string[],
Expand Down
9 changes: 7 additions & 2 deletions src/help.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,13 @@ const m = Deps()
export interface FlagUsageOptions { displayRequired?: boolean }
export function flagUsage(flag: IFlag<any>, options: FlagUsageOptions = {}): [string, string | undefined] {
const label = []
if (flag.char) label.push(`-${flag.char}`)
if (flag.name) label.push(` --${flag.name}`)

if (flag.helpLabel) {
label.push(flag.helpLabel)
} else {
if (flag.char) label.push(`-${flag.char}`)
if (flag.name) label.push(` --${flag.name}`)
}

const usage = flag.type === 'option' ? ` ${flag.name.toUpperCase()}` : ''

Expand Down
4 changes: 4 additions & 0 deletions test/help.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,14 @@ describe('flagUsage', () => {
flags.string({name: 'baz', description: 'baz'}),
flags.string({name: 'bar', char: 'b', description: 'bar'}),
flags.string({name: 'foo', char: 'f', description: 'desc'}),
flags.string({name: 'foo', char: 'f', helpLabel: '-f'}),
flags.boolean({char: 'g', description: 'goo'}),
]
expect(flagUsages(f)).to.deep.equal([
[' -b, --bar BAR', 'bar'],
[' -f, --foo FOO', 'desc'],
[' -f FOO', undefined],
[' -g', 'goo'],
[' --bak BAK', undefined],
[' --baz BAZ', 'baz'],
])
Expand Down

0 comments on commit 769ec91

Please sign in to comment.