Parse only positional arguments after the first is encountered

This commit is contained in:
Jerko Steiner 2019-08-18 08:36:00 +07:00
parent 385555123a
commit b807e84539
3 changed files with 40 additions and 29 deletions

View File

@ -319,7 +319,7 @@ Positional arguments:
b: 'bbb',
})
})
it('works amongs regular arguments', () => {
it('works amongst regular arguments', () => {
const {parse} = argparse({
arg1: {
type: 'string',
@ -332,11 +332,16 @@ Positional arguments:
type: 'string',
},
})
expect(parse([CMD, '--arg1', 'one', '2', '--arg3', 'three'])).toEqual({
expect(parse([CMD, '--arg1', 'one', '--arg3', 'three', '2'])).toEqual({
arg1: 'one',
arg2: 2,
arg3: 'three',
})
expect(parse([CMD, '2'])).toEqual({
arg1: '',
arg2: 2,
arg3: '',
})
})
})

View File

@ -338,9 +338,9 @@ export function argparse<T extends IArgsConfig>(
return lastArgName
}
function getNextPositional(): string {
function getNextPositional(argument: string): string {
const p = positional.shift()
assert(!!p, 'No defined positional arguments')
assert(!!p, 'Unknown positional argument: ' + argument)
return p!
}
@ -352,9 +352,12 @@ export function argparse<T extends IArgsConfig>(
continue
}
const isPositional = argument.substring(0, 1) !== '-' || onlyPositionals
if (isPositional) {
onlyPositionals = true
}
const argName = !isPositional
? processFlags(argument)
: getNextPositional()
: getNextPositional(argument)
const argConfig = config[argName]
if (!isPositional && argName === 'help') {
log(help(command, config))

View File

@ -9,18 +9,21 @@ const commandNames = Object.keys(commands).filter(cmd => !cmd.startsWith('_'))
const {parse} = argparse({
help: arg('boolean', {alias: 'h'}),
debug: arg('boolean'),
command: arg('string[]', {
n: '+',
command: arg('string', {
required: true,
positional: true,
description: '\n ' + commandNames.join('\n '),
choices: commandNames,
}),
args: arg('string[]', {
n: '*',
positional: true,
}),
})
type TArgs = ReturnType<typeof parse>
async function run(args: TArgs, exit: (code: number) => void) {
const commandName = args.command[0]
const commandName = args.command
if (!(commandName in commands)) {
const c = commandNames
log.info(
@ -29,7 +32,7 @@ async function run(args: TArgs, exit: (code: number) => void) {
return
}
const command = (commands as any)[commandName] as TCommand
await command(...args.command)
await command(args.command, ...args.args)
}
async function start(