From 93b20ba4dc14d2c6ab7a15b481125aafadb55bb5 Mon Sep 17 00:00:00 2001 From: Daniel Langbein Date: Wed, 28 Jun 2023 16:15:46 +0200 Subject: [PATCH] feat: argparse --- src/netcup_dns/main.py | 24 ++++++++++++++++-------- 1 file changed, 16 insertions(+), 8 deletions(-) diff --git a/src/netcup_dns/main.py b/src/netcup_dns/main.py index 75c39a7..a8b7d31 100644 --- a/src/netcup_dns/main.py +++ b/src/netcup_dns/main.py @@ -1,8 +1,8 @@ #!/usr/bin/env python3 # -*- coding: utf-8 -*- +import argparse import ipaddress import json -import sys from functools import lru_cache from pathlib import Path from typing import Type @@ -17,13 +17,8 @@ def main(): """ The main effort is done by https://github.com/nbuchwitz/nc_dnsapi """ - if len(sys.argv) > 1: - cfg_dir = Path(sys.argv[1]) - else: - cfg_dir = Path('/etc/netcup-dns') - - if not cfg_dir.exists(): - raise Exception(f'The config directory is missing: {cfg_dir}') + args = parse_args() + cfg_dir: Path = args.cfg_dir cfg_files = [file for file in cfg_dir.iterdir() if file.name.endswith('.json') and file.is_file()] for cfg_file in cfg_files: @@ -59,6 +54,19 @@ def main(): print(f'The {hostname}.{domain} {type_} record points already to {destination}') +def parse_args(): + parser = argparse.ArgumentParser(description='Update DNS A/AAAA records with your current external IP address' + ' using the netcup DNS API.') + parser.add_argument('--config-directory', + help='Path to directory where `.json` config files reside.', + dest='cfg_dir', + default=Path('/etc/netcup-dns'), + type=Path) + args = parser.parse_args() + + if not args.cfg_dir.exists(): + raise Exception(f'The given config directory does not exist: {args.cfg_dir}') + def update_record_destination(api: Client, domain: str, hostname: str, type_: str, destination: str) -> bool: """ Updates the `destination` of the DNS record identified by `domain`, `hostname` and `type`.