Parse only positional arguments after the first is encountered
This commit is contained in:
parent
385555123a
commit
b807e84539
@ -195,7 +195,7 @@ describe('argparse', () => {
|
|||||||
'',
|
'',
|
||||||
'Options:',
|
'Options:',
|
||||||
' --value [VALUE1 VALUE2 VALUE3]',
|
' --value [VALUE1 VALUE2 VALUE3]',
|
||||||
'-o, --other number',
|
' -o, --other number',
|
||||||
' --help boolean',
|
' --help boolean',
|
||||||
].join('\n'))
|
].join('\n'))
|
||||||
})
|
})
|
||||||
@ -319,7 +319,7 @@ Positional arguments:
|
|||||||
b: 'bbb',
|
b: 'bbb',
|
||||||
})
|
})
|
||||||
})
|
})
|
||||||
it('works amongs regular arguments', () => {
|
it('works amongst regular arguments', () => {
|
||||||
const {parse} = argparse({
|
const {parse} = argparse({
|
||||||
arg1: {
|
arg1: {
|
||||||
type: 'string',
|
type: 'string',
|
||||||
@ -332,11 +332,16 @@ Positional arguments:
|
|||||||
type: 'string',
|
type: 'string',
|
||||||
},
|
},
|
||||||
})
|
})
|
||||||
expect(parse([CMD, '--arg1', 'one', '2', '--arg3', 'three'])).toEqual({
|
expect(parse([CMD, '--arg1', 'one', '--arg3', 'three', '2'])).toEqual({
|
||||||
arg1: 'one',
|
arg1: 'one',
|
||||||
arg2: 2,
|
arg2: 2,
|
||||||
arg3: 'three',
|
arg3: 'three',
|
||||||
})
|
})
|
||||||
|
expect(parse([CMD, '2'])).toEqual({
|
||||||
|
arg1: '',
|
||||||
|
arg2: 2,
|
||||||
|
arg3: '',
|
||||||
|
})
|
||||||
})
|
})
|
||||||
})
|
})
|
||||||
|
|
||||||
@ -390,7 +395,7 @@ Positional arguments:
|
|||||||
' THREE number',
|
' THREE number',
|
||||||
'',
|
'',
|
||||||
'Options:',
|
'Options:',
|
||||||
'-o, --one string first argument ' +
|
' -o, --one string first argument ' +
|
||||||
'(required, default: choice-1, choices: choice-1,choice-2)',
|
'(required, default: choice-1, choices: choice-1,choice-2)',
|
||||||
' --help boolean',
|
' --help boolean',
|
||||||
].join('\n'))
|
].join('\n'))
|
||||||
|
|||||||
@ -231,7 +231,7 @@ export function help(command: string, config: IArgsConfig) {
|
|||||||
const argConfig = config[argument]
|
const argConfig = config[argument]
|
||||||
const {alias, type, required, n} = argConfig
|
const {alias, type, required, n} = argConfig
|
||||||
const name = alias
|
const name = alias
|
||||||
? `-${alias}, --${argument}`
|
? ` -${alias}, --${argument}`
|
||||||
: ` --${argument}`
|
: ` --${argument}`
|
||||||
const description = getDescription(argConfig)
|
const description = getDescription(argConfig)
|
||||||
const argType = getArgType(type, argument, required, n)
|
const argType = getArgType(type, argument, required, n)
|
||||||
@ -338,9 +338,9 @@ export function argparse<T extends IArgsConfig>(
|
|||||||
return lastArgName
|
return lastArgName
|
||||||
}
|
}
|
||||||
|
|
||||||
function getNextPositional(): string {
|
function getNextPositional(argument: string): string {
|
||||||
const p = positional.shift()
|
const p = positional.shift()
|
||||||
assert(!!p, 'No defined positional arguments')
|
assert(!!p, 'Unknown positional argument: ' + argument)
|
||||||
return p!
|
return p!
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -352,9 +352,12 @@ export function argparse<T extends IArgsConfig>(
|
|||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
const isPositional = argument.substring(0, 1) !== '-' || onlyPositionals
|
const isPositional = argument.substring(0, 1) !== '-' || onlyPositionals
|
||||||
|
if (isPositional) {
|
||||||
|
onlyPositionals = true
|
||||||
|
}
|
||||||
const argName = !isPositional
|
const argName = !isPositional
|
||||||
? processFlags(argument)
|
? processFlags(argument)
|
||||||
: getNextPositional()
|
: 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))
|
||||||
|
|||||||
@ -9,18 +9,21 @@ const commandNames = Object.keys(commands).filter(cmd => !cmd.startsWith('_'))
|
|||||||
const {parse} = argparse({
|
const {parse} = argparse({
|
||||||
help: arg('boolean', {alias: 'h'}),
|
help: arg('boolean', {alias: 'h'}),
|
||||||
debug: arg('boolean'),
|
debug: arg('boolean'),
|
||||||
command: arg('string[]', {
|
command: arg('string', {
|
||||||
n: '+',
|
|
||||||
required: true,
|
required: true,
|
||||||
positional: true,
|
positional: true,
|
||||||
description: '\n ' + commandNames.join('\n '),
|
choices: commandNames,
|
||||||
|
}),
|
||||||
|
args: arg('string[]', {
|
||||||
|
n: '*',
|
||||||
|
positional: true,
|
||||||
}),
|
}),
|
||||||
})
|
})
|
||||||
|
|
||||||
type TArgs = ReturnType<typeof parse>
|
type TArgs = ReturnType<typeof parse>
|
||||||
|
|
||||||
async function run(args: TArgs, exit: (code: number) => void) {
|
async function run(args: TArgs, exit: (code: number) => void) {
|
||||||
const commandName = args.command[0]
|
const commandName = args.command
|
||||||
if (!(commandName in commands)) {
|
if (!(commandName in commands)) {
|
||||||
const c = commandNames
|
const c = commandNames
|
||||||
log.info(
|
log.info(
|
||||||
@ -29,7 +32,7 @@ async function run(args: TArgs, exit: (code: number) => void) {
|
|||||||
return
|
return
|
||||||
}
|
}
|
||||||
const command = (commands as any)[commandName] as TCommand
|
const command = (commands as any)[commandName] as TCommand
|
||||||
await command(...args.command)
|
await command(args.command, ...args.args)
|
||||||
}
|
}
|
||||||
|
|
||||||
async function start(
|
async function start(
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user