Skip to content

Commit

Permalink
feat: prefer choices
Browse files Browse the repository at this point in the history
  • Loading branch information
hongaar committed May 27, 2020
1 parent 1d14d8b commit e7bdc11
Show file tree
Hide file tree
Showing 2 changed files with 60 additions and 21 deletions.
39 changes: 33 additions & 6 deletions examples/types.ts
Original file line number Diff line number Diff line change
@@ -1,20 +1,47 @@
import { program, command } from '../src'

/**
* Keep in mind that argument/option types are not validated at runtime.
* For example, when providing a default value with type boolean, it can be set
* to a string value at runtime.
*/

const string = command('string')
.argument('arg', 'Required string argument')
.option('opt', 'Optional string option', { type: 'string' })
.argument('arg', { description: 'Required string argument' })
.option('opt', { description: 'Optional string option', type: 'string' })
.action((args) => {
console.log('Args are', args)
})

const choices = command('string')
.argument('arg', 'Required string argument')
.option('opt', 'Optional string option', { type: 'string' })
const choices = command('choices')
.argument('arg', {
description: 'Argument with choices',
choices: ['foo', 'bar'] as const,
})
.option('opt', {
description: 'Option with choices',
choices: ['option1', 'option2'] as const,
default: 'option3',
})
.action((args) => {
console.log('Args are', args)
})

const defaultValues = command('default')
.argument('arg', {
description: 'Optional argument with default value',
default: 5,
optional: true,
})
.option('opt', { description: 'Default value', default: true })
.action((args) => {
console.log('Args are', args)
})

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

app.withHelp().runOrRepl()
42 changes: 27 additions & 15 deletions src/baseArg.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,21 +6,33 @@ export interface BaseArgOptions {
prompt?: true | string
}

export type InferArgType<
O extends Options | PositionalOptions,
D = unknown
> = O extends {
variadic: true
type: 'number'
} // Add support for numeric variadic arguments
? Array<number>
: O extends {
variadic: true
} // Add support for string variadic arguments
? Array<string>
: unknown extends InferredOptionType<O> // Allow default type
? D
: InferredOptionType<O>
export type InferArgType<O extends Options | PositionalOptions, F = unknown> =
/**
* Add support for numeric variadic arguments
*/
O extends {
variadic: true
type: 'number'
}
? Array<number>
: /**
* Add support for string variadic arguments
*/
O extends {
variadic: true
}
? Array<string>
: /**
* Prefer choices over default
*/
O extends { choices: ReadonlyArray<infer C> }
? C
: /**
* Allow fallback type
*/
unknown extends InferredOptionType<O>
? F
: InferredOptionType<O>

export class BaseArg {
protected name: string
Expand Down

0 comments on commit e7bdc11

Please sign in to comment.