-
Notifications
You must be signed in to change notification settings - Fork 30
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
12 changed files
with
369 additions
and
373 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,21 +1,21 @@ | ||
'use strict'; | ||
'use strict' | ||
|
||
module.exports = function(usage, description, init, aliases) { | ||
if (Array.isArray(init)) { | ||
aliases = init; | ||
init = undefined; | ||
aliases = init | ||
init = undefined | ||
} | ||
if (aliases && Array.isArray(aliases)) { | ||
usage = [].concat([usage], aliases); | ||
usage = [].concat([usage], aliases) | ||
} | ||
|
||
// Register command to global scope | ||
this.details.commands.push({ | ||
usage, | ||
description, | ||
init: typeof init === 'function' ? init : false | ||
}); | ||
}) | ||
|
||
// Allow chaining of .command() | ||
return this; | ||
}; | ||
return this | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,12 +1,12 @@ | ||
'use strict'; | ||
'use strict' | ||
|
||
module.exports = function(usage, description) { | ||
if (typeof usage !== 'string' || typeof description !== 'string') { | ||
throw new TypeError( | ||
'Usage for adding an Example: args.example("usage", "description")' | ||
); | ||
) | ||
} | ||
this.details.examples.push({ usage, description }); | ||
this.details.examples.push({ usage, description }) | ||
|
||
return this; | ||
}; | ||
return this | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,15 +1,15 @@ | ||
'use strict'; | ||
'use strict' | ||
|
||
module.exports = function(list) { | ||
if (list.constructor !== Array) { | ||
throw new Error('Item passed to .examples is not an array'); | ||
throw new Error('Item passed to .examples is not an array') | ||
} | ||
|
||
for (const item of list) { | ||
const usage = item.usage || false; | ||
const description = item.description || false; | ||
this.example(usage, description); | ||
const usage = item.usage || false | ||
const description = item.description || false | ||
this.example(usage, description) | ||
} | ||
|
||
return this; | ||
}; | ||
return this | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,76 +1,76 @@ | ||
'use strict'; | ||
'use strict' | ||
|
||
module.exports = function() { | ||
const name = this.config.name || this.binary.replace('-', ' '); | ||
const firstBig = word => word.charAt(0).toUpperCase() + word.substr(1); | ||
const name = this.config.name || this.binary.replace('-', ' ') | ||
const firstBig = word => word.charAt(0).toUpperCase() + word.substr(1) | ||
|
||
const parts = []; | ||
const parts = [] | ||
|
||
const groups = { | ||
commands: true, | ||
options: true, | ||
examples: true | ||
}; | ||
} | ||
|
||
for (const group in groups) { | ||
if (this.details[group].length > 0) { | ||
continue; | ||
continue | ||
} | ||
|
||
groups[group] = false; | ||
groups[group] = false | ||
} | ||
|
||
const optionHandle = groups.options ? '[options] ' : ''; | ||
const cmdHandle = groups.commands ? '[command]' : ''; | ||
const optionHandle = groups.options ? '[options] ' : '' | ||
const cmdHandle = groups.commands ? '[command]' : '' | ||
const value = | ||
typeof this.config.value === 'string' ? ' ' + this.config.value : ''; | ||
typeof this.config.value === 'string' ? ' ' + this.config.value : '' | ||
|
||
parts.push([ | ||
'', | ||
`Usage: ${this.printMainColor(name)} ${this.printSubColor( | ||
optionHandle + cmdHandle + value | ||
)}`, | ||
'' | ||
]); | ||
]) | ||
|
||
for (const group in groups) { | ||
if (!groups[group]) { | ||
continue; | ||
continue | ||
} | ||
|
||
parts.push(['', firstBig(group) + ':', '', '']); | ||
parts.push(['', firstBig(group) + ':', '', '']) | ||
|
||
if (group === 'examples') { | ||
parts.push(this.generateExamples()); | ||
parts.push(this.generateExamples()) | ||
} else { | ||
parts.push(this.generateDetails(group)); | ||
parts.push(this.generateDetails(group)) | ||
} | ||
|
||
parts.push(['', '']); | ||
parts.push(['', '']) | ||
} | ||
|
||
let output = ''; | ||
let output = '' | ||
|
||
// And finally, merge and output them | ||
for (const part of parts) { | ||
output += part.join('\n '); | ||
output += part.join('\n ') | ||
} | ||
|
||
if (!groups.commands && !groups.options) { | ||
output = 'No sub commands or options available'; | ||
output = 'No sub commands or options available' | ||
} | ||
|
||
const usageFilter = this.config.usageFilter; | ||
const usageFilter = this.config.usageFilter | ||
|
||
// If filter is available, pass usage information through | ||
if (typeof usageFilter === 'function') { | ||
output = usageFilter(output) || output; | ||
output = usageFilter(output) || output | ||
} | ||
|
||
console.log(output); | ||
console.log(output) | ||
|
||
if (this.config.exit && this.config.exit.help) { | ||
// eslint-disable-next-line unicorn/no-process-exit | ||
process.exit(); | ||
process.exit() | ||
} | ||
}; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,65 +1,65 @@ | ||
'use strict'; | ||
'use strict' | ||
|
||
module.exports = function(name, description, defaultValue, init) { | ||
let usage = []; | ||
let usage = [] | ||
|
||
const assignShort = (name, options, short) => { | ||
if (options.find(flagName => flagName.usage[0] === short)) { | ||
short = name.charAt(0).toUpperCase(); | ||
short = name.charAt(0).toUpperCase() | ||
} | ||
return [short, name]; | ||
}; | ||
return [short, name] | ||
} | ||
|
||
// If name is an array, pick the values | ||
// Otherwise just use the whole thing | ||
switch (name.constructor) { | ||
case String: | ||
usage = assignShort(name, this.details.options, name.charAt(0)); | ||
break; | ||
usage = assignShort(name, this.details.options, name.charAt(0)) | ||
break | ||
case Array: | ||
usage = usage.concat(name); | ||
break; | ||
usage = usage.concat(name) | ||
break | ||
default: | ||
throw new Error('Invalid name for option'); | ||
throw new Error('Invalid name for option') | ||
} | ||
|
||
// Throw error if short option is too long | ||
if (usage.length > 0 && usage[0].length > 1) { | ||
throw new Error('Short version of option is longer than 1 char'); | ||
throw new Error('Short version of option is longer than 1 char') | ||
} | ||
|
||
const optionDetails = { | ||
defaultValue, | ||
usage, | ||
description | ||
}; | ||
} | ||
|
||
let defaultIsWrong; | ||
let defaultIsWrong | ||
|
||
switch (defaultValue) { | ||
case false: | ||
defaultIsWrong = true; | ||
break; | ||
defaultIsWrong = true | ||
break | ||
case null: | ||
defaultIsWrong = true; | ||
break; | ||
defaultIsWrong = true | ||
break | ||
case undefined: | ||
defaultIsWrong = true; | ||
break; | ||
defaultIsWrong = true | ||
break | ||
default: | ||
defaultIsWrong = false; | ||
defaultIsWrong = false | ||
} | ||
|
||
if (typeof init === 'function') { | ||
optionDetails.init = init; | ||
optionDetails.init = init | ||
} else if (!defaultIsWrong) { | ||
// Set initializer depending on type of default value | ||
optionDetails.init = this.handleType(defaultValue)[1]; | ||
optionDetails.init = this.handleType(defaultValue)[1] | ||
} | ||
|
||
// Register option to global scope | ||
this.details.options.push(optionDetails); | ||
this.details.options.push(optionDetails) | ||
|
||
// Allow chaining of .option() | ||
return this; | ||
}; | ||
return this | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,16 +1,16 @@ | ||
'use strict'; | ||
'use strict' | ||
|
||
module.exports = function(list) { | ||
if (list.constructor !== Array) { | ||
throw new Error('Item passed to .options is not an array'); | ||
throw new Error('Item passed to .options is not an array') | ||
} | ||
|
||
for (const item of list) { | ||
const preset = item.defaultValue; | ||
const init = item.init || false; | ||
const preset = item.defaultValue | ||
const init = item.init || false | ||
|
||
this.option(item.name, item.description, preset, init); | ||
this.option(item.name, item.description, preset, init) | ||
} | ||
|
||
return this; | ||
}; | ||
return this | ||
} |
Oops, something went wrong.