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', 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: '',
})
}) })
}) })

View File

@ -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))

View File

@ -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(