Remove ESBuild experiment. Add --install option for bash completions. Move shell scripts to separate files for auditability. Fix template inspect command autocomplete and output formatting
This commit is contained in:
@@ -8,12 +8,13 @@
|
||||
* Usage: xo-complete <context> [args...]
|
||||
*
|
||||
* Contexts:
|
||||
* mnemonics - List mnemonic file names
|
||||
* templates - List template names/IDs
|
||||
* actions <template> - List actions for a template
|
||||
* invitations - List invitation IDs
|
||||
* resources - List UTXO outpoints (txhash:vout)
|
||||
* subcommands <command> - List subcommands for a top-level command
|
||||
* mnemonics - List mnemonic file names
|
||||
* templates - List template names/IDs
|
||||
* actions <template> - List actions for a template
|
||||
* fields <category> <template> - List fields for a template category
|
||||
* invitations - List invitation IDs
|
||||
* resources - List UTXO outpoints (txhash:vout)
|
||||
* subcommands <command> - List subcommands for a top-level command
|
||||
*
|
||||
* Output: One completion suggestion per line, suitable for shell completion.
|
||||
*
|
||||
@@ -150,6 +151,33 @@ async function listTemplates(prefix?: string): Promise<void> {
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Resolves a template by name or ID.
|
||||
*/
|
||||
async function resolveTemplate(
|
||||
engine: Awaited<ReturnType<Awaited<ReturnType<typeof getOfflineEngineModule>>["tryCreateOfflineEngine"]>>,
|
||||
templateQuery: string,
|
||||
) {
|
||||
if (!engine) return null;
|
||||
|
||||
const { generateTemplateIdentifier } = await getEngineModule();
|
||||
const templates = await engine.listImportedTemplates();
|
||||
|
||||
// Try exact match on name or ID
|
||||
let template = templates.find(
|
||||
(t) => t.name === templateQuery || generateTemplateIdentifier(t) === templateQuery,
|
||||
);
|
||||
|
||||
// Try partial match on name
|
||||
if (!template) {
|
||||
template = templates.find((t) =>
|
||||
t.name?.toLowerCase().includes(templateQuery.toLowerCase()),
|
||||
);
|
||||
}
|
||||
|
||||
return template ?? null;
|
||||
}
|
||||
|
||||
/**
|
||||
* Lists actions for a specific template.
|
||||
*/
|
||||
@@ -158,7 +186,6 @@ async function listActions(templateQuery: string, prefix?: string): Promise<void
|
||||
if (!mnemonic) return;
|
||||
|
||||
const { tryCreateOfflineEngine } = await getOfflineEngineModule();
|
||||
const { generateTemplateIdentifier } = await getEngineModule();
|
||||
|
||||
const engine = await tryCreateOfflineEngine(mnemonic, {
|
||||
databasePath: getDataDir(),
|
||||
@@ -168,18 +195,7 @@ async function listActions(templateQuery: string, prefix?: string): Promise<void
|
||||
if (!engine) return;
|
||||
|
||||
try {
|
||||
// Try to resolve the template by name or ID
|
||||
const templates = await engine.listImportedTemplates();
|
||||
let template = templates.find(
|
||||
(t) => t.name === templateQuery || generateTemplateIdentifier(t) === templateQuery,
|
||||
);
|
||||
|
||||
if (!template) {
|
||||
// Try partial match on name
|
||||
template = templates.find((t) =>
|
||||
t.name?.toLowerCase().includes(templateQuery.toLowerCase()),
|
||||
);
|
||||
}
|
||||
const template = await resolveTemplate(engine, templateQuery);
|
||||
|
||||
if (template && template.actions) {
|
||||
const actions = Object.keys(template.actions);
|
||||
@@ -190,6 +206,53 @@ async function listActions(templateQuery: string, prefix?: string): Promise<void
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Lists fields (actions, transactions, outputs, etc.) for a specific template category.
|
||||
* Used for completing the 3rd argument of `template inspect <category> <template> <field>`.
|
||||
*/
|
||||
async function listFields(category: string, templateQuery: string, prefix?: string): Promise<void> {
|
||||
const mnemonic = getCurrentMnemonic();
|
||||
if (!mnemonic) return;
|
||||
|
||||
const { tryCreateOfflineEngine } = await getOfflineEngineModule();
|
||||
|
||||
const engine = await tryCreateOfflineEngine(mnemonic, {
|
||||
databasePath: getDataDir(),
|
||||
databaseFilename: "xo-wallet.db",
|
||||
});
|
||||
|
||||
if (!engine) return;
|
||||
|
||||
try {
|
||||
const template = await resolveTemplate(engine, templateQuery);
|
||||
if (!template) return;
|
||||
|
||||
let fields: string[] = [];
|
||||
|
||||
switch (category) {
|
||||
case "action":
|
||||
fields = Object.keys(template.actions || {});
|
||||
break;
|
||||
case "transaction":
|
||||
fields = Object.keys(template.transactions || {});
|
||||
break;
|
||||
case "output":
|
||||
fields = Object.keys(template.outputs || {});
|
||||
break;
|
||||
case "lockingscript":
|
||||
fields = Object.keys(template.lockingScripts || {});
|
||||
break;
|
||||
case "variable":
|
||||
fields = Object.keys(template.variables || {});
|
||||
break;
|
||||
}
|
||||
|
||||
outputCompletions(fields, prefix);
|
||||
} finally {
|
||||
await engine.stop();
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Lists invitation IDs from the invitation storage.
|
||||
*/
|
||||
@@ -278,6 +341,13 @@ async function main(): Promise<void> {
|
||||
}
|
||||
break;
|
||||
|
||||
case "fields":
|
||||
// fields <category> <template> [prefix]
|
||||
if (arg1 && arg2) {
|
||||
await listFields(arg1, arg2, process.argv[5]);
|
||||
}
|
||||
break;
|
||||
|
||||
case "invitations":
|
||||
await listInvitations(arg1);
|
||||
break;
|
||||
|
||||
@@ -1,9 +1,9 @@
|
||||
/**
|
||||
* Shell completion script generation.
|
||||
*
|
||||
* Defines the CLI command tree in one place and generates
|
||||
* bash/zsh/fish completion scripts from it. Users source the output
|
||||
* in their shell profile for tab-completion support.
|
||||
* Loads shell-native template files and replaces placeholders with
|
||||
* dynamic values. This approach keeps the shell scripts readable
|
||||
* and auditable in their native format.
|
||||
*
|
||||
* The generated scripts use the `xo-complete` helper binary for dynamic
|
||||
* completions (invitation IDs, template names, resources, etc.).
|
||||
@@ -12,8 +12,18 @@
|
||||
* eval "$(xo-cli completions bash)"
|
||||
* eval "$(xo-cli completions zsh)"
|
||||
* xo-cli completions fish | source
|
||||
*
|
||||
* Install to shell config:
|
||||
* xo-cli completions bash --install
|
||||
* xo-cli completions zsh --install
|
||||
* xo-cli completions fish --install
|
||||
*/
|
||||
|
||||
import { existsSync, readFileSync, appendFileSync, writeFileSync } from "node:fs";
|
||||
import { dirname, join } from "node:path";
|
||||
import { fileURLToPath } from "node:url";
|
||||
import { homedir } from "node:os";
|
||||
|
||||
/**
|
||||
* Single source of truth for the CLI command tree.
|
||||
* Each top-level key is a command, and its value is an array of sub-commands.
|
||||
@@ -50,490 +60,96 @@ export const COMMAND_TREE = {
|
||||
const GLOBAL_OPTIONS = ["-h", "--help", "-v", "--verbose", "-m", "--mnemonic-file", "-o", "--output"];
|
||||
|
||||
/**
|
||||
* Generates a bash completion script with dynamic completion support.
|
||||
* @param binName - The name of the CLI binary (used in the `complete` registration).
|
||||
* Gets the path to the scripts directory containing shell templates.
|
||||
*/
|
||||
export function generateBashCompletions(binName: string): string {
|
||||
const commands = Object.keys(COMMAND_TREE).join(" ");
|
||||
const options = GLOBAL_OPTIONS.join(" ");
|
||||
const funcName = binName.replace(/-/g, "_");
|
||||
|
||||
return `# bash completion for ${binName}
|
||||
# Add to ~/.bashrc: eval "$(${binName} completions bash)"
|
||||
|
||||
# Find xo-complete in the same directory as xo-cli
|
||||
__xo_complete_bin=""
|
||||
if command -v xo-complete &>/dev/null; then
|
||||
__xo_complete_bin="xo-complete"
|
||||
elif command -v ${binName} &>/dev/null; then
|
||||
__xo_complete_bin="$(dirname "$(command -v ${binName})")/xo-complete"
|
||||
fi
|
||||
|
||||
# Wrapper to call xo-complete helper
|
||||
__xo_complete() {
|
||||
[[ -n "\${__xo_complete_bin}" ]] && "\${__xo_complete_bin}" "$@" 2>/dev/null
|
||||
}
|
||||
|
||||
_${funcName}_completions() {
|
||||
local cur prev words cword
|
||||
_init_completion || return
|
||||
|
||||
# Handle -m/--mnemonic-file argument (previous word was -m)
|
||||
if [[ "\${prev}" == "-m" || "\${prev}" == "--mnemonic-file" ]]; then
|
||||
local mnemonics
|
||||
mnemonics=$(__xo_complete mnemonics "\${cur}")
|
||||
if [[ -n "\${mnemonics}" ]]; then
|
||||
while IFS= read -r line; do
|
||||
COMPREPLY+=("\$line")
|
||||
done <<< "\${mnemonics}"
|
||||
return 0
|
||||
fi
|
||||
fi
|
||||
|
||||
# If the current word starts with "-", offer option flags
|
||||
if [[ "\${cur}" == -* ]]; then
|
||||
COMPREPLY=($(compgen -W "${options}" -- "\${cur}"))
|
||||
return 0
|
||||
fi
|
||||
|
||||
# Find the command and subcommand positions
|
||||
local cmd="" subcmd="" cmd_idx=0 subcmd_idx=0
|
||||
for ((i=1; i < cword; i++)); do
|
||||
if [[ "\${words[i]}" != -* ]]; then
|
||||
if [[ -z "\${cmd}" ]]; then
|
||||
cmd="\${words[i]}"
|
||||
cmd_idx=\$i
|
||||
else
|
||||
subcmd="\${words[i]}"
|
||||
subcmd_idx=\$i
|
||||
break
|
||||
fi
|
||||
fi
|
||||
done
|
||||
|
||||
# No command yet — offer the top-level commands
|
||||
if [[ -z "\${cmd}" ]]; then
|
||||
COMPREPLY=($(compgen -W "${commands}" -- "\${cur}"))
|
||||
return 0
|
||||
fi
|
||||
|
||||
# Handle each command's completion
|
||||
case "\${cmd}" in
|
||||
mnemonic)
|
||||
if [[ -z "\${subcmd}" ]]; then
|
||||
COMPREPLY=($(compgen -W "${MNEMONIC_SUBS.join(" ")}" -- "\${cur}"))
|
||||
fi
|
||||
;;
|
||||
|
||||
template)
|
||||
if [[ -z "\${subcmd}" ]]; then
|
||||
COMPREPLY=($(compgen -W "${TEMPLATE_SUBS.join(" ")}" -- "\${cur}"))
|
||||
elif [[ "\${subcmd}" == "list" || "\${subcmd}" == "inspect" ]]; then
|
||||
# template list/inspect <category> <template> [field] - category first, then template
|
||||
local pos=$((cword - subcmd_idx))
|
||||
if [[ \$pos -eq 1 ]]; then
|
||||
COMPREPLY=($(compgen -W "action transaction output lockingscript variable" -- "\${cur}"))
|
||||
elif [[ \$pos -eq 2 ]]; then
|
||||
local templates
|
||||
templates=$(__xo_complete templates "\${cur}")
|
||||
if [[ -n "\${templates}" ]]; then
|
||||
while IFS= read -r line; do
|
||||
COMPREPLY+=("\$line")
|
||||
done <<< "\${templates}"
|
||||
fi
|
||||
fi
|
||||
elif [[ "\${subcmd}" == "set-default" ]]; then
|
||||
# template set-default <template> <output> <role> - template first
|
||||
local pos=$((cword - subcmd_idx))
|
||||
if [[ \$pos -eq 1 ]]; then
|
||||
local templates
|
||||
templates=$(__xo_complete templates "\${cur}")
|
||||
if [[ -n "\${templates}" ]]; then
|
||||
while IFS= read -r line; do
|
||||
COMPREPLY+=("\$line")
|
||||
done <<< "\${templates}"
|
||||
fi
|
||||
fi
|
||||
fi
|
||||
;;
|
||||
|
||||
invitation)
|
||||
if [[ -z "\${subcmd}" ]]; then
|
||||
COMPREPLY=($(compgen -W "${INVITATION_SUBS.join(" ")}" -- "\${cur}"))
|
||||
else
|
||||
case "\${subcmd}" in
|
||||
create)
|
||||
# invitation create <template> <action> - offer templates then actions
|
||||
local pos=$((cword - subcmd_idx))
|
||||
if [[ \$pos -eq 1 ]]; then
|
||||
local templates
|
||||
templates=$(__xo_complete templates "\${cur}")
|
||||
if [[ -n "\${templates}" ]]; then
|
||||
while IFS= read -r line; do
|
||||
COMPREPLY+=("\$line")
|
||||
done <<< "\${templates}"
|
||||
fi
|
||||
elif [[ \$pos -eq 2 ]]; then
|
||||
local template_arg="\${words[subcmd_idx + 1]}"
|
||||
local actions
|
||||
actions=$(__xo_complete actions "\${template_arg}" "\${cur}")
|
||||
if [[ -n "\${actions}" ]]; then
|
||||
while IFS= read -r line; do
|
||||
COMPREPLY+=("\$line")
|
||||
done <<< "\${actions}"
|
||||
fi
|
||||
fi
|
||||
;;
|
||||
append|sign|broadcast|requirements|inspect)
|
||||
# These take an invitation ID
|
||||
local pos=$((cword - subcmd_idx))
|
||||
if [[ \$pos -eq 1 ]]; then
|
||||
local invitations
|
||||
invitations=$(__xo_complete invitations "\${cur}")
|
||||
if [[ -n "\${invitations}" ]]; then
|
||||
while IFS= read -r line; do
|
||||
COMPREPLY+=("\$line")
|
||||
done <<< "\${invitations}"
|
||||
fi
|
||||
fi
|
||||
;;
|
||||
import)
|
||||
# import takes a file path - use default file completion
|
||||
COMPREPLY=($(compgen -f -- "\${cur}"))
|
||||
;;
|
||||
esac
|
||||
fi
|
||||
;;
|
||||
|
||||
resource)
|
||||
if [[ -z "\${subcmd}" ]]; then
|
||||
COMPREPLY=($(compgen -W "${RESOURCE_SUBS.join(" ")}" -- "\${cur}"))
|
||||
elif [[ "\${subcmd}" == "unreserve" ]]; then
|
||||
# resource unreserve <txhash:vout> - offer resources
|
||||
local pos=$((cword - subcmd_idx))
|
||||
if [[ \$pos -eq 1 ]]; then
|
||||
local resources
|
||||
resources=$(__xo_complete resources "\${cur}")
|
||||
if [[ -n "\${resources}" ]]; then
|
||||
while IFS= read -r line; do
|
||||
COMPREPLY+=("\$line")
|
||||
done <<< "\${resources}"
|
||||
fi
|
||||
fi
|
||||
fi
|
||||
;;
|
||||
|
||||
receive)
|
||||
# receive <template> [output] - offer templates
|
||||
local pos=$((cword - cmd_idx))
|
||||
if [[ \$pos -eq 1 ]]; then
|
||||
local templates
|
||||
templates=$(__xo_complete templates "\${cur}")
|
||||
if [[ -n "\${templates}" ]]; then
|
||||
while IFS= read -r line; do
|
||||
COMPREPLY+=("\$line")
|
||||
done <<< "\${templates}"
|
||||
fi
|
||||
fi
|
||||
;;
|
||||
|
||||
completions)
|
||||
if [[ -z "\${subcmd}" ]]; then
|
||||
COMPREPLY=($(compgen -W "bash zsh fish" -- "\${cur}"))
|
||||
fi
|
||||
;;
|
||||
esac
|
||||
}
|
||||
|
||||
complete -F _${funcName}_completions ${binName}
|
||||
`;
|
||||
function getScriptsDir(): string {
|
||||
const currentFile = fileURLToPath(import.meta.url);
|
||||
return join(dirname(currentFile), "scripts");
|
||||
}
|
||||
|
||||
/**
|
||||
* Generates a zsh completion script with dynamic completion support.
|
||||
* @param binName - The name of the CLI binary.
|
||||
* Loads a shell template file and replaces placeholders.
|
||||
* @param templateName - The template file name (e.g., "bash.sh")
|
||||
* @param binName - The CLI binary name
|
||||
*/
|
||||
export function generateZshCompletions(binName: string): string {
|
||||
const commands = Object.keys(COMMAND_TREE).join(" ");
|
||||
const options = GLOBAL_OPTIONS.join(" ");
|
||||
const funcName = binName.replace(/-/g, "_");
|
||||
function loadAndProcessTemplate(templateName: string, binName: string): string {
|
||||
const scriptsDir = getScriptsDir();
|
||||
const templatePath = join(scriptsDir, templateName);
|
||||
|
||||
return `# zsh completion for ${binName}
|
||||
# Add to ~/.zshrc: eval "$(${binName} completions zsh)"
|
||||
|
||||
# Find xo-complete in the same directory as xo-cli
|
||||
__xo_complete_bin=""
|
||||
if (( \$+commands[xo-complete] )); then
|
||||
__xo_complete_bin="xo-complete"
|
||||
elif (( \$+commands[${binName}] )); then
|
||||
__xo_complete_bin="\${commands[${binName}]:h}/xo-complete"
|
||||
fi
|
||||
|
||||
# Wrapper to call xo-complete helper
|
||||
__xo_complete() {
|
||||
[[ -n "\${__xo_complete_bin}" ]] && "\${__xo_complete_bin}" "$@" 2>/dev/null
|
||||
}
|
||||
|
||||
_${funcName}_completions() {
|
||||
local -a commands
|
||||
commands=(${commands})
|
||||
|
||||
# Handle -m/--mnemonic-file argument (previous word was -m)
|
||||
if [[ "\${words[CURRENT-1]}" == "-m" || "\${words[CURRENT-1]}" == "--mnemonic-file" ]]; then
|
||||
local mnemonics
|
||||
mnemonics=("\${(@f)$(__xo_complete mnemonics "\${words[CURRENT]}")}")
|
||||
if [[ \${#mnemonics[@]} -gt 0 ]]; then
|
||||
compadd -- "\${mnemonics[@]}"
|
||||
return
|
||||
fi
|
||||
fi
|
||||
|
||||
# If typing an option flag, complete options
|
||||
if [[ "\${words[\${CURRENT}]}" == -* ]]; then
|
||||
compadd -- ${options}
|
||||
return
|
||||
fi
|
||||
|
||||
# Find the command and subcommand
|
||||
local cmd="" subcmd="" cmd_idx=0 subcmd_idx=0
|
||||
for ((i=2; i < CURRENT; i++)); do
|
||||
if [[ "\${words[i]}" != -* ]]; then
|
||||
if [[ -z "\${cmd}" ]]; then
|
||||
cmd="\${words[i]}"
|
||||
cmd_idx=\$i
|
||||
else
|
||||
subcmd="\${words[i]}"
|
||||
subcmd_idx=\$i
|
||||
break
|
||||
fi
|
||||
fi
|
||||
done
|
||||
|
||||
# No command yet — offer top-level commands
|
||||
if [[ -z "\${cmd}" ]]; then
|
||||
compadd -- \${commands[@]}
|
||||
return
|
||||
fi
|
||||
|
||||
# Handle each command's completion
|
||||
case "\${cmd}" in
|
||||
mnemonic)
|
||||
if [[ -z "\${subcmd}" ]]; then
|
||||
compadd -- ${MNEMONIC_SUBS.join(" ")}
|
||||
fi
|
||||
;;
|
||||
|
||||
template)
|
||||
if [[ -z "\${subcmd}" ]]; then
|
||||
compadd -- ${TEMPLATE_SUBS.join(" ")}
|
||||
elif [[ "\${subcmd}" == "list" || "\${subcmd}" == "inspect" ]]; then
|
||||
# template list/inspect <category> <template> - category first
|
||||
local pos=$((CURRENT - subcmd_idx))
|
||||
if [[ \$pos -eq 1 ]]; then
|
||||
compadd -- action transaction output lockingscript variable
|
||||
elif [[ \$pos -eq 2 ]]; then
|
||||
local templates
|
||||
templates=("\${(@f)$(__xo_complete templates "\${words[CURRENT]}")}")
|
||||
if [[ \${#templates[@]} -gt 0 ]]; then
|
||||
compadd -- "\${templates[@]}"
|
||||
fi
|
||||
fi
|
||||
elif [[ "\${subcmd}" == "set-default" ]]; then
|
||||
# template set-default <template> <output> <role> - template first
|
||||
local pos=$((CURRENT - subcmd_idx))
|
||||
if [[ \$pos -eq 1 ]]; then
|
||||
local templates
|
||||
templates=("\${(@f)$(__xo_complete templates "\${words[CURRENT]}")}")
|
||||
if [[ \${#templates[@]} -gt 0 ]]; then
|
||||
compadd -- "\${templates[@]}"
|
||||
fi
|
||||
fi
|
||||
fi
|
||||
;;
|
||||
|
||||
invitation)
|
||||
if [[ -z "\${subcmd}" ]]; then
|
||||
compadd -- ${INVITATION_SUBS.join(" ")}
|
||||
else
|
||||
case "\${subcmd}" in
|
||||
create)
|
||||
local pos=$((CURRENT - subcmd_idx))
|
||||
if [[ \$pos -eq 1 ]]; then
|
||||
local templates
|
||||
templates=("\${(@f)$(__xo_complete templates "\${words[CURRENT]}")}")
|
||||
if [[ \${#templates[@]} -gt 0 ]]; then
|
||||
compadd -- "\${templates[@]}"
|
||||
fi
|
||||
elif [[ \$pos -eq 2 ]]; then
|
||||
local template_arg="\${words[subcmd_idx + 1]}"
|
||||
local actions
|
||||
actions=("\${(@f)$(__xo_complete actions "\${template_arg}" "\${words[CURRENT]}")}")
|
||||
if [[ \${#actions[@]} -gt 0 ]]; then
|
||||
compadd -- "\${actions[@]}"
|
||||
fi
|
||||
fi
|
||||
;;
|
||||
append|sign|broadcast|requirements|inspect)
|
||||
local pos=$((CURRENT - subcmd_idx))
|
||||
if [[ \$pos -eq 1 ]]; then
|
||||
local invitations
|
||||
invitations=("\${(@f)$(__xo_complete invitations "\${words[CURRENT]}")}")
|
||||
if [[ \${#invitations[@]} -gt 0 ]]; then
|
||||
compadd -- "\${invitations[@]}"
|
||||
fi
|
||||
fi
|
||||
;;
|
||||
import)
|
||||
_files
|
||||
;;
|
||||
esac
|
||||
fi
|
||||
;;
|
||||
|
||||
resource)
|
||||
if [[ -z "\${subcmd}" ]]; then
|
||||
compadd -- ${RESOURCE_SUBS.join(" ")}
|
||||
elif [[ "\${subcmd}" == "unreserve" ]]; then
|
||||
local pos=$((CURRENT - subcmd_idx))
|
||||
if [[ \$pos -eq 1 ]]; then
|
||||
local resources
|
||||
resources=("\${(@f)$(__xo_complete resources "\${words[CURRENT]}")}")
|
||||
if [[ \${#resources[@]} -gt 0 ]]; then
|
||||
compadd -- "\${resources[@]}"
|
||||
fi
|
||||
fi
|
||||
fi
|
||||
;;
|
||||
|
||||
receive)
|
||||
local pos=$((CURRENT - cmd_idx))
|
||||
if [[ \$pos -eq 1 ]]; then
|
||||
local templates
|
||||
templates=("\${(@f)$(__xo_complete templates "\${words[CURRENT]}")}")
|
||||
if [[ \${#templates[@]} -gt 0 ]]; then
|
||||
compadd -- "\${templates[@]}"
|
||||
fi
|
||||
fi
|
||||
;;
|
||||
|
||||
completions)
|
||||
if [[ -z "\${subcmd}" ]]; then
|
||||
compadd -- bash zsh fish
|
||||
fi
|
||||
;;
|
||||
esac
|
||||
}
|
||||
|
||||
compdef _${funcName}_completions ${binName}
|
||||
`;
|
||||
}
|
||||
|
||||
/**
|
||||
* Generates a fish completion script with dynamic completion support.
|
||||
* @param binName - The name of the CLI binary.
|
||||
*/
|
||||
export function generateFishCompletions(binName: string): string {
|
||||
const lines: string[] = [
|
||||
`# fish completion for ${binName}`,
|
||||
`# Add to fish config: ${binName} completions fish | source`,
|
||||
"",
|
||||
`# Disable file completions by default`,
|
||||
`complete -c ${binName} -f`,
|
||||
"",
|
||||
`# Helper function to get dynamic completions`,
|
||||
`# Finds xo-complete in the same directory as ${binName}`,
|
||||
`function __${binName.replace(/-/g, "_")}_complete_dynamic`,
|
||||
` set -l xo_complete_bin ""`,
|
||||
` if command -q xo-complete`,
|
||||
` set xo_complete_bin xo-complete`,
|
||||
` else if command -q ${binName}`,
|
||||
` set xo_complete_bin (dirname (command -s ${binName}))/xo-complete`,
|
||||
` end`,
|
||||
` if test -n "$xo_complete_bin"`,
|
||||
` $xo_complete_bin $argv 2>/dev/null`,
|
||||
` end`,
|
||||
`end`,
|
||||
"",
|
||||
];
|
||||
|
||||
// Global options
|
||||
for (const opt of GLOBAL_OPTIONS) {
|
||||
const isShort = !opt.startsWith("--");
|
||||
const flag = opt.replace(/^-+/, "");
|
||||
if (isShort) {
|
||||
lines.push(`complete -c ${binName} -s ${flag} -d "Option flag"`);
|
||||
} else {
|
||||
lines.push(`complete -c ${binName} -l ${flag} -d "Option flag"`);
|
||||
}
|
||||
if (!existsSync(templatePath)) {
|
||||
throw new Error(`Template file not found: ${templatePath}`);
|
||||
}
|
||||
|
||||
// Mnemonic file completion for -m
|
||||
lines.push("");
|
||||
lines.push(`# Dynamic mnemonic file completion for -m`);
|
||||
lines.push(`complete -c ${binName} -s m -l mnemonic-file -xa '(__${binName.replace(/-/g, "_")}_complete_dynamic mnemonics)'`);
|
||||
lines.push("");
|
||||
let content = readFileSync(templatePath, "utf8");
|
||||
const funcName = binName.replace(/-/g, "_");
|
||||
const commands = Object.keys(COMMAND_TREE).join(" ");
|
||||
const options = GLOBAL_OPTIONS.join(" ");
|
||||
|
||||
// Top-level commands (only when no sub-command is given yet)
|
||||
lines.push(`# Top-level commands`);
|
||||
const commandNames = Object.keys(COMMAND_TREE);
|
||||
for (const cmd of commandNames) {
|
||||
// Replace all placeholders
|
||||
content = content.replace(/\{\{BIN_NAME\}\}/g, binName);
|
||||
content = content.replace(/\{\{FUNC_NAME\}\}/g, funcName);
|
||||
content = content.replace(/\{\{COMMANDS\}\}/g, commands);
|
||||
content = content.replace(/\{\{OPTIONS\}\}/g, options);
|
||||
content = content.replace(/\{\{MNEMONIC_SUBS\}\}/g, MNEMONIC_SUBS.join(" "));
|
||||
content = content.replace(/\{\{TEMPLATE_SUBS\}\}/g, TEMPLATE_SUBS.join(" "));
|
||||
content = content.replace(/\{\{INVITATION_SUBS\}\}/g, INVITATION_SUBS.join(" "));
|
||||
content = content.replace(/\{\{RESOURCE_SUBS\}\}/g, RESOURCE_SUBS.join(" "));
|
||||
|
||||
// Fish-specific placeholders
|
||||
if (templateName.endsWith(".fish")) {
|
||||
content = content.replace(/\{\{TOP_LEVEL_COMMANDS\}\}/g, generateFishTopLevelCommands(binName));
|
||||
content = content.replace(/\{\{STATIC_SUBCOMMANDS\}\}/g, generateFishStaticSubcommands(binName));
|
||||
}
|
||||
|
||||
return content;
|
||||
}
|
||||
|
||||
/**
|
||||
* Generates fish top-level command completions.
|
||||
*/
|
||||
function generateFishTopLevelCommands(binName: string): string {
|
||||
const lines: string[] = [];
|
||||
for (const cmd of Object.keys(COMMAND_TREE)) {
|
||||
lines.push(`complete -c ${binName} -n "__fish_use_subcommand" -a "${cmd}" -d "${cmd} command"`);
|
||||
}
|
||||
lines.push("");
|
||||
return lines.join("\n");
|
||||
}
|
||||
|
||||
// Static sub-commands for each command
|
||||
lines.push(`# Static sub-commands`);
|
||||
/**
|
||||
* Generates fish static subcommand completions.
|
||||
*/
|
||||
function generateFishStaticSubcommands(binName: string): string {
|
||||
const lines: string[] = [];
|
||||
for (const [cmd, subs] of Object.entries(COMMAND_TREE)) {
|
||||
for (const sub of subs) {
|
||||
lines.push(`complete -c ${binName} -n "__fish_seen_subcommand_from ${cmd}; and not __fish_seen_subcommand_from ${subs.join(" ")}" -a "${sub}" -d "${cmd} ${sub}"`);
|
||||
}
|
||||
}
|
||||
lines.push("");
|
||||
return lines.join("\n");
|
||||
}
|
||||
|
||||
// Dynamic completions
|
||||
lines.push(`# Dynamic completions`);
|
||||
lines.push("");
|
||||
/**
|
||||
* Generates a bash completion script.
|
||||
* @param binName - The name of the CLI binary.
|
||||
*/
|
||||
export function generateBashCompletions(binName: string): string {
|
||||
return loadAndProcessTemplate("bash.sh", binName);
|
||||
}
|
||||
|
||||
// invitation create <template> <action>
|
||||
lines.push(`# invitation create: template names`);
|
||||
lines.push(`complete -c ${binName} -n "__fish_seen_subcommand_from invitation; and __fish_seen_subcommand_from create; and test (count (commandline -opc)) -eq 3" -xa '(__${binName.replace(/-/g, "_")}_complete_dynamic templates)'`);
|
||||
lines.push("");
|
||||
/**
|
||||
* Generates a zsh completion script.
|
||||
* @param binName - The name of the CLI binary.
|
||||
*/
|
||||
export function generateZshCompletions(binName: string): string {
|
||||
return loadAndProcessTemplate("zsh.zsh", binName);
|
||||
}
|
||||
|
||||
// invitation append/sign/broadcast/requirements/inspect: invitation IDs
|
||||
lines.push(`# invitation append/sign/broadcast/requirements/inspect: invitation IDs`);
|
||||
for (const sub of ["append", "sign", "broadcast", "requirements", "inspect"]) {
|
||||
lines.push(`complete -c ${binName} -n "__fish_seen_subcommand_from invitation; and __fish_seen_subcommand_from ${sub}; and test (count (commandline -opc)) -eq 3" -xa '(__${binName.replace(/-/g, "_")}_complete_dynamic invitations)'`);
|
||||
}
|
||||
lines.push("");
|
||||
|
||||
// invitation import: file completion
|
||||
lines.push(`# invitation import: file completion`);
|
||||
lines.push(`complete -c ${binName} -n "__fish_seen_subcommand_from invitation; and __fish_seen_subcommand_from import" -F`);
|
||||
lines.push("");
|
||||
|
||||
// template list/inspect: category first, then template
|
||||
lines.push(`# template list/inspect: category first (pos 3), then template (pos 4)`);
|
||||
for (const sub of ["list", "inspect"]) {
|
||||
lines.push(`complete -c ${binName} -n "__fish_seen_subcommand_from template; and __fish_seen_subcommand_from ${sub}; and test (count (commandline -opc)) -eq 3" -xa 'action transaction output lockingscript variable'`);
|
||||
lines.push(`complete -c ${binName} -n "__fish_seen_subcommand_from template; and __fish_seen_subcommand_from ${sub}; and test (count (commandline -opc)) -eq 4" -xa '(__${binName.replace(/-/g, "_")}_complete_dynamic templates)'`);
|
||||
}
|
||||
lines.push("");
|
||||
|
||||
// template set-default: template first
|
||||
lines.push(`# template set-default: template first`);
|
||||
lines.push(`complete -c ${binName} -n "__fish_seen_subcommand_from template; and __fish_seen_subcommand_from set-default; and test (count (commandline -opc)) -eq 3" -xa '(__${binName.replace(/-/g, "_")}_complete_dynamic templates)'`);
|
||||
lines.push("");
|
||||
|
||||
// resource unreserve: outpoints
|
||||
lines.push(`# resource unreserve: UTXO outpoints`);
|
||||
lines.push(`complete -c ${binName} -n "__fish_seen_subcommand_from resource; and __fish_seen_subcommand_from unreserve; and test (count (commandline -opc)) -eq 3" -xa '(__${binName.replace(/-/g, "_")}_complete_dynamic resources)'`);
|
||||
lines.push("");
|
||||
|
||||
// receive: template names
|
||||
lines.push(`# receive: template names`);
|
||||
lines.push(`complete -c ${binName} -n "__fish_seen_subcommand_from receive; and test (count (commandline -opc)) -eq 2" -xa '(__${binName.replace(/-/g, "_")}_complete_dynamic templates)'`);
|
||||
|
||||
return lines.join("\n") + "\n";
|
||||
/**
|
||||
* Generates a fish completion script.
|
||||
* @param binName - The name of the CLI binary.
|
||||
*/
|
||||
export function generateFishCompletions(binName: string): string {
|
||||
return loadAndProcessTemplate("fish.fish", binName);
|
||||
}
|
||||
|
||||
type ShellType = "bash" | "zsh" | "fish";
|
||||
@@ -544,25 +160,96 @@ const generators: Record<ShellType, (binName: string) => string> = {
|
||||
fish: generateFishCompletions,
|
||||
};
|
||||
|
||||
/**
|
||||
* Shell config file paths and eval commands for each shell type.
|
||||
*/
|
||||
const shellConfigs: Record<ShellType, { configFile: string; evalCommand: (binName: string) => string }> = {
|
||||
bash: {
|
||||
configFile: join(homedir(), ".bashrc"),
|
||||
evalCommand: (binName) => `eval "$(${binName} completions bash)"`,
|
||||
},
|
||||
zsh: {
|
||||
configFile: join(homedir(), ".zshrc"),
|
||||
evalCommand: (binName) => `eval "$(${binName} completions zsh)"`,
|
||||
},
|
||||
fish: {
|
||||
configFile: join(homedir(), ".config", "fish", "config.fish"),
|
||||
evalCommand: (binName) => `${binName} completions fish | source`,
|
||||
},
|
||||
};
|
||||
|
||||
/**
|
||||
* Installs completions to the user's shell config file.
|
||||
* Adds the eval command if not already present.
|
||||
* @param shell - The shell type
|
||||
* @param binName - The CLI binary name
|
||||
* @returns true if installed, false if already present
|
||||
*/
|
||||
function installCompletions(shell: ShellType, binName: string): boolean {
|
||||
const config = shellConfigs[shell];
|
||||
const evalCommand = config.evalCommand(binName);
|
||||
|
||||
// Check if config file exists and already has the completion line
|
||||
let existingContent = "";
|
||||
if (existsSync(config.configFile)) {
|
||||
existingContent = readFileSync(config.configFile, "utf8");
|
||||
if (existingContent.includes(evalCommand)) {
|
||||
return false; // Already installed
|
||||
}
|
||||
}
|
||||
|
||||
// Append the completion line
|
||||
const newLine = existingContent.endsWith("\n") || existingContent === "" ? "" : "\n";
|
||||
const completionBlock = `${newLine}\n# ${binName} shell completions\n${evalCommand}\n`;
|
||||
|
||||
appendFileSync(config.configFile, completionBlock);
|
||||
return true;
|
||||
}
|
||||
|
||||
/**
|
||||
* Handles the `completions` command.
|
||||
* Prints the generated completion script for the given shell to stdout.
|
||||
* Prints the generated completion script for the given shell to stdout,
|
||||
* or installs it to the shell config file with --install.
|
||||
* @param args - Positional args after "completions", e.g. ["bash"].
|
||||
* @param options - Parsed command options (may include "install").
|
||||
* @param binName - The CLI binary name to use in the completion script.
|
||||
*/
|
||||
export function handleCompletionsCommand(args: string[], binName: string = "xo-cli"): void {
|
||||
export function handleCompletionsCommand(
|
||||
args: string[],
|
||||
options: Record<string, string> = {},
|
||||
binName: string = "xo-cli",
|
||||
): void {
|
||||
const shell = args[0] as ShellType | undefined;
|
||||
const installFlag = options["install"] === "true";
|
||||
|
||||
if (!shell || !generators[shell]) {
|
||||
const supported = Object.keys(generators).join(", ");
|
||||
console.error(`Usage: ${binName} completions <${supported}>`);
|
||||
console.error(`Usage: ${binName} completions <${supported}> [--install]`);
|
||||
console.error("");
|
||||
console.error("Examples:");
|
||||
console.error(` eval "$(${binName} completions bash)" # Add to ~/.bashrc`);
|
||||
console.error(` eval "$(${binName} completions zsh)" # Add to ~/.zshrc`);
|
||||
console.error(` ${binName} completions fish | source # Add to fish config`);
|
||||
console.error(` eval "$(${binName} completions bash)" # Output to stdout (add to ~/.bashrc)`);
|
||||
console.error(` eval "$(${binName} completions zsh)" # Output to stdout (add to ~/.zshrc)`);
|
||||
console.error(` ${binName} completions fish | source # Output to stdout (add to fish config)`);
|
||||
console.error("");
|
||||
console.error("Install directly to shell config:");
|
||||
console.error(` ${binName} completions bash --install # Appends to ~/.bashrc`);
|
||||
console.error(` ${binName} completions zsh --install # Appends to ~/.zshrc`);
|
||||
console.error(` ${binName} completions fish --install # Appends to ~/.config/fish/config.fish`);
|
||||
process.exit(1);
|
||||
}
|
||||
|
||||
if (installFlag) {
|
||||
const config = shellConfigs[shell];
|
||||
const installed = installCompletions(shell, binName);
|
||||
|
||||
if (installed) {
|
||||
console.log(`Completions installed to ${config.configFile}`);
|
||||
console.log(`Restart your shell or run: source ${config.configFile}`);
|
||||
} else {
|
||||
console.log(`Completions already installed in ${config.configFile}`);
|
||||
}
|
||||
return;
|
||||
}
|
||||
|
||||
process.stdout.write(generators[shell](binName));
|
||||
}
|
||||
|
||||
199
src/cli/autocomplete/scripts/bash.sh
Normal file
199
src/cli/autocomplete/scripts/bash.sh
Normal file
@@ -0,0 +1,199 @@
|
||||
# bash completion for {{BIN_NAME}}
|
||||
# Add to ~/.bashrc: eval "$({{BIN_NAME}} completions bash)"
|
||||
|
||||
# Find xo-complete in the same directory as xo-cli
|
||||
__xo_complete_bin=""
|
||||
if command -v xo-complete &>/dev/null; then
|
||||
__xo_complete_bin="xo-complete"
|
||||
elif command -v {{BIN_NAME}} &>/dev/null; then
|
||||
__xo_complete_bin="$(dirname "$(command -v {{BIN_NAME}})")/xo-complete"
|
||||
fi
|
||||
|
||||
# Wrapper to call xo-complete helper
|
||||
__xo_complete() {
|
||||
[[ -n "${__xo_complete_bin}" ]] && "${__xo_complete_bin}" "$@" 2>/dev/null
|
||||
}
|
||||
|
||||
_{{FUNC_NAME}}_completions() {
|
||||
local cur prev words cword
|
||||
_init_completion || return
|
||||
|
||||
# Handle -m/--mnemonic-file argument (previous word was -m)
|
||||
if [[ "${prev}" == "-m" || "${prev}" == "--mnemonic-file" ]]; then
|
||||
local mnemonics
|
||||
mnemonics=$(__xo_complete mnemonics "${cur}")
|
||||
if [[ -n "${mnemonics}" ]]; then
|
||||
while IFS= read -r line; do
|
||||
COMPREPLY+=("$line")
|
||||
done <<< "${mnemonics}"
|
||||
return 0
|
||||
fi
|
||||
fi
|
||||
|
||||
# If the current word starts with "-", offer option flags
|
||||
if [[ "${cur}" == -* ]]; then
|
||||
COMPREPLY=($(compgen -W "{{OPTIONS}}" -- "${cur}"))
|
||||
return 0
|
||||
fi
|
||||
|
||||
# Find the command and subcommand positions
|
||||
local cmd="" subcmd="" cmd_idx=0 subcmd_idx=0
|
||||
for ((i=1; i < cword; i++)); do
|
||||
if [[ "${words[i]}" != -* ]]; then
|
||||
if [[ -z "${cmd}" ]]; then
|
||||
cmd="${words[i]}"
|
||||
cmd_idx=$i
|
||||
else
|
||||
subcmd="${words[i]}"
|
||||
subcmd_idx=$i
|
||||
break
|
||||
fi
|
||||
fi
|
||||
done
|
||||
|
||||
# No command yet — offer the top-level commands
|
||||
if [[ -z "${cmd}" ]]; then
|
||||
COMPREPLY=($(compgen -W "{{COMMANDS}}" -- "${cur}"))
|
||||
return 0
|
||||
fi
|
||||
|
||||
# Handle each command's completion
|
||||
case "${cmd}" in
|
||||
mnemonic)
|
||||
if [[ -z "${subcmd}" ]]; then
|
||||
COMPREPLY=($(compgen -W "{{MNEMONIC_SUBS}}" -- "${cur}"))
|
||||
fi
|
||||
;;
|
||||
|
||||
template)
|
||||
if [[ -z "${subcmd}" ]]; then
|
||||
COMPREPLY=($(compgen -W "{{TEMPLATE_SUBS}}" -- "${cur}"))
|
||||
elif [[ "${subcmd}" == "list" || "${subcmd}" == "inspect" ]]; then
|
||||
# template list/inspect <category> <template> [field] - category first, then template, then field
|
||||
local pos=$((cword - subcmd_idx))
|
||||
if [[ $pos -eq 1 ]]; then
|
||||
COMPREPLY=($(compgen -W "action transaction output lockingscript variable" -- "${cur}"))
|
||||
elif [[ $pos -eq 2 ]]; then
|
||||
local templates
|
||||
templates=$(__xo_complete templates "${cur}")
|
||||
if [[ -n "${templates}" ]]; then
|
||||
while IFS= read -r line; do
|
||||
COMPREPLY+=("$line")
|
||||
done <<< "${templates}"
|
||||
fi
|
||||
elif [[ $pos -eq 3 && "${subcmd}" == "inspect" ]]; then
|
||||
# Get the category and template from previous args
|
||||
local category="${words[subcmd_idx + 1]}"
|
||||
local template_arg="${words[subcmd_idx + 2]}"
|
||||
local fields
|
||||
fields=$(__xo_complete fields "${category}" "${template_arg}" "${cur}")
|
||||
if [[ -n "${fields}" ]]; then
|
||||
while IFS= read -r line; do
|
||||
COMPREPLY+=("$line")
|
||||
done <<< "${fields}"
|
||||
fi
|
||||
fi
|
||||
elif [[ "${subcmd}" == "set-default" ]]; then
|
||||
# template set-default <template> <output> <role> - template first
|
||||
local pos=$((cword - subcmd_idx))
|
||||
if [[ $pos -eq 1 ]]; then
|
||||
local templates
|
||||
templates=$(__xo_complete templates "${cur}")
|
||||
if [[ -n "${templates}" ]]; then
|
||||
while IFS= read -r line; do
|
||||
COMPREPLY+=("$line")
|
||||
done <<< "${templates}"
|
||||
fi
|
||||
fi
|
||||
fi
|
||||
;;
|
||||
|
||||
invitation)
|
||||
if [[ -z "${subcmd}" ]]; then
|
||||
COMPREPLY=($(compgen -W "{{INVITATION_SUBS}}" -- "${cur}"))
|
||||
else
|
||||
case "${subcmd}" in
|
||||
create)
|
||||
# invitation create <template> <action> - offer templates then actions
|
||||
local pos=$((cword - subcmd_idx))
|
||||
if [[ $pos -eq 1 ]]; then
|
||||
local templates
|
||||
templates=$(__xo_complete templates "${cur}")
|
||||
if [[ -n "${templates}" ]]; then
|
||||
while IFS= read -r line; do
|
||||
COMPREPLY+=("$line")
|
||||
done <<< "${templates}"
|
||||
fi
|
||||
elif [[ $pos -eq 2 ]]; then
|
||||
local template_arg="${words[subcmd_idx + 1]}"
|
||||
local actions
|
||||
actions=$(__xo_complete actions "${template_arg}" "${cur}")
|
||||
if [[ -n "${actions}" ]]; then
|
||||
while IFS= read -r line; do
|
||||
COMPREPLY+=("$line")
|
||||
done <<< "${actions}"
|
||||
fi
|
||||
fi
|
||||
;;
|
||||
append|sign|broadcast|requirements|inspect)
|
||||
# These take an invitation ID
|
||||
local pos=$((cword - subcmd_idx))
|
||||
if [[ $pos -eq 1 ]]; then
|
||||
local invitations
|
||||
invitations=$(__xo_complete invitations "${cur}")
|
||||
if [[ -n "${invitations}" ]]; then
|
||||
while IFS= read -r line; do
|
||||
COMPREPLY+=("$line")
|
||||
done <<< "${invitations}"
|
||||
fi
|
||||
fi
|
||||
;;
|
||||
import)
|
||||
# import takes a file path - use default file completion
|
||||
COMPREPLY=($(compgen -f -- "${cur}"))
|
||||
;;
|
||||
esac
|
||||
fi
|
||||
;;
|
||||
|
||||
resource)
|
||||
if [[ -z "${subcmd}" ]]; then
|
||||
COMPREPLY=($(compgen -W "{{RESOURCE_SUBS}}" -- "${cur}"))
|
||||
elif [[ "${subcmd}" == "unreserve" ]]; then
|
||||
# resource unreserve <txhash:vout> - offer resources
|
||||
local pos=$((cword - subcmd_idx))
|
||||
if [[ $pos -eq 1 ]]; then
|
||||
local resources
|
||||
resources=$(__xo_complete resources "${cur}")
|
||||
if [[ -n "${resources}" ]]; then
|
||||
while IFS= read -r line; do
|
||||
COMPREPLY+=("$line")
|
||||
done <<< "${resources}"
|
||||
fi
|
||||
fi
|
||||
fi
|
||||
;;
|
||||
|
||||
receive)
|
||||
# receive <template> [output] - offer templates
|
||||
local pos=$((cword - cmd_idx))
|
||||
if [[ $pos -eq 1 ]]; then
|
||||
local templates
|
||||
templates=$(__xo_complete templates "${cur}")
|
||||
if [[ -n "${templates}" ]]; then
|
||||
while IFS= read -r line; do
|
||||
COMPREPLY+=("$line")
|
||||
done <<< "${templates}"
|
||||
fi
|
||||
fi
|
||||
;;
|
||||
|
||||
completions)
|
||||
if [[ -z "${subcmd}" ]]; then
|
||||
COMPREPLY=($(compgen -W "bash zsh fish" -- "${cur}"))
|
||||
fi
|
||||
;;
|
||||
esac
|
||||
}
|
||||
|
||||
complete -F _{{FUNC_NAME}}_completions {{BIN_NAME}}
|
||||
70
src/cli/autocomplete/scripts/fish.fish
Normal file
70
src/cli/autocomplete/scripts/fish.fish
Normal file
@@ -0,0 +1,70 @@
|
||||
# fish completion for {{BIN_NAME}}
|
||||
# Add to fish config: {{BIN_NAME}} completions fish | source
|
||||
|
||||
# Disable file completions by default
|
||||
complete -c {{BIN_NAME}} -f
|
||||
|
||||
# Helper function to get dynamic completions
|
||||
# Finds xo-complete in the same directory as {{BIN_NAME}}
|
||||
function __{{FUNC_NAME}}_complete_dynamic
|
||||
set -l xo_complete_bin ""
|
||||
if command -q xo-complete
|
||||
set xo_complete_bin xo-complete
|
||||
else if command -q {{BIN_NAME}}
|
||||
set xo_complete_bin (dirname (command -s {{BIN_NAME}}))/xo-complete
|
||||
end
|
||||
if test -n "$xo_complete_bin"
|
||||
$xo_complete_bin $argv 2>/dev/null
|
||||
end
|
||||
end
|
||||
|
||||
# Global options
|
||||
complete -c {{BIN_NAME}} -s h -d "Show help"
|
||||
complete -c {{BIN_NAME}} -l help -d "Show help"
|
||||
complete -c {{BIN_NAME}} -s v -d "Verbose output"
|
||||
complete -c {{BIN_NAME}} -l verbose -d "Verbose output"
|
||||
complete -c {{BIN_NAME}} -s o -d "Output file"
|
||||
complete -c {{BIN_NAME}} -l output -d "Output file"
|
||||
|
||||
# Dynamic mnemonic file completion for -m
|
||||
complete -c {{BIN_NAME}} -s m -l mnemonic-file -xa '(__{{FUNC_NAME}}_complete_dynamic mnemonics)'
|
||||
|
||||
# Top-level commands
|
||||
{{TOP_LEVEL_COMMANDS}}
|
||||
|
||||
# Static sub-commands
|
||||
{{STATIC_SUBCOMMANDS}}
|
||||
|
||||
# Dynamic completions
|
||||
|
||||
# invitation create: template names
|
||||
complete -c {{BIN_NAME}} -n "__fish_seen_subcommand_from invitation; and __fish_seen_subcommand_from create; and test (count (commandline -opc)) -eq 3" -xa '(__{{FUNC_NAME}}_complete_dynamic templates)'
|
||||
|
||||
# invitation create: action names (2nd arg)
|
||||
complete -c {{BIN_NAME}} -n "__fish_seen_subcommand_from invitation; and __fish_seen_subcommand_from create; and test (count (commandline -opc)) -eq 4" -xa '(__{{FUNC_NAME}}_complete_dynamic actions (commandline -opc)[4])'
|
||||
|
||||
# invitation append/sign/broadcast/requirements/inspect: invitation IDs
|
||||
complete -c {{BIN_NAME}} -n "__fish_seen_subcommand_from invitation; and __fish_seen_subcommand_from append; and test (count (commandline -opc)) -eq 3" -xa '(__{{FUNC_NAME}}_complete_dynamic invitations)'
|
||||
complete -c {{BIN_NAME}} -n "__fish_seen_subcommand_from invitation; and __fish_seen_subcommand_from sign; and test (count (commandline -opc)) -eq 3" -xa '(__{{FUNC_NAME}}_complete_dynamic invitations)'
|
||||
complete -c {{BIN_NAME}} -n "__fish_seen_subcommand_from invitation; and __fish_seen_subcommand_from broadcast; and test (count (commandline -opc)) -eq 3" -xa '(__{{FUNC_NAME}}_complete_dynamic invitations)'
|
||||
complete -c {{BIN_NAME}} -n "__fish_seen_subcommand_from invitation; and __fish_seen_subcommand_from requirements; and test (count (commandline -opc)) -eq 3" -xa '(__{{FUNC_NAME}}_complete_dynamic invitations)'
|
||||
complete -c {{BIN_NAME}} -n "__fish_seen_subcommand_from invitation; and __fish_seen_subcommand_from inspect; and test (count (commandline -opc)) -eq 3" -xa '(__{{FUNC_NAME}}_complete_dynamic invitations)'
|
||||
|
||||
# invitation import: file completion
|
||||
complete -c {{BIN_NAME}} -n "__fish_seen_subcommand_from invitation; and __fish_seen_subcommand_from import" -F
|
||||
|
||||
# template list/inspect: category first (pos 3), then template (pos 4), then field (pos 5 for inspect)
|
||||
complete -c {{BIN_NAME}} -n "__fish_seen_subcommand_from template; and __fish_seen_subcommand_from list; and test (count (commandline -opc)) -eq 3" -xa 'action transaction output lockingscript variable'
|
||||
complete -c {{BIN_NAME}} -n "__fish_seen_subcommand_from template; and __fish_seen_subcommand_from list; and test (count (commandline -opc)) -eq 4" -xa '(__{{FUNC_NAME}}_complete_dynamic templates)'
|
||||
complete -c {{BIN_NAME}} -n "__fish_seen_subcommand_from template; and __fish_seen_subcommand_from inspect; and test (count (commandline -opc)) -eq 3" -xa 'action transaction output lockingscript variable'
|
||||
complete -c {{BIN_NAME}} -n "__fish_seen_subcommand_from template; and __fish_seen_subcommand_from inspect; and test (count (commandline -opc)) -eq 4" -xa '(__{{FUNC_NAME}}_complete_dynamic templates)'
|
||||
complete -c {{BIN_NAME}} -n "__fish_seen_subcommand_from template; and __fish_seen_subcommand_from inspect; and test (count (commandline -opc)) -eq 5" -xa '(__{{FUNC_NAME}}_complete_dynamic fields (commandline -opc)[4] (commandline -opc)[5])'
|
||||
|
||||
# template set-default: template first
|
||||
complete -c {{BIN_NAME}} -n "__fish_seen_subcommand_from template; and __fish_seen_subcommand_from set-default; and test (count (commandline -opc)) -eq 3" -xa '(__{{FUNC_NAME}}_complete_dynamic templates)'
|
||||
|
||||
# resource unreserve: UTXO outpoints
|
||||
complete -c {{BIN_NAME}} -n "__fish_seen_subcommand_from resource; and __fish_seen_subcommand_from unreserve; and test (count (commandline -opc)) -eq 3" -xa '(__{{FUNC_NAME}}_complete_dynamic resources)'
|
||||
|
||||
# receive: template names
|
||||
complete -c {{BIN_NAME}} -n "__fish_seen_subcommand_from receive; and test (count (commandline -opc)) -eq 2" -xa '(__{{FUNC_NAME}}_complete_dynamic templates)'
|
||||
176
src/cli/autocomplete/scripts/zsh.zsh
Normal file
176
src/cli/autocomplete/scripts/zsh.zsh
Normal file
@@ -0,0 +1,176 @@
|
||||
# zsh completion for {{BIN_NAME}}
|
||||
# Add to ~/.zshrc: eval "$({{BIN_NAME}} completions zsh)"
|
||||
|
||||
# Find xo-complete in the same directory as xo-cli
|
||||
__xo_complete_bin=""
|
||||
if (( $+commands[xo-complete] )); then
|
||||
__xo_complete_bin="xo-complete"
|
||||
elif (( $+commands[{{BIN_NAME}}] )); then
|
||||
__xo_complete_bin="${commands[{{BIN_NAME}}]:h}/xo-complete"
|
||||
fi
|
||||
|
||||
# Wrapper to call xo-complete helper
|
||||
__xo_complete() {
|
||||
[[ -n "${__xo_complete_bin}" ]] && "${__xo_complete_bin}" "$@" 2>/dev/null
|
||||
}
|
||||
|
||||
_{{FUNC_NAME}}_completions() {
|
||||
local -a commands
|
||||
commands=({{COMMANDS}})
|
||||
|
||||
# Handle -m/--mnemonic-file argument (previous word was -m)
|
||||
if [[ "${words[CURRENT-1]}" == "-m" || "${words[CURRENT-1]}" == "--mnemonic-file" ]]; then
|
||||
local mnemonics
|
||||
mnemonics=("${(@f)$(__xo_complete mnemonics "${words[CURRENT]}")}")
|
||||
if [[ ${#mnemonics[@]} -gt 0 ]]; then
|
||||
compadd -- "${mnemonics[@]}"
|
||||
return
|
||||
fi
|
||||
fi
|
||||
|
||||
# If typing an option flag, complete options
|
||||
if [[ "${words[${CURRENT}]}" == -* ]]; then
|
||||
compadd -- {{OPTIONS}}
|
||||
return
|
||||
fi
|
||||
|
||||
# Find the command and subcommand
|
||||
local cmd="" subcmd="" cmd_idx=0 subcmd_idx=0
|
||||
for ((i=2; i < CURRENT; i++)); do
|
||||
if [[ "${words[i]}" != -* ]]; then
|
||||
if [[ -z "${cmd}" ]]; then
|
||||
cmd="${words[i]}"
|
||||
cmd_idx=$i
|
||||
else
|
||||
subcmd="${words[i]}"
|
||||
subcmd_idx=$i
|
||||
break
|
||||
fi
|
||||
fi
|
||||
done
|
||||
|
||||
# No command yet — offer top-level commands
|
||||
if [[ -z "${cmd}" ]]; then
|
||||
compadd -- ${commands[@]}
|
||||
return
|
||||
fi
|
||||
|
||||
# Handle each command's completion
|
||||
case "${cmd}" in
|
||||
mnemonic)
|
||||
if [[ -z "${subcmd}" ]]; then
|
||||
compadd -- {{MNEMONIC_SUBS}}
|
||||
fi
|
||||
;;
|
||||
|
||||
template)
|
||||
if [[ -z "${subcmd}" ]]; then
|
||||
compadd -- {{TEMPLATE_SUBS}}
|
||||
elif [[ "${subcmd}" == "list" || "${subcmd}" == "inspect" ]]; then
|
||||
# template list/inspect <category> <template> [field] - category first, then template, then field
|
||||
local pos=$((CURRENT - subcmd_idx))
|
||||
if [[ $pos -eq 1 ]]; then
|
||||
compadd -- action transaction output lockingscript variable
|
||||
elif [[ $pos -eq 2 ]]; then
|
||||
local templates
|
||||
templates=("${(@f)$(__xo_complete templates "${words[CURRENT]}")}")
|
||||
if [[ ${#templates[@]} -gt 0 ]]; then
|
||||
compadd -- "${templates[@]}"
|
||||
fi
|
||||
elif [[ $pos -eq 3 && "${subcmd}" == "inspect" ]]; then
|
||||
# Get the category and template from previous args
|
||||
local category="${words[subcmd_idx + 1]}"
|
||||
local template_arg="${words[subcmd_idx + 2]}"
|
||||
local fields
|
||||
fields=("${(@f)$(__xo_complete fields "${category}" "${template_arg}" "${words[CURRENT]}")}")
|
||||
if [[ ${#fields[@]} -gt 0 ]]; then
|
||||
compadd -- "${fields[@]}"
|
||||
fi
|
||||
fi
|
||||
elif [[ "${subcmd}" == "set-default" ]]; then
|
||||
# template set-default <template> <output> <role> - template first
|
||||
local pos=$((CURRENT - subcmd_idx))
|
||||
if [[ $pos -eq 1 ]]; then
|
||||
local templates
|
||||
templates=("${(@f)$(__xo_complete templates "${words[CURRENT]}")}")
|
||||
if [[ ${#templates[@]} -gt 0 ]]; then
|
||||
compadd -- "${templates[@]}"
|
||||
fi
|
||||
fi
|
||||
fi
|
||||
;;
|
||||
|
||||
invitation)
|
||||
if [[ -z "${subcmd}" ]]; then
|
||||
compadd -- {{INVITATION_SUBS}}
|
||||
else
|
||||
case "${subcmd}" in
|
||||
create)
|
||||
local pos=$((CURRENT - subcmd_idx))
|
||||
if [[ $pos -eq 1 ]]; then
|
||||
local templates
|
||||
templates=("${(@f)$(__xo_complete templates "${words[CURRENT]}")}")
|
||||
if [[ ${#templates[@]} -gt 0 ]]; then
|
||||
compadd -- "${templates[@]}"
|
||||
fi
|
||||
elif [[ $pos -eq 2 ]]; then
|
||||
local template_arg="${words[subcmd_idx + 1]}"
|
||||
local actions
|
||||
actions=("${(@f)$(__xo_complete actions "${template_arg}" "${words[CURRENT]}")}")
|
||||
if [[ ${#actions[@]} -gt 0 ]]; then
|
||||
compadd -- "${actions[@]}"
|
||||
fi
|
||||
fi
|
||||
;;
|
||||
append|sign|broadcast|requirements|inspect)
|
||||
local pos=$((CURRENT - subcmd_idx))
|
||||
if [[ $pos -eq 1 ]]; then
|
||||
local invitations
|
||||
invitations=("${(@f)$(__xo_complete invitations "${words[CURRENT]}")}")
|
||||
if [[ ${#invitations[@]} -gt 0 ]]; then
|
||||
compadd -- "${invitations[@]}"
|
||||
fi
|
||||
fi
|
||||
;;
|
||||
import)
|
||||
_files
|
||||
;;
|
||||
esac
|
||||
fi
|
||||
;;
|
||||
|
||||
resource)
|
||||
if [[ -z "${subcmd}" ]]; then
|
||||
compadd -- {{RESOURCE_SUBS}}
|
||||
elif [[ "${subcmd}" == "unreserve" ]]; then
|
||||
local pos=$((CURRENT - subcmd_idx))
|
||||
if [[ $pos -eq 1 ]]; then
|
||||
local resources
|
||||
resources=("${(@f)$(__xo_complete resources "${words[CURRENT]}")}")
|
||||
if [[ ${#resources[@]} -gt 0 ]]; then
|
||||
compadd -- "${resources[@]}"
|
||||
fi
|
||||
fi
|
||||
fi
|
||||
;;
|
||||
|
||||
receive)
|
||||
local pos=$((CURRENT - cmd_idx))
|
||||
if [[ $pos -eq 1 ]]; then
|
||||
local templates
|
||||
templates=("${(@f)$(__xo_complete templates "${words[CURRENT]}")}")
|
||||
if [[ ${#templates[@]} -gt 0 ]]; then
|
||||
compadd -- "${templates[@]}"
|
||||
fi
|
||||
fi
|
||||
;;
|
||||
|
||||
completions)
|
||||
if [[ -z "${subcmd}" ]]; then
|
||||
compadd -- bash zsh fish
|
||||
fi
|
||||
;;
|
||||
esac
|
||||
}
|
||||
|
||||
compdef _{{FUNC_NAME}}_completions {{BIN_NAME}}
|
||||
Reference in New Issue
Block a user