Skip to content

Commit

Permalink
refactor: description
Browse files Browse the repository at this point in the history
  • Loading branch information
hongaar committed May 27, 2020
1 parent 4df6090 commit 1d14d8b
Show file tree
Hide file tree
Showing 7 changed files with 43 additions and 65 deletions.
10 changes: 9 additions & 1 deletion examples/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,14 @@ const string = command('string')
console.log('Args are', args)
})

const app = program('All argument and option types').add(string)
const choices = command('string')
.argument('arg', 'Required string argument')
.option('opt', 'Optional string option', { type: 'string' })
.action((args) => {
console.log('Args are', args)
})


const app = program().description('All argument and option types').add(string)

app.withHelp().runOrRepl()
17 changes: 5 additions & 12 deletions src/argument.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,21 +13,17 @@ export interface ArgumentOptions
variadic?: true
}

export function argument(
name: string,
description?: string,
options?: ArgumentOptions
) {
return new Argument(name, description, options)
export function argument(name: string, options?: ArgumentOptions) {
return new Argument(name, options)
}

export const defaultOptions: ArgumentOptions = { type: 'string' }

export class Argument extends BaseArg {
protected options: ArgumentOptions = {}

constructor(name: string, description?: string, options?: ArgumentOptions) {
super(name, description)
constructor(name: string, options?: ArgumentOptions) {
super(name)

this.configure(options || {})
}
Expand Down Expand Up @@ -69,9 +65,6 @@ export class Argument extends BaseArg {
* it. See http://yargs.js.org/docs/#api-positionalkey-opt
*/
toYargs<T>(yargs: Argv<T>) {
return yargs.positional(this.name, {
description: this.description,
...this.options
})
return yargs.positional(this.name, this.options)
}
}
18 changes: 12 additions & 6 deletions src/baseArg.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,12 +24,18 @@ export type InferArgType<

export class BaseArg {
protected name: string
protected description?: string
protected options: ArgumentOptions | OptionOptions = {}

constructor(name: string, description?: string) {
constructor(name: string) {
this.name = name
this.description = description
}

/**
* Set the argument/option description.
*/
public description(description: string) {
this.options.description = description
return this
}

/**
Expand All @@ -45,8 +51,8 @@ export class BaseArg {
getPrompt() {
return typeof this.options.prompt === 'string'
? this.options.prompt
: this.description
? this.description
: this.options.description
? this.options.description
: this.name
}

Expand All @@ -61,7 +67,7 @@ export class BaseArg {
* Returns the argument/option description.
*/
getDescription() {
return this.description
return this.options.description
}

/**
Expand Down
28 changes: 4 additions & 24 deletions src/command.ts
Original file line number Diff line number Diff line change
Expand Up @@ -66,18 +66,8 @@ export class Command<T = {}> {
* Adds a new positional argument to the command.
* This is shorthand for `.add(argument(...))`
*/
argument<K extends string, O extends ArgumentOptions>(
name: K,
descriptionOrOptions?: string | O,
options?: O
) {
// Shift arguments
if (typeof descriptionOrOptions !== 'string') {
options = descriptionOrOptions
descriptionOrOptions = undefined
}

this.add(new Argument(name, descriptionOrOptions, options))
argument<K extends string, O extends ArgumentOptions>(name: K, options?: O) {
this.add(new Argument(name, options))

return (this as unknown) as Command<
T & { [key in K]: InferArgType<O, string> }
Expand All @@ -88,18 +78,8 @@ export class Command<T = {}> {
* Adds a new option to the command.
* This is shorthand for `.add(option(...))`
*/
option<K extends string, O extends OptionOptions>(
name: K,
descriptionOrOptions?: string | O,
options?: O
) {
// Shift arguments
if (typeof descriptionOrOptions !== 'string') {
options = descriptionOrOptions
descriptionOrOptions = undefined
}

this.add(new Option(name, descriptionOrOptions, options))
option<K extends string, O extends OptionOptions>(name: K, options?: O) {
this.add(new Option(name, options))

return (this as unknown) as Command<T & { [key in K]: InferArgType<O> }>
}
Expand Down
9 changes: 3 additions & 6 deletions src/option.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,8 @@ export function option(name: string) {
export class Option extends BaseArg {
protected options: OptionOptions = {}

constructor(name: string, description?: string, options?: OptionOptions) {
super(name, description)
constructor(name: string, options?: OptionOptions) {
super(name)

this.configure(options || {})
}
Expand All @@ -40,9 +40,6 @@ export class Option extends BaseArg {
* it. See http://yargs.js.org/docs/#api-positionalkey-opt
*/
toYargs<T>(yargs: Argv<T>) {
return yargs.option(this.name, {
description: this.description,
...this.options
})
return yargs.option(this.name, this.options)
}
}
22 changes: 8 additions & 14 deletions tests/argument.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,38 +9,32 @@ test('getName', () => {
})

test('getDescription', () => {
expect(argument('test', 'foo').getDescription()).toBe('foo')
expect(argument('test', { description: 'foo' }).getDescription()).toBe('foo')
})

test('getOptions', () => {
expect(argument('test', 'foo', { type: 'number' }).getOptions().type).toBe(
'number'
)
expect(argument('test', { type: 'number' }).getOptions().type).toBe('number')
})

test('required argument', () => {
expect(argument('test', 'test').toCommand()).toBe('<test>')
expect(argument('test').toCommand()).toBe('<test>')
})

test('optional argument', () => {
expect(argument('test', 'test', { optional: true }).toCommand()).toBe(
'[test]'
)
expect(argument('test', { optional: true }).toCommand()).toBe('[test]')
})

test('variadic argument', () => {
expect(argument('test', 'test', { variadic: true }).toCommand()).toBe(
'[test..]'
)
expect(argument('test', { variadic: true }).toCommand()).toBe('[test..]')
})

test('promptable argument', () => {
const arg1 = argument('test', 'foo', { prompt: true })
const arg1 = argument('test', { description: 'foo', prompt: true })
expect(arg1.getPrompt()).toBe('foo')

const arg2 = argument('test', undefined, { prompt: true })
const arg2 = argument('test', { prompt: true })
expect(arg2.getPrompt()).toBe('test')

const arg3 = argument('test', 'foo', { prompt: 'bar' })
const arg3 = argument('test', { prompt: 'bar' })
expect(arg3.getPrompt()).toBe('bar')
})
4 changes: 2 additions & 2 deletions tests/command.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ test('default argument', async () => {
})

test('argument with description', async () => {
const cmd = command('test').argument('foo', 'bar description')
const cmd = command('test').argument('foo').description('bar description')
await program().add(cmd).withHelp().run('test --help')
expect(outputSpy.mock.calls[0][0]).toContain('bar description')
})
Expand All @@ -102,7 +102,7 @@ test('default option', async () => {
})

test('option with description', async () => {
const cmd = command('test').option('foo', 'bar description')
const cmd = command('test').option('foo').description('bar description')
await program().add(cmd).withHelp().run('test --help')
})

Expand Down

0 comments on commit 1d14d8b

Please sign in to comment.