Skip to content

Instantly share code, notes, and snippets.

@sigoden
Created August 4, 2022 01:08
Show Gist options
  • Save sigoden/b40623dedc5d55f2b7bad356c6daaaf1 to your computer and use it in GitHub Desktop.
Save sigoden/b40623dedc5d55f2b7bad356c6daaaf1 to your computer and use it in GitHub Desktop.
completions for argc script
_argc_script() {
local argcfile cur opts
cur="${COMP_WORDS[COMP_CWORD]}"
COMPREPLY=()
argcfile=$(which ${COMP_WORDS[0]})
if [ $? != 0 ]; then
return 0
fi
opts=$(argc --argc-compgen "$argcfile" ${COMP_WORDS[@]:1} 2>/dev/null)
COMPREPLY=( $(compgen -W "${opts}" -- "${cur}") )
return 0
}
complete -F _argc_script -o bashdefault -o default mycmd1 mycmd2
$_argc_script_completion = {
param($wordToComplete, $commandAst, $cursorPosition)
$argcfile = (Get-Command $commandAst.CommandElements[0] -ErrorAction SilentlyContinue | Select-Object -ExpandProperty Source)
if (!$argcfile) {
return;
}
$cmds = $commandAst.CommandElements[1..($commandAst.CommandElements.Count - 1)]
(argc --argc-compgen "$argcfile" $cmds 2>$null) -split " " |
Where-Object { $_ -like "$wordToComplete*" } |
ForEach-Object {
if ($_.StartsWith("-")) {
$t = [System.Management.Automation.CompletionResultType]::ParameterName
} else {
$t = [System.Management.Automation.CompletionResultType]::ParameterValue
}
[System.Management.Automation.CompletionResult]::new($_, $_, $t, '-')
}
}
Register-ArgumentCompleter -Native -CommandName mycmd1 -ScriptBlock $_argc_script_completion
Register-ArgumentCompleter -Native -CommandName mycmd2 -ScriptBlock $_argc_script_completion
_argc_script()
{
local argcfile items
argcfile=$(which $words[1])
if [[ $? -ne 0 ]]; then
return 0
fi
items=( $(argc --argc-compgen "$argcfile" ${words[@]:1} 2>/dev/null) )
compadd $items[@]
return 0
}
compdef _argc_script mycmd1 mycmd2
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment