Add help for positional arguments
This commit is contained in:
parent
a1ac8880d4
commit
87d515d89b
@ -220,6 +220,9 @@ describe('argparse', () => {
|
|||||||
three: arg('boolean'),
|
three: arg('boolean'),
|
||||||
})
|
})
|
||||||
expect(help()).toEqual([
|
expect(help()).toEqual([
|
||||||
|
'[OPTIONS] ',
|
||||||
|
'',
|
||||||
|
'Options:',
|
||||||
' --one string ',
|
' --one string ',
|
||||||
' --two number ',
|
' --two number ',
|
||||||
' --three boolean ',
|
' --three boolean ',
|
||||||
@ -234,12 +237,22 @@ describe('argparse', () => {
|
|||||||
default: 'choice-1',
|
default: 'choice-1',
|
||||||
alias: 'o',
|
alias: 'o',
|
||||||
}),
|
}),
|
||||||
two: arg('number'),
|
two: arg('number', {
|
||||||
|
positional: true,
|
||||||
|
required: true,
|
||||||
|
}),
|
||||||
|
three: arg('number', {
|
||||||
|
positional: true,
|
||||||
|
}),
|
||||||
})
|
})
|
||||||
expect(help()).toEqual([
|
expect(help()).toEqual([
|
||||||
|
'[OPTIONS] TWO [THREE]',
|
||||||
|
'',
|
||||||
|
'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)',
|
||||||
' --two number ',
|
' --two number (required)',
|
||||||
|
' --three number ',
|
||||||
].join('\n'))
|
].join('\n'))
|
||||||
})
|
})
|
||||||
|
|
||||||
|
|||||||
@ -51,9 +51,13 @@ const iterate = <T>(arr: T[]): IIterator<T> => {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
function createError(message: string) {
|
||||||
|
return new Error('Error parsing arguments: ' + message)
|
||||||
|
}
|
||||||
|
|
||||||
function assert(cond: boolean, message: string) {
|
function assert(cond: boolean, message: string) {
|
||||||
if (!cond) {
|
if (!cond) {
|
||||||
throw new Error('Error parsing arguments: ' + message)
|
throw createError(message)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -118,7 +122,20 @@ export function padRight(str: string, chars: number) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
export function help(config: IArgsConfig) {
|
export function help(config: IArgsConfig) {
|
||||||
return Object.keys(config).map(argument => {
|
const keys = Object.keys(config)
|
||||||
|
|
||||||
|
const positionalHelp = [
|
||||||
|
'[OPTIONS]',
|
||||||
|
keys
|
||||||
|
.filter(k => config[k].positional)
|
||||||
|
.map(k => config[k].required
|
||||||
|
? `${k.toUpperCase()}`
|
||||||
|
: `[${k.toUpperCase()}]`,
|
||||||
|
)
|
||||||
|
.join(' '),
|
||||||
|
].join(' ')
|
||||||
|
|
||||||
|
const argsHelp = 'Options:\n' + keys.map(argument => {
|
||||||
const argConfig = config[argument]
|
const argConfig = config[argument]
|
||||||
const {alias, type} = argConfig
|
const {alias, type} = argConfig
|
||||||
const name = alias
|
const name = alias
|
||||||
@ -140,6 +157,10 @@ export function help(config: IArgsConfig) {
|
|||||||
return padRight(name + ' ' + type, 30) + ' ' + description + sample
|
return padRight(name + ' ' + type, 30) + ' ' + description + sample
|
||||||
})
|
})
|
||||||
.join('\n')
|
.join('\n')
|
||||||
|
|
||||||
|
return [positionalHelp, argsHelp]
|
||||||
|
.filter(h => h.length)
|
||||||
|
.join('\n\n')
|
||||||
}
|
}
|
||||||
|
|
||||||
export function arg<T extends TArgTypeName>(
|
export function arg<T extends TArgTypeName>(
|
||||||
|
|||||||
@ -1,6 +1,6 @@
|
|||||||
import * as fs from 'fs'
|
import * as fs from 'fs'
|
||||||
import * as p from 'path'
|
import * as p from 'path'
|
||||||
import {argparse} from '@rondo/argparse'
|
import {argparse, arg} from '@rondo/argparse'
|
||||||
import {findNodeModules} from '../modules'
|
import {findNodeModules} from '../modules'
|
||||||
import {join} from 'path'
|
import {join} from 'path'
|
||||||
import {run} from '../run'
|
import {run} from '../run'
|
||||||
@ -8,22 +8,32 @@ import {run} from '../run'
|
|||||||
const tsc = 'ttsc'
|
const tsc = 'ttsc'
|
||||||
|
|
||||||
export async function build(...argv: string[]) {
|
export async function build(...argv: string[]) {
|
||||||
const {esm, project, watch} = argparse({
|
const {parse, help} = argparse({
|
||||||
project: {
|
project: arg('string', {
|
||||||
type: 'string',
|
|
||||||
alias: 'p',
|
alias: 'p',
|
||||||
default: '.',
|
default: '.',
|
||||||
},
|
description: 'Project to build',
|
||||||
esm: {
|
positional: true,
|
||||||
type: 'boolean',
|
}),
|
||||||
},
|
esm: arg('boolean', {
|
||||||
watch: {
|
description: 'Build project from tsconfig.esm.json',
|
||||||
type: 'boolean',
|
}),
|
||||||
|
watch: arg('boolean', {
|
||||||
alias: 'w',
|
alias: 'w',
|
||||||
},
|
description: 'Watch for changes',
|
||||||
})(argv)
|
}),
|
||||||
const path = esm ? join(project, 'tsconfig.esm.json') : project
|
help: arg('boolean', {
|
||||||
const watchArgs = watch ? ['--watch', '--preserveWatchOutput'] : []
|
alias: 'h',
|
||||||
|
description: 'Print help message',
|
||||||
|
}),
|
||||||
|
})
|
||||||
|
const args = parse(argv)
|
||||||
|
if (args.help) {
|
||||||
|
console.log('Usage: rondo build ' + help())
|
||||||
|
return
|
||||||
|
}
|
||||||
|
const path = args.esm ? join(args.project, 'tsconfig.esm.json') : args.project
|
||||||
|
const watchArgs = args.watch ? ['--watch', '--preserveWatchOutput'] : []
|
||||||
await run(tsc, ['--build', path, ...watchArgs])
|
await run(tsc, ['--build', path, ...watchArgs])
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@ -17,7 +17,7 @@ async function run(...argv: string[]) {
|
|||||||
if (typeof require !== 'undefined' && require.main === module) {
|
if (typeof require !== 'undefined' && require.main === module) {
|
||||||
run(...process.argv.slice(2))
|
run(...process.argv.slice(2))
|
||||||
.catch(err => {
|
.catch(err => {
|
||||||
console.log('> ' + err.stack)
|
console.log('> ' + err.message)
|
||||||
process.exit(1)
|
process.exit(1)
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user