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
@ -209,7 +220,7 @@ for i in range(0, count):
except: except:
exp = now exp = now
# log.debug('Time Now: ' + datetime.datetime.strftime(now,"%y-%m-%d %H:%M")) # log.debug('Time Now: ' + datetime.datetime.strftime(now,"%y-%m-%d %H:%M"))
# log.debug('Expiration: ' + datetime.datetime.strftime(exp,"%y-%m-%d %H:%M")) # log.debug('Expiration: ' + datetime.datetime.strftime(exp,"%y-%m-%d %H:%M"))
if now > exp: if now > exp:
@ -308,7 +319,7 @@ for i in range(0, count):
k += 1 k += 1
curr_item = j curr_item = j
try: try:
next_item = sorted_zcs[k] next_item = sorted_zcs[k]
except: except:
next_item = '' next_item = ''
@ -323,12 +334,12 @@ for i in range(0, count):
i_curr_plus1 = i_curr_suffix + 1 i_curr_plus1 = i_curr_suffix + 1
# Always print entire first item # Always print entire first item
if k == 1: if k == 1:
zcs = curr_item zcs = curr_item
# Did prefix change? # Did prefix change?
elif prev_prefix != curr_prefix: elif prev_prefix != curr_prefix:
zcs = zcs + "-" + curr_item zcs = zcs + "-" + curr_item
# Current suffix is 1 more than previous # Current suffix is 1 more than previous
elif i_prev_plus1 == i_curr_suffix: elif i_prev_plus1 == i_curr_suffix:
if i_next_suffix != i_curr_plus1: if i_next_suffix != i_curr_plus1:
zcs = zcs + ">" + curr_suffix zcs = zcs + ">" + curr_suffix
@ -341,7 +352,7 @@ for i in range(0, count):
# For debugging only # For debugging only
# print (zcs) # print (zcs)
prev_item = curr_item prev_item = curr_item
prev_prefix = curr_prefix prev_prefix = curr_prefix
prev_suffix = curr_suffix prev_suffix = curr_suffix
@ -361,7 +372,7 @@ for i in range(0, count):
log.info("Msg: " + message) log.info("Msg: " + message)
# Make sure message does not exceed 67 chars. If it does, trim it. # Make sure message does not exceed 67 chars. If it does, trim it.
n = 0 n = 0
while len(message) > 67: while len(message) > 67:
n = zcs.rfind('-') n = zcs.rfind('-')
@ -375,7 +386,7 @@ for i in range(0, count):
print(":NWS_" + event + ":" + message + line) print(":NWS_" + event + ":" + message + line)
if myResend > 0: if myResend > 0:
resend[id] = bytes(str(myResend), 'utf-8') resend[id] = bytes(str(myResend), 'utf-8')
break break
if hit == 0: if hit == 0: