Add command-line argument parsing

This commit is contained in:
Scott Weis KB2EAR 2025-06-24 12:06:01 -04:00 committed by K2IE
parent 19246878ed
commit b9e37c8656
1 changed files with 22 additions and 11 deletions

View File

@ -22,6 +22,7 @@ import configparser
import re import re
import logging import logging
from systemd.journal import JournalHandler from systemd.journal import JournalHandler
import argparse
log = logging.getLogger('noaacap') log = logging.getLogger('noaacap')
log.addHandler(JournalHandler(SYSLOG_IDENTIFIER='noaacap')) log.addHandler(JournalHandler(SYSLOG_IDENTIFIER='noaacap'))
@ -33,13 +34,23 @@ if len(sys.argv) == 2 and sys.argv[1] == '-v':
print("Copyright 2017-2024 by Daniel L. Srebnick\n") print("Copyright 2017-2024 by Daniel L. Srebnick\n")
sys.exit(0) sys.exit(0)
# Command-line argument parsing
parser = argparse.ArgumentParser(description="noaacap.py - NOAA CAP Parser for APRS.")
parser.add_argument('--config', '-c',
help='Path to the configuration file (default: /etc/noaacap.conf)',
default='/etc/noaacap.conf')
parser.add_argument('--dbfile', '-d',
help='Path to the database file (default: /dev/shm/noaaconf.db)',
default='/dev/shm/noaaconf.db')
args = parser.parse_args()
# We need this function early in execution # We need this function early in execution
def ErrExit(): def ErrExit():
log.error("Exiting") log.error("Exiting")
print() print()
exit(0) exit(0)
conffile = '/etc/noaacap.conf' conffile = args.config
if not os.path.isfile(conffile): if not os.path.isfile(conffile):
log.error(conffile + " not found") log.error(conffile + " not found")
@ -112,7 +123,7 @@ soup = BeautifulSoup(r.text, 'xml')
entries = soup.find_all('entry') entries = soup.find_all('entry')
count = len(entries) count = len(entries)
dbfile = '/dev/shm/noaaconf.db' dbfile = args.dbfile
if count == 0: if count == 0:
log.info("Exiting - no events found") log.info("Exiting - no events found")
if os.path.isfile(dbfile): if os.path.isfile(dbfile):
@ -194,9 +205,9 @@ for i in range(0, count):
EventBegin, EventEnd = vtecparse(VTEC) EventBegin, EventEnd = vtecparse(VTEC)
except: except:
log.debug("VTEC parse failed") log.debug("VTEC parse failed")
continue #Loop if error parsing P-VTEC continue #Loop if error parsing P-VTEC
if ProductClass != "/O": #Loop if not operational if ProductClass != "/O": #Loop if not operational
continue continue
EventEnd = "20" + EventEnd EventEnd = "20" + EventEnd