handle the expanding filters better

This commit is contained in:
Isaac 2023-05-04 23:23:29 -07:00
parent b4a38d8be9
commit 8f3039be23
No known key found for this signature in database
GPG key ID: E69FB5A841448A48

View file

@ -67,16 +67,35 @@ def bash_completion():
) )
file_metavars = ( # fish section
'FILENAME',
'PATH',
)
file_destinations = 'config_paths' def has_file_options(action: Action):
return action.metavar in (
'FILENAME',
'PATH',
) or action.dest in ('config_paths',)
def has_choice_options(action: Action):
return action.choices is not None
def has_required_param_options(action: Action):
return (
action.nargs
in (
"+",
"*",
)
or '--archive' in action.option_strings
or action.metavar in ('PATTERN', 'KEYS', 'N')
)
def has_exact_options(action: Action): def has_exact_options(action: Action):
return action.metavar in file_metavars or action.dest in file_destinations or action.choices return (
has_file_options(action) or has_choice_options(action) or has_required_param_options(action)
)
def exact_options_completion(action: Action): def exact_options_completion(action: Action):
@ -93,12 +112,15 @@ def exact_options_completion(action: Action):
args = ' '.join(action.option_strings) args = ' '.join(action.option_strings)
if action.metavar in file_metavars or action.dest in file_destinations: if has_file_options(action):
return f'''\ncomplete -c borgmatic -Fr -n "__borgmatic_last_arg {args}"''' return f'''\ncomplete -c borgmatic -Fr -n "__borgmatic_last_arg {args}"'''
if action.choices: if has_choice_options(action):
return f'''\ncomplete -c borgmatic -f -a '{' '.join(map(str, action.choices))}' -n "__borgmatic_last_arg {args}"''' return f'''\ncomplete -c borgmatic -f -a '{' '.join(map(str, action.choices))}' -n "__borgmatic_last_arg {args}"'''
if has_required_param_options(action):
return f'''\ncomplete -c borgmatic -x -n "__borgmatic_last_arg {args}"'''
raise RuntimeError( raise RuntimeError(
f'Unexpected action: {action} passes has_exact_options but has no choices produced' f'Unexpected action: {action} passes has_exact_options but has no choices produced'
) )