2024-02-01 11:07:08 +03:00
|
|
|
#!/usr/bin/env python3
|
|
|
|
"""
|
|
|
|
Generate Completion files
|
|
|
|
"""
|
|
|
|
from typing import *
|
|
|
|
from option import Option
|
|
|
|
|
|
|
|
|
|
|
|
def generate_bash_completion(name: str, options: List[Option]):
|
|
|
|
available_options = ["--" + option.long for option in options] + [
|
|
|
|
"--help",
|
|
|
|
"--version",
|
|
|
|
]
|
|
|
|
return (
|
|
|
|
"# "
|
|
|
|
+ name
|
|
|
|
+ """(1) completion -*- shell-script -*-
|
|
|
|
_"""
|
|
|
|
+ name
|
|
|
|
+ """()
|
|
|
|
{
|
|
|
|
local cur prev words cword
|
|
|
|
_init_completion || return
|
|
|
|
|
|
|
|
if [[ $cur == -* ]]; then
|
|
|
|
COMPREPLY=($(compgen -W '"""
|
|
|
|
+ " ".join(available_options)
|
|
|
|
+ """' -- "$cur"))
|
|
|
|
return
|
|
|
|
fi
|
|
|
|
|
|
|
|
} &&
|
|
|
|
complete -F _"""
|
|
|
|
+ name
|
|
|
|
+ " "
|
|
|
|
+ name
|
|
|
|
+ """
|
|
|
|
# ex: filetype=sh
|
|
|
|
"""
|
|
|
|
)
|
2024-02-27 15:02:19 +03:00
|
|
|
|
|
|
|
|
2024-02-28 11:11:28 +03:00
|
|
|
def generate_zsh_completion(name: str, options: List[Option]):
|
|
|
|
return (
|
|
|
|
"""#compdef """
|
|
|
|
+ name
|
|
|
|
+ """
|
|
|
|
|
|
|
|
autoload -U is-at-least
|
|
|
|
|
|
|
|
_"""
|
|
|
|
+ name
|
|
|
|
+ """() {
|
|
|
|
typeset -A opt_args
|
|
|
|
typeset -a _arguments_options
|
|
|
|
local ret=1
|
|
|
|
|
|
|
|
if is-at-least 5.2; then
|
|
|
|
_arguments_options=(-s -S -C)
|
|
|
|
else
|
|
|
|
_arguments_options=(-s -C)
|
|
|
|
fi
|
|
|
|
|
|
|
|
local context curcontext="$curcontext" state line
|
|
|
|
_arguments "${_arguments_options[@]}" \
|
|
|
|
"""
|
|
|
|
+ """\n """.join([zsh_option(option) for option in options])
|
|
|
|
+ """
|
|
|
|
'--help[Print help]' \
|
|
|
|
'--version[Print version]' \
|
|
|
|
'*::params:' \\
|
|
|
|
&& ret=0
|
|
|
|
}
|
|
|
|
|
|
|
|
(( $+functions[_"""
|
|
|
|
+ name
|
|
|
|
+ """_commands] )) ||
|
|
|
|
_"""
|
|
|
|
+ name
|
|
|
|
+ """_commands() {
|
|
|
|
local commands; commands=()
|
|
|
|
_describe -t commands '"""
|
|
|
|
+ name
|
|
|
|
+ """ commands' commands "$@"
|
|
|
|
}
|
|
|
|
|
|
|
|
if [ "$funcstack[1]" = "_"""
|
|
|
|
+ name
|
|
|
|
+ """" ]; then
|
|
|
|
_"""
|
|
|
|
+ name
|
|
|
|
+ """ "$@"
|
|
|
|
else
|
|
|
|
compdef _"""
|
|
|
|
+ name
|
|
|
|
+ " "
|
|
|
|
+ name
|
|
|
|
+ """
|
|
|
|
fi"""
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
|
|
def zsh_option(option: Option):
|
|
|
|
return (
|
|
|
|
"'--"
|
|
|
|
+ option.long
|
|
|
|
+ "["
|
|
|
|
+ option.help.replace("[", "\[").replace("]", "\]")
|
|
|
|
+ "]: : ' \\"
|
|
|
|
)
|
|
|
|
|
|
|
|
|
2024-02-27 15:02:19 +03:00
|
|
|
def generate_powershell_completion(name: str, options: List[Option]):
|
|
|
|
return (
|
|
|
|
"""using namespace System.Management.Automation
|
|
|
|
using namespace System.Management.Automation.Language
|
|
|
|
|
|
|
|
Register-ArgumentCompleter -Native -CommandName '"""
|
|
|
|
+ name
|
|
|
|
+ """' -ScriptBlock {
|
|
|
|
param($wordToComplete, $commandAst, $cursorPosition)
|
|
|
|
|
|
|
|
$commandElements = $commandAst.CommandElements
|
|
|
|
$command = @(
|
|
|
|
'"""
|
|
|
|
+ name
|
|
|
|
+ """'
|
|
|
|
for ($i = 1; $i -lt $commandElements.Count; $i++) {
|
|
|
|
$element = $commandElements[$i]
|
|
|
|
if ($element -isnot [StringConstantExpressionAst] -or
|
|
|
|
$element.StringConstantType -ne [StringConstantType]::BareWord -or
|
|
|
|
$element.Value.StartsWith('-') -or
|
|
|
|
$element.Value -eq $wordToComplete) {
|
|
|
|
break
|
|
|
|
}
|
|
|
|
$element.Value
|
|
|
|
}) -join ';'
|
|
|
|
|
|
|
|
$completions = @(switch ($command) {
|
|
|
|
'"""
|
|
|
|
+ name
|
|
|
|
+ """'
|
|
|
|
{"""
|
|
|
|
+ "\n ".join(
|
|
|
|
[
|
|
|
|
"[CompletionResult]::new('--"
|
|
|
|
+ option.long
|
|
|
|
+ "', '"
|
|
|
|
+ option.long
|
|
|
|
+ "', [CompletionResultType]::ParameterName, '"
|
|
|
|
+ option.help
|
|
|
|
+ "')"
|
|
|
|
for option in options
|
|
|
|
]
|
|
|
|
)
|
|
|
|
+ """
|
|
|
|
[CompletionResult]::new('--help', 'help', [CompletionResultType]::ParameterName, 'Print help')
|
|
|
|
[CompletionResult]::new('--version', 'version', [CompletionResultType]::ParameterName, 'Print version')"""
|
|
|
|
+ """
|
|
|
|
break
|
|
|
|
}
|
|
|
|
})
|
|
|
|
|
|
|
|
$completions.Where{ $_.CompletionText -like "$wordToComplete*" } |
|
|
|
|
Sort-Object -Property ListItemText
|
|
|
|
}
|
|
|
|
"""
|
|
|
|
)
|