beautify usage output

feature/linter
Niclas Thobaben 2021-07-07 18:51:32 +02:00
parent e0bf9ed40d
commit f40207195c
4 changed files with 39 additions and 6 deletions

View File

@ -84,5 +84,17 @@ function run(args) {
}
module.exports = {
run
description: 'Generate a new api spec from a template.',
usage: {
options: {
'--verbose, -v': 'Verbose output. Includes debugging information. (optional)',
'--interactive, -i': 'Interactive mode. Generate spec with interactive input from user. (optional)',
'--name': 'Name of the service.',
'--description': 'Description of the service.',
'--namespace': 'Namespace of the service. (optional)',
'--baseurl': 'Base URL of the service. (optional)',
}
},
run,
}

View File

@ -16,7 +16,7 @@ module.exports = {
run,
usage: {
options: {
'-v, --verbose': 'verbose output. Includes name and description.',
'-v, --verbose': 'Verbose output. Includes name and description.',
}
}
}

View File

@ -14,7 +14,6 @@ if(!command) {
command = commands.help
}
try {
command.run(args)
}catch(err) {

View File

@ -1,16 +1,30 @@
const fs = require('fs')
const path = require('path')
function printColumns(left, right) {
let indent = 48 - left.length
let result = left + ' ';
for(let i = 0; i < indent; i++) {
result += '.'
}
result += ' ' + right
return result
}
function printCommandUsage(command) {
console.log(`- <${command.name}> ${command.description || ''}`)
if(command.name === '?' || command.name === 'help') {
return
}
console.log(`\n[${command.name}]\n${command.description || ''}`)
if(command.usage && command.usage.options) {
Object.entries(command.usage.options)
.forEach(entry => console.log(`\t ${entry[0]} | ${entry[1]}`))
.forEach(entry => console.log(`\t ${printColumns(entry[0], entry[1])}`))
}
}
function printCompleteUsage(commands) {
Object.values(commands).forEach(printCommandUsage)
Object.values(commands).sort((a, b) => a.name.localeCompare(b.name)).forEach(printCommandUsage)
}
function load() {
@ -24,6 +38,14 @@ function load() {
command.name = result.name || name
command.printUsage = () => printCommandUsage(command)
let run_command = command.run
command.run = (args) => {
if(args['?'] || args.help) {
printCommandUsage(command)
return
}
run_command(args)
}
result[name] = command
return result