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
|
2024-03-06 03:38:30 +03:00
|
|
|
_arguments "${_arguments_options[@]}" \\\n\
|
2024-02-28 11:11:28 +03:00
|
|
|
"""
|
2024-03-07 20:04:28 +03:00
|
|
|
+ """\n """.join(zsh_option(option) for option in options)
|
2024-02-28 11:11:28 +03:00
|
|
|
+ """
|
2024-03-07 20:04:28 +03:00
|
|
|
'--help[Print help]' \\
|
|
|
|
'--version[Print version]' \\
|
2024-03-06 03:38:30 +03:00
|
|
|
'*:file:_files' \\
|
2024-02-28 11:11:28 +03:00
|
|
|
&& ret=0
|
2024-03-07 20:04:28 +03:00
|
|
|
|
2024-02-28 11:11:28 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
(( $+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):
|
2024-03-06 03:38:30 +03:00
|
|
|
help = option.help.replace("[", r"\[").replace("]", r"\]")
|
2024-03-07 20:04:28 +03:00
|
|
|
option_specifier = ""
|
|
|
|
cardinality = ""
|
|
|
|
action = ""
|
|
|
|
|
|
|
|
# TODO: Handle conflicting arguments like --color/--no-color
|
|
|
|
|
2024-03-06 03:38:30 +03:00
|
|
|
if option.append:
|
2024-03-06 16:17:46 +03:00
|
|
|
cardinality = "*"
|
2024-03-07 20:04:28 +03:00
|
|
|
|
|
|
|
if option.value == "FILE" or option.value == "PATH":
|
|
|
|
action = ": :_files"
|
|
|
|
# This ensures that the completion waits for user input
|
|
|
|
# TODO: Either check for a pattern using _guard
|
|
|
|
# Or display a help message using _message
|
|
|
|
elif option.value:
|
|
|
|
action = ": :"
|
|
|
|
|
|
|
|
if option.short and option.long:
|
|
|
|
option_specifier = (
|
|
|
|
f"(-{option.short} --{option.long})'{{-{option.short},--{option.long}}}'"
|
|
|
|
)
|
|
|
|
elif option.long:
|
|
|
|
option_specifier = f"--{option.long}"
|
|
|
|
elif option.short:
|
|
|
|
option_specifier = f"-{option.short}"
|
2024-03-06 03:38:30 +03:00
|
|
|
else:
|
2024-03-07 20:04:28 +03:00
|
|
|
raise ValueError("No long or short option specified")
|
|
|
|
|
|
|
|
return f"'{cardinality}{option_specifier}[{help}]{action}' \\"
|
2024-02-28 11:11:28 +03:00
|
|
|
|
|
|
|
|
2024-02-28 16:08:18 +03:00
|
|
|
def generate_fish_completion(name: str, options: List[Option]):
|
|
|
|
return (
|
|
|
|
"\n".join([fish_option(name, option) for option in options])
|
|
|
|
+ """
|
|
|
|
complete -c """
|
|
|
|
+ name
|
|
|
|
+ """ -l help -d 'Print help'
|
|
|
|
complete -c """
|
|
|
|
+ name
|
|
|
|
+ """ -l version -d 'Print version'
|
|
|
|
"""
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
|
|
def fish_option(name: str, option: Option):
|
|
|
|
return "complete -c " + name + " -l " + option.long + " -d '" + option.help + "'"
|
|
|
|
|
|
|
|
|
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
|
|
|
|
}
|
|
|
|
"""
|
|
|
|
)
|