From 3ae74ef3cc3876672e667879a7006276e851e861 Mon Sep 17 00:00:00 2001 From: Sven Hoexter Date: Thu, 31 Mar 2022 22:18:51 +0200 Subject: [PATCH] Move enverbridge setid script from optparse to argparse --- home/enverbridge.py | 30 ++++++++++++++---------------- 1 file changed, 14 insertions(+), 16 deletions(-) diff --git a/home/enverbridge.py b/home/enverbridge.py index 9f86907..04cbe0e 100755 --- a/home/enverbridge.py +++ b/home/enverbridge.py @@ -14,18 +14,21 @@ # port 8765. Wait for a response paket to the broadcast address # on port 8764. -from optparse import OptionParser +import argparse import socket import sys import re -parser = OptionParser(usage="usage: %prog [options] MIIDs") -parser.add_option("-b", "--bid", - action="store", - type="int", - dest="bid", - help="Serial Number of your EnverBridge") -(options, args) = parser.parse_args() +parser = argparse.ArgumentParser() +parser.add_argument("-b", + "--bid", + action="store", + type=int, + dest="bid", + required=True, + help="Serial Number of your EnverBridge") +parser.add_argument("miids", nargs="+", help="MIIDs of your MicroInverter") +args = parser.parse_args() def validateMIID(miids): @@ -49,17 +52,12 @@ def buildMIIDList(miids): return msg -# minimal input validation -if len(args) < 1: - print("Error: missing MIIDs, provide at least one inverter serial") - sys.exit(22) - -validateMIID(args) +validateMIID(args.miids) # assemble message string and convert to bytes -message = str(options.bid) + buildMIIDList(args) +message = str(args.bid) + buildMIIDList(args.miids) message = message.encode() -print('Sending to EnverBridge with ID', options.bid) +print('Sending to EnverBridge with ID', args.bid) # Create a UDP client socket w/ broadcast sock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM, socket.IPPROTO_UDP) -- 2.39.5