Skip to content
This repository was archived by the owner on Jun 15, 2025. It is now read-only.

Commit e22cb5c

Browse files
committed
Rework sub-command dispatching
The dispatching of sub-commands has always been a bit messy, with string comparisons that are prone to become outdated once changes are made. To that end, this change reworks this dispatching and gets rid of this problem. We use the subparser's set_defaults method to unconditionally set the 'method' attribute which can be invoked uniformly by callers.
1 parent d8c8705 commit e22cb5c

File tree

1 file changed

+6
-11
lines changed

1 file changed

+6
-11
lines changed

btrfs-backup/src/deso/btrfs/main.py

Lines changed: 6 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -304,6 +304,7 @@ def addBackupParser(parser):
304304
"backup", add_help=False, formatter_class=SubLevelHelpFormatter,
305305
help="Backup one or more subvolumes.",
306306
)
307+
backup.set_defaults(method=lambda x: x.backup)
307308

308309
required = backup.add_argument_group("Required arguments")
309310
addRequiredArgs(required)
@@ -329,6 +330,7 @@ def addRestoreParser(parser):
329330
"restore", add_help=False, formatter_class=SubLevelHelpFormatter,
330331
help="Restore subvolumes or snapshots from a repository.",
331332
)
333+
restore.set_defaults(method=lambda x: x.restore)
332334

333335
required = restore.add_argument_group("Required arguments")
334336
addRequiredArgs(required)
@@ -386,7 +388,7 @@ def prepare(filters):
386388
else:
387389
return split(filters)
388390

389-
command = ns.command
391+
method = ns.method
390392
src_repo = ns.src
391393
dst_repo = ns.dst
392394
remote_cmd = ns.remote_cmd
@@ -422,8 +424,9 @@ def prepare(filters):
422424
del ns.src
423425
del ns.dst
424426
del ns.command
427+
del ns.method
425428

426-
return command, subvolumes, src_repo, dst_repo
429+
return method, subvolumes, src_repo, dst_repo
427430

428431

429432
def main(argv):
@@ -455,15 +458,7 @@ def main(argv):
455458
namespace = parser.parse_args(args)
456459

457460
with alias(namespace) as ns:
458-
command, subvolumes, src_repo, dst_repo = prepareNamespace(ns)
459-
460-
if command == "backup":
461-
method = lambda x: x.backup
462-
elif command == "restore":
463-
method = lambda x: x.restore
464-
else:
465-
assert False
466-
461+
method, subvolumes, src_repo, dst_repo = prepareNamespace(ns)
467462
return run(method, subvolumes, src_repo, dst_repo, **vars(ns))
468463

469464

0 commit comments

Comments
 (0)