From a50c1472b1b5f34e6edb9f4f38f6075f146be4f1 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Mike=20F=C3=A4hrmann?= Date: Mon, 25 Dec 2023 15:15:59 +0100 Subject: [PATCH] fix and update zsh completion (#4972) - backslash-escape '[' and ']' - replace single quotes with '\'' instead of " - ignore SUPPRESSed options --- scripts/completion_zsh.py | 15 +++++++++++---- 1 file changed, 11 insertions(+), 4 deletions(-) diff --git a/scripts/completion_zsh.py b/scripts/completion_zsh.py index f2fcdd62..78f8dfd0 100755 --- a/scripts/completion_zsh.py +++ b/scripts/completion_zsh.py @@ -10,32 +10,39 @@ """Generate zsh completion script from gallery-dl's argument parser""" import util +import argparse from gallery_dl import option - TEMPLATE = """#compdef gallery-dl local curcontext="$curcontext" typeset -A opt_args local rc=1 -_arguments -C -S \\ +_arguments -s -S \\ %(opts)s && rc=0 return rc """ +TR = str.maketrans({ + "'": "'\\''", + "[": "\\[", + "]": "\\]", +}) + + opts = [] for action in option.build_parser()._actions: - if not action.option_strings: + if not action.option_strings or action.help == argparse.SUPPRESS: continue elif len(action.option_strings) == 1: opt = action.option_strings[0] else: opt = "{" + ",".join(action.option_strings) + "}" - opt += "'[" + action.help.replace("'", '"') + "]'" + opt += "'[" + action.help.translate(TR) + "]'" if action.metavar: opt += ":'<" + action.metavar.lower() + ">'"