From efaeb2196677ff99dc1aa636bea46bd7f89f8102 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E9=82=B9=E6=98=8E=E5=93=B2?= Date: Sat, 2 Dec 2023 15:06:18 +0800 Subject: [PATCH] enhance scripts/activate-global-python-argcomplete (#461) * automatically answer yes for all questions * avoid repeatedly appending to configuration files --- scripts/activate-global-python-argcomplete | 10 ++++++++++ 1 file changed, 10 insertions(+) diff --git a/scripts/activate-global-python-argcomplete b/scripts/activate-global-python-argcomplete index fc0a90e1..e98dec73 100755 --- a/scripts/activate-global-python-argcomplete +++ b/scripts/activate-global-python-argcomplete @@ -81,6 +81,11 @@ def install_to_destination(dest): def get_consent(): + try: + if args.yes is True: + return True + except NameError: + pass while True: res = input("OK to proceed? [y/n] ") if res.lower() not in {"y", "n", "yes", "no"}: @@ -93,6 +98,10 @@ def get_consent(): def append_to_config_file(path, shellcode): if os.path.exists(path): + with open(path, 'r') as fh: + if shellcode in fh.read(): + print(f"The code already exists in the file {path}.", file=sys.stderr) + return print(f"argcomplete needs to append to the file {path}. The following code will be appended:", file=sys.stderr) for line in shellcode.splitlines(): print(">", line, file=sys.stderr) @@ -115,6 +124,7 @@ def link_user_rcfiles(): parser = argparse.ArgumentParser(description=__doc__, formatter_class=argparse.RawDescriptionHelpFormatter) +parser.add_argument("-y", "--yes", help="automatically answer yes for all questions", action="store_true") parser.add_argument("--dest", help='Specify the shell completion modules directory to install into, or "-" for stdout') parser.add_argument("--user", help="Install into user directory", action="store_true") argcomplete.autocomplete(parser)