From 3a01e9021f7e7c5eafec996f4fd5d5613bb776ce Mon Sep 17 00:00:00 2001 From: Rob Browning Date: Sat, 27 Feb 2021 11:30:21 -0600 Subject: [PATCH] bup.main: drop unicode-only getopt It's relatively straightforward to replace it, and this will allow us to avoid the unnecessary unicode round-trip. Signed-off-by: Rob Browning Tested-by: Rob Browning --- lib/bup/main.py | 51 +++++++++++++++++++++++++++---------------------- 1 file changed, 28 insertions(+), 23 deletions(-) diff --git a/lib/bup/main.py b/lib/bup/main.py index 383611d..928f5a6 100755 --- a/lib/bup/main.py +++ b/lib/bup/main.py @@ -14,7 +14,7 @@ from importlib import import_module from pkgutil import iter_modules from subprocess import PIPE from threading import Thread -import errno, getopt, re, select, signal, subprocess +import errno, re, select, signal, subprocess from bup import compat, path, helpers from bup.compat import ( @@ -107,39 +107,44 @@ def usage(msg=""): log("\n%s\n" % msg) sys.exit(99) -argv = compat.get_argv() -if len(argv) < 2: +args = compat.get_argvb() +if len(args) < 2: usage() -# Handle global options. -try: - optspec = ['help', 'version', 'debug', 'profile', 'bup-dir=', - 'import-py-module='] - global_args, subcmd = getopt.getopt(argv[1:], '?VDd:', optspec) -except getopt.GetoptError as ex: - usage('error: %s' % ex.msg) - -subcmd = [argv_bytes(x) for x in subcmd] +## Parse global options help_requested = None do_profile = False bup_dir = None - -for opt in global_args: - if opt[0] in ['-?', '--help']: +args = args[1:] +while args: + arg = args[0] + if arg in (b'-?', b'--help'): help_requested = True - elif opt[0] in ['-V', '--version']: + args = args[1:] + elif arg in (b'-V', b'--version'): subcmd = [b'version'] - elif opt[0] in ['-D', '--debug']: + args = args[1:] + elif arg in (b'-D', b'--debug'): helpers.buglvl += 1 environ[b'BUP_DEBUG'] = b'%d' % helpers.buglvl - elif opt[0] in ['--profile']: + args = args[1:] + elif arg == b'--profile': do_profile = True - elif opt[0] in ['-d', '--bup-dir']: - bup_dir = argv_bytes(opt[1]) - elif opt[0] == '--import-py-module': - pass + args = args[1:] + elif arg in (b'-d', b'--bup-dir'): + if len(args) < 2: + usage('error: no path provided for %s option' % arg) + bup_dir = args[1] + args = args[2:] + elif arg == b'--import-py-module': + args = args[2:] + elif arg.startswith(b'-'): + usage('error: unexpected option "%s"' + % arg.decode('ascii', 'backslashescape')) else: - usage('error: unexpected option "%s"' % opt[0]) + break + +subcmd = args # Make BUP_DIR absolute, so we aren't affected by chdir (i.e. save -C, etc.). if bup_dir: -- 2.39.2