start work on conditional file completion
This commit is contained in:
parent
28efc85660
commit
f12a10d888
1 changed files with 16 additions and 9 deletions
|
@ -1,5 +1,6 @@
|
||||||
import shlex
|
import shlex
|
||||||
from argparse import Action
|
from argparse import Action
|
||||||
|
from textwrap import dedent
|
||||||
|
|
||||||
from borgmatic.commands import arguments
|
from borgmatic.commands import arguments
|
||||||
|
|
||||||
|
@ -66,14 +67,20 @@ def bash_completion():
|
||||||
)
|
)
|
||||||
|
|
||||||
|
|
||||||
def build_fish_flags(action: Action):
|
def conditionally_emit_file_completion(action: Action):
|
||||||
'''
|
'''
|
||||||
Given an argparse.Action instance, return a string containing the fish flags for that action.
|
Given an argparse.Action instance, return a completion invocation that forces file completion
|
||||||
|
if the action takes a file argument and was the last action on the command line.
|
||||||
|
|
||||||
|
Otherwise, return an empty string.
|
||||||
'''
|
'''
|
||||||
if action.metavar and action.metavar == 'PATH' or action.metavar == 'FILENAME':
|
if not action.metavar:
|
||||||
return '-r -F'
|
return ''
|
||||||
else:
|
|
||||||
return '-f'
|
args = ' '.join(action.option_strings)
|
||||||
|
|
||||||
|
return dedent(f'''
|
||||||
|
complete -c borgmatic -a {args} -Fr -n "__borgmatic_last_arg {args}"''')
|
||||||
|
|
||||||
|
|
||||||
def fish_completion():
|
def fish_completion():
|
||||||
|
@ -105,18 +112,18 @@ def fish_completion():
|
||||||
+ (f'''set --local subparser_condition "not __fish_seen_subcommand_from {all_subparsers}"''',)
|
+ (f'''set --local subparser_condition "not __fish_seen_subcommand_from {all_subparsers}"''',)
|
||||||
+ ('\n# subparser completions',)
|
+ ('\n# subparser completions',)
|
||||||
+ tuple(
|
+ tuple(
|
||||||
f'''complete -c borgmatic -n "$subparser_condition" -a '{action_name}' -d {shlex.quote(subparser.description)} -f'''
|
f'''complete -c borgmatic -f -n "$subparser_condition" -a '{action_name}' -d {shlex.quote(subparser.description)}'''
|
||||||
for action_name, subparser in subparsers.choices.items()
|
for action_name, subparser in subparsers.choices.items()
|
||||||
)
|
)
|
||||||
+ ('\n# global flags',)
|
+ ('\n# global flags',)
|
||||||
+ tuple(
|
+ tuple(
|
||||||
f'''complete -c borgmatic -a '{' '.join(action.option_strings)}' -d {shlex.quote(action.help)} {build_fish_flags(action)}'''
|
f'''complete -c borgmatic -f -a '{' '.join(action.option_strings)}' -d {shlex.quote(action.help)}{conditionally_emit_file_completion(action)}'''
|
||||||
for action in top_level_parser._actions
|
for action in top_level_parser._actions
|
||||||
if len(action.option_strings) > 0
|
if len(action.option_strings) > 0
|
||||||
)
|
)
|
||||||
+ ('\n# subparser flags',)
|
+ ('\n# subparser flags',)
|
||||||
+ tuple(
|
+ tuple(
|
||||||
f'''complete -c borgmatic -a '{' '.join(action.option_strings)}' -d {shlex.quote(action.help)} -n "__fish_seen_subcommand_from {action_name}" {build_fish_flags(action)}'''
|
f'''complete -c borgmatic -f -a '{' '.join(action.option_strings)}' -d {shlex.quote(action.help)} -n "__fish_seen_subcommand_from {action_name}"{conditionally_emit_file_completion(action)}'''
|
||||||
for action_name, subparser in subparsers.choices.items()
|
for action_name, subparser in subparsers.choices.items()
|
||||||
for action in subparser._actions
|
for action in subparser._actions
|
||||||
)
|
)
|
||||||
|
|
Loading…
Reference in a new issue