Skip to content
Snippets Groups Projects

Resolve "Add zsh, fish, and bash shell completion helper function generation as well as the related command"

1 file
+ 17
16
Compare changes
  • Side-by-side
  • Inline
@@ -10,7 +10,7 @@ function computeDepth(cmd: Command | undefined): number {
@@ -10,7 +10,7 @@ function computeDepth(cmd: Command | undefined): number {
}
}
// Computes the maximum number of commands until the root is reached
// Computes the maximum number of commands until the root is reached
function computeHeigth(cmd: Command | null): number {
function computeHeight(cmd: Command | null): number {
let height = 0
let height = 0
let tmp = cmd
let tmp = cmd
while (tmp !== null) {
while (tmp !== null) {
@@ -90,29 +90,30 @@ function generateCommandChain(cmd: Command | null): string {
@@ -90,29 +90,30 @@ function generateCommandChain(cmd: Command | null): string {
return data.trimEnd()
return data.trimEnd()
}
}
 
function hasOptions(cmd: Command): boolean {
 
return cmd.options.length > 0
 
}
 
function optionsToString(cmd: Command): string {
function optionsToString(cmd: Command): string {
if (cmd.options.length == 0) {
return ''
}
return cmd.options.filter(opt => !opt.hidden).map(opt => {
return cmd.options.filter(opt => !opt.hidden).map(opt => {
return `${prefix} ${computeHeigth(cmd)} ${generateCommandChain(cmd)}' -a "--${opt.name()}" -d "${opt.description}"`
return `${prefix} ${computeHeight(cmd)} ${generateCommandChain(cmd)}' -a "${opt.short ?? ''} ${opt.long ?? ''}" -d "${opt.description}"`
}).join('\n').concat('\n')
}).join('\n').concat('\n')
}
}
export function writeFishCompletion(root: Command, filename: string) {
export function writeFishCompletion(root: Command, filename: string) {
const commands = flatten(root)
const commands = flatten(root)
let data = fishFunction
const data = fishFunction.concat(
// add commands and subcommands
// add completions for options
commands.forEach(cmd => {
commands.filter(cmd => hasOptions(cmd)).map(cmd => optionsToString(cmd)).filter(str => str != '').join('')
data += optionsToString(cmd)
).concat(
if (!isLeaf(cmd)) {
// add completions for commands and subcommands
data += cmd.commands.map(subCmd => {
commands.filter(
return `${prefix} ${computeHeigth(cmd)} ${generateCommandChain(cmd)}' -a ${subCmd.name()} -d "${subCmd.description()}"`
cmd => !isLeaf(cmd)).map(
}).join('\n').concat('\n')
cmd => cmd.commands.map(subCmd => {
}
return `${prefix} ${computeHeight(cmd)} ${generateCommandChain(cmd)}' -a ${subCmd.name()} -d "${subCmd.description()}"`
})
}).join('\n').concat('\n')).join('')
 
)
writeFileSync(filename, data);
writeFileSync(filename, data);
}
}
Loading