Add ability to specify command description to argparse
This commit is contained in:
parent
dfed2d8445
commit
ea5872d7c0
@ -159,7 +159,7 @@ describe('argparse', () => {
|
|||||||
type: 'string[]',
|
type: 'string[]',
|
||||||
},
|
},
|
||||||
help: arg('boolean'),
|
help: arg('boolean'),
|
||||||
}, exit, log)
|
}, '', exit, log)
|
||||||
expect(parse([CMD]).value).toEqual([])
|
expect(parse([CMD]).value).toEqual([])
|
||||||
expect(parse([CMD, '--value', 'one']).value).toEqual(['one'])
|
expect(parse([CMD, '--value', 'one']).value).toEqual(['one'])
|
||||||
parse([CMD, '--help'])
|
parse([CMD, '--help'])
|
||||||
@ -182,7 +182,7 @@ describe('argparse', () => {
|
|||||||
alias: 'o',
|
alias: 'o',
|
||||||
},
|
},
|
||||||
help: arg('boolean'),
|
help: arg('boolean'),
|
||||||
}, exit, log)
|
}, '', exit, log)
|
||||||
expect(parse([CMD]).value).toEqual([])
|
expect(parse([CMD]).value).toEqual([])
|
||||||
expect(parse([CMD, '--value', 'a', 'b', '--other', '-o', '3'])).toEqual({
|
expect(parse([CMD, '--value', 'a', 'b', '--other', '-o', '3'])).toEqual({
|
||||||
value: ['a', 'b', '--other'],
|
value: ['a', 'b', '--other'],
|
||||||
@ -204,7 +204,7 @@ describe('argparse', () => {
|
|||||||
value: arg('string[]', {n: '+', required: true}),
|
value: arg('string[]', {n: '+', required: true}),
|
||||||
other: arg('number'),
|
other: arg('number'),
|
||||||
help: arg('boolean'),
|
help: arg('boolean'),
|
||||||
}, exit, log)
|
}, '', exit, log)
|
||||||
expect(() => parse([CMD])).toThrowError(/Missing required args: value/)
|
expect(() => parse([CMD])).toThrowError(/Missing required args: value/)
|
||||||
expect(parse([CMD, '--value', 'a', '--other', '3'])).toEqual({
|
expect(parse([CMD, '--value', 'a', '--other', '3'])).toEqual({
|
||||||
value: ['a', '--other', '3'],
|
value: ['a', '--other', '3'],
|
||||||
@ -232,7 +232,7 @@ describe('argparse', () => {
|
|||||||
value: arg('string[]', {n: '*', required: true, positional: true}),
|
value: arg('string[]', {n: '*', required: true, positional: true}),
|
||||||
other: arg('number'),
|
other: arg('number'),
|
||||||
help: arg('boolean'),
|
help: arg('boolean'),
|
||||||
}, exit, log)
|
}, '', exit, log)
|
||||||
expect(parse([CMD, 'a', 'b']).value).toEqual(['a', 'b'])
|
expect(parse([CMD, 'a', 'b']).value).toEqual(['a', 'b'])
|
||||||
expect(() => parse([CMD, '--other', '3']).value)
|
expect(() => parse([CMD, '--other', '3']).value)
|
||||||
.toThrowError(/Missing.*: value/)
|
.toThrowError(/Missing.*: value/)
|
||||||
@ -272,7 +272,7 @@ describe('argparse', () => {
|
|||||||
type: 'number',
|
type: 'number',
|
||||||
positional: true,
|
positional: true,
|
||||||
},
|
},
|
||||||
}, exit, log)
|
}, '', exit, log)
|
||||||
expect(parse([CMD]).a).toBe(NaN)
|
expect(parse([CMD]).a).toBe(NaN)
|
||||||
expect(parse([CMD, '12']).a).toBe(12)
|
expect(parse([CMD, '12']).a).toBe(12)
|
||||||
parse([CMD, '--help'])
|
parse([CMD, '--help'])
|
||||||
@ -352,13 +352,15 @@ Positional arguments:
|
|||||||
two: arg('number'),
|
two: arg('number'),
|
||||||
three: arg('boolean'),
|
three: arg('boolean'),
|
||||||
help: arg('boolean'),
|
help: arg('boolean'),
|
||||||
}, exit, log)
|
}, 'This command does something', exit, log)
|
||||||
expect(exit.mock.calls.length).toBe(0)
|
expect(exit.mock.calls.length).toBe(0)
|
||||||
parse([CMD, '--help'])
|
parse([CMD, '--help'])
|
||||||
expect(exit.mock.calls.length).toBe(1)
|
expect(exit.mock.calls.length).toBe(1)
|
||||||
expect(log.mock.calls[0][0]).toEqual([
|
expect(log.mock.calls[0][0]).toEqual([
|
||||||
`${CMD} [OPTIONS]`,
|
`${CMD} [OPTIONS]`,
|
||||||
'',
|
'',
|
||||||
|
'This command does something',
|
||||||
|
'',
|
||||||
'Options:',
|
'Options:',
|
||||||
' --one string',
|
' --one string',
|
||||||
' --two number',
|
' --two number',
|
||||||
@ -383,7 +385,7 @@ Positional arguments:
|
|||||||
positional: true,
|
positional: true,
|
||||||
}),
|
}),
|
||||||
help: arg('boolean'),
|
help: arg('boolean'),
|
||||||
}, exit, log)
|
}, '', exit, log)
|
||||||
expect(exit.mock.calls.length).toBe(0)
|
expect(exit.mock.calls.length).toBe(0)
|
||||||
parse([CMD, '--help'])
|
parse([CMD, '--help'])
|
||||||
expect(exit.mock.calls.length).toBe(1)
|
expect(exit.mock.calls.length).toBe(1)
|
||||||
|
|||||||
@ -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) {
|
while (str.length < chars) {
|
||||||
str += ' '
|
str += ' '
|
||||||
}
|
}
|
||||||
return str
|
return str
|
||||||
}
|
}
|
||||||
|
|
||||||
export function help(command: string, config: IArgsConfig) {
|
function help(command: string, config: IArgsConfig, desc: string = '') {
|
||||||
const keys = Object.keys(config)
|
const keys = Object.keys(config)
|
||||||
|
|
||||||
function getArrayHelp(
|
function getArrayHelp(
|
||||||
@ -257,7 +257,7 @@ export function help(command: string, config: IArgsConfig) {
|
|||||||
.filter(k => k.length)
|
.filter(k => k.length)
|
||||||
.join(' ')
|
.join(' ')
|
||||||
|
|
||||||
return [commandHelp, positionalHelp, optionsHelp]
|
return [commandHelp, desc, positionalHelp, optionsHelp]
|
||||||
.filter(h => h.length)
|
.filter(h => h.length)
|
||||||
.join('\n\n')
|
.join('\n\n')
|
||||||
}
|
}
|
||||||
@ -274,6 +274,7 @@ export function arg<T extends TArgTypeName>(
|
|||||||
|
|
||||||
export function argparse<T extends IArgsConfig>(
|
export function argparse<T extends IArgsConfig>(
|
||||||
config: T,
|
config: T,
|
||||||
|
description: string = '',
|
||||||
exit: () => void = () => process.exit(),
|
exit: () => void = () => process.exit(),
|
||||||
/* tslint:disable-next-line */
|
/* tslint:disable-next-line */
|
||||||
log: (message: string) => void = console.log.bind(console),
|
log: (message: string) => void = console.log.bind(console),
|
||||||
@ -360,7 +361,7 @@ export function argparse<T extends IArgsConfig>(
|
|||||||
: getNextPositional(argument)
|
: getNextPositional(argument)
|
||||||
const argConfig = config[argName]
|
const argConfig = config[argName]
|
||||||
if (!isPositional && argName === 'help') {
|
if (!isPositional && argName === 'help') {
|
||||||
log(help(command, config))
|
log(help(command, config, description))
|
||||||
exit()
|
exit()
|
||||||
// should never reach this in real life
|
// should never reach this in real life
|
||||||
return null as any
|
return null as any
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user