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:
@@ -3,20 +3,34 @@
|
|||||||
|
|
||||||
import argparse
|
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(
|
parser = argparse.ArgumentParser(
|
||||||
prog='unisync',
|
prog='unisync',
|
||||||
description='File synchronisation application',
|
description='File synchronisation application',
|
||||||
epilog="""
|
epilog="Copyright © 2025 Paul Retourné.\n"
|
||||||
Copyright © 2025 Paul Retourné.
|
"License GPLv3+: GNU GPL version 3 or later <https://gnu.org/licenses/gpl.html>.",
|
||||||
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("local", nargs="?")
|
||||||
parser.add_argument("remote", nargs="?")
|
parser.add_argument("remote", nargs="?")
|
||||||
|
parser.set_defaults(func=sync_function)
|
||||||
|
|
||||||
remote_addr_group = parser.add_mutually_exclusive_group()
|
remote_addr_group = parser.add_mutually_exclusive_group()
|
||||||
remote_addr_group.add_argument("--ip")
|
remote_addr_group.add_argument("--ip")
|
||||||
remote_addr_group.add_argument("--hostname")
|
remote_addr_group.add_argument("--hostname")
|
||||||
|
|
||||||
parser.add_argument("--config", help="Path to the configuration file", metavar="path_to_config")
|
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
|
return parser
|
||||||
|
|||||||
Reference in New Issue
Block a user