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.
This commit is contained in:
2026-01-01 23:17:33 +01:00
parent bd65c623a4
commit 885050cf84

View File

@@ -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 <https://gnu.org/licenses/gpl.html>."""
epilog="Copyright © 2025 Paul Retourné.\n"
"License GPLv3+: GNU GPL version 3 or later <https://gnu.org/licenses/gpl.html>.",
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