Add ability to specify command description to argparse

This commit is contained in:
Jerko Steiner 2019-08-25 11:25:40 +07:00
parent dfed2d8445
commit ea5872d7c0
2 changed files with 14 additions and 11 deletions

View File

@ -159,7 +159,7 @@ describe('argparse', () => {
type: 'string[]',
},
help: arg('boolean'),
}, exit, log)
}, '', exit, log)
expect(parse([CMD]).value).toEqual([])
expect(parse([CMD, '--value', 'one']).value).toEqual(['one'])
parse([CMD, '--help'])
@ -182,7 +182,7 @@ describe('argparse', () => {
alias: 'o',
},
help: arg('boolean'),
}, exit, log)
}, '', exit, log)
expect(parse([CMD]).value).toEqual([])
expect(parse([CMD, '--value', 'a', 'b', '--other', '-o', '3'])).toEqual({
value: ['a', 'b', '--other'],
@ -204,7 +204,7 @@ describe('argparse', () => {
value: arg('string[]', {n: '+', required: true}),
other: arg('number'),
help: arg('boolean'),
}, exit, log)
}, '', exit, log)
expect(() => parse([CMD])).toThrowError(/Missing required args: value/)
expect(parse([CMD, '--value', 'a', '--other', '3'])).toEqual({
value: ['a', '--other', '3'],
@ -232,7 +232,7 @@ describe('argparse', () => {
value: arg('string[]', {n: '*', required: true, positional: true}),
other: arg('number'),
help: arg('boolean'),
}, exit, log)
}, '', exit, log)
expect(parse([CMD, 'a', 'b']).value).toEqual(['a', 'b'])
expect(() => parse([CMD, '--other', '3']).value)
.toThrowError(/Missing.*: value/)
@ -272,7 +272,7 @@ describe('argparse', () => {
type: 'number',
positional: true,
},
}, exit, log)
}, '', exit, log)
expect(parse([CMD]).a).toBe(NaN)
expect(parse([CMD, '12']).a).toBe(12)
parse([CMD, '--help'])
@ -352,13 +352,15 @@ Positional arguments:
two: arg('number'),
three: arg('boolean'),
help: arg('boolean'),
}, exit, log)
}, 'This command does something', exit, log)
expect(exit.mock.calls.length).toBe(0)
parse([CMD, '--help'])
expect(exit.mock.calls.length).toBe(1)
expect(log.mock.calls[0][0]).toEqual([
`${CMD} [OPTIONS]`,
'',
'This command does something',
'',
'Options:',
' --one string',
' --two number',
@ -383,7 +385,7 @@ Positional arguments:
positional: true,
}),
help: arg('boolean'),
}, exit, log)
}, '', exit, log)
expect(exit.mock.calls.length).toBe(0)
parse([CMD, '--help'])
expect(exit.mock.calls.length).toBe(1)

View File

@ -150,14 +150,14 @@ function checkChoice<T>(argument: string, choice: T, choices?: T[]) {
}
}
export function padRight(str: string, chars: number) {
function padRight(str: string, chars: number) {
while (str.length < chars) {
str += ' '
}
return str
}
export function help(command: string, config: IArgsConfig) {
function help(command: string, config: IArgsConfig, desc: string = '') {
const keys = Object.keys(config)
function getArrayHelp(
@ -257,7 +257,7 @@ export function help(command: string, config: IArgsConfig) {
.filter(k => k.length)
.join(' ')
return [commandHelp, positionalHelp, optionsHelp]
return [commandHelp, desc, positionalHelp, optionsHelp]
.filter(h => h.length)
.join('\n\n')
}
@ -274,6 +274,7 @@ export function arg<T extends TArgTypeName>(
export function argparse<T extends IArgsConfig>(
config: T,
description: string = '',
exit: () => void = () => process.exit(),
/* tslint:disable-next-line */
log: (message: string) => void = console.log.bind(console),
@ -360,7 +361,7 @@ export function argparse<T extends IArgsConfig>(
: getNextPositional(argument)
const argConfig = config[argName]
if (!isPositional && argName === 'help') {
log(help(command, config))
log(help(command, config, description))
exit()
// should never reach this in real life
return null as any