From 885050cf84a6b6c7954b580f5229e67e0d2407a8 Mon Sep 17 00:00:00 2001 From: furtest Date: Thu, 1 Jan 2026 23:17:33 +0100 Subject: [PATCH] argparser : adds subcommands to the argparser this adds subcommands to the argparser using subparsers, we also set a default value for func depending on which of the subcommands is selected. Also change the formatting of the epilog so it is on two lines. --- src/unisync/argparser.py | 22 ++++++++++++++++++---- 1 file changed, 18 insertions(+), 4 deletions(-) diff --git a/src/unisync/argparser.py b/src/unisync/argparser.py index dda0eb0..ba28700 100644 --- a/src/unisync/argparser.py +++ b/src/unisync/argparser.py @@ -3,20 +3,34 @@ import argparse -def create_argparser() -> argparse.ArgumentParser: +def create_argparser(sync_function, add_function, mount_function) -> argparse.ArgumentParser: + """ + Creates an argument parser to parse the command line arguments. + We use subparsers and set a default function for each to perform the correct action. + """ parser = argparse.ArgumentParser( prog='unisync', description='File synchronisation application', - epilog=""" - Copyright © 2025 Paul Retourné. - License GPLv3+: GNU GPL version 3 or later .""" + epilog="Copyright © 2025 Paul Retourné.\n" + "License GPLv3+: GNU GPL version 3 or later .", + formatter_class=argparse.RawDescriptionHelpFormatter ) parser.add_argument("local", nargs="?") parser.add_argument("remote", nargs="?") + parser.set_defaults(func=sync_function) remote_addr_group = parser.add_mutually_exclusive_group() remote_addr_group.add_argument("--ip") remote_addr_group.add_argument("--hostname") parser.add_argument("--config", help="Path to the configuration file", metavar="path_to_config") + + subparsers = parser.add_subparsers(help='Actions other than synchronisation') + + parser_add = subparsers.add_parser('add', help='Add files to be synchronised.') + parser_add.set_defaults(func=add_function) + + parser_mount = subparsers.add_parser('mount', help='Mount the remote.') + parser_mount.set_defaults(func=mount_function) + return parser