Skip to content

Commit

Permalink
optionList definition names are no longer mandatory
Browse files Browse the repository at this point in the history
  • Loading branch information
75lb committed Nov 3, 2019
1 parent a9a619f commit 141eb65
Show file tree
Hide file tree
Showing 5 changed files with 261 additions and 375 deletions.
22 changes: 22 additions & 0 deletions example/alias-opts-only.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
const commandLineUsage = require('../')

const usage = commandLineUsage([
{
header: 'Options',
optionList: [
{
description: 'Display this usage guide.',
alias: 'h',
type: Boolean
},
{
description: 'Timeout value in ms.',
alias: 't',
type: Number,
typeLabel: '{underline ms}'
}
]
}
])

console.log(usage)
29 changes: 15 additions & 14 deletions lib/section/option-list.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
const Section = require('../section')
const Table = require('table-layout')
const chalkFormat = require('../chalk-format')
const chalk = require('../chalk-format')
const t = require('typical')
const arrayify = require('array-back')

Expand All @@ -18,12 +18,6 @@ class OptionList extends Section {
})
}

/* All definitions must have a name */
const validNames = definitions.every(d => d.name)
if (!validNames) {
throw new Error('Every definition in the optionList must have a `name` property')
}

if (data.header) this.header(data.header)

if (groups.length) {
Expand All @@ -37,7 +31,7 @@ class OptionList extends Section {
const rows = definitions.map(def => {
return {
option: getOptionNames(def, data.reverseNameOrder),
description: chalkFormat(def.description)
description: chalk(def.description)
}
})

Expand All @@ -61,18 +55,25 @@ function getOptionNames (definition, reverseNameOrder) {
if (type) {
type = type === 'boolean' ? '' : `{underline ${type}${multiple}}`
}
// console.error(require('util').inspect(definition.typeLabel || type, { depth: 6, colors: true }))
type = chalkFormat(definition.typeLabel || type)
type = chalk(definition.typeLabel || type)

let result = ''
if (definition.alias) {
if (reverseNameOrder) {
result = `${chalkFormat(`{bold --${definition.name}}`)}, ${chalkFormat(`{bold -${definition.alias}}`)} ${type}`
if (definition.name) {
if (reverseNameOrder) {
result = chalk(`{bold --${definition.name}}, {bold -${definition.alias}} ${type}`)
} else {
result = chalk(`{bold -${definition.alias}}, {bold --${definition.name}} ${type}`)
}
} else {
result = `${chalkFormat(`{bold -${definition.alias}}`)}, ${chalkFormat(`{bold --${definition.name}} ${type}`)}`
if (reverseNameOrder) {
result = chalk(`{bold -${definition.alias}} ${type}`)
} else {
result = chalk(`{bold -${definition.alias}} ${type}`)
}
}
} else {
result = `${chalkFormat(`{bold --${definition.name}}`)} ${type}`
result = chalk(`{bold --${definition.name}} ${type}`)
}
return result
}
Expand Down
Loading

0 comments on commit 141eb65

Please sign in to comment.