Browse Source

added option handling and command-line help [fixes #1]

master
Tamás Gérczei 1 month ago
parent
commit
09b2a8f5f2
Signed by: tgerczei GPG Key ID: 5B59A7760597B1A1
1 changed files with 10 additions and 7 deletions
  1. +10
    -7
      hap.py

+ 10
- 7
hap.py View File

@@ -4,13 +4,16 @@ from kubernetes import client, config
import sys, random

SVC_TYPE = 'LoadBalancer'
PORT_COUNT = 1

if len(sys.argv) > 1:
try:
PORT_COUNT = int(sys.argv[-1])
except:
sys.exit('optional positional parameter must be a number (default: 1)')
def getopts():
"""parse command-line"""
from optparse import OptionParser, OptionValueError
usage = "%prog [options]"
parser = OptionParser(usage=usage)
parser.add_option("-n", "--count", type="int", default=1, dest="port_count", help="number of ports needed (default: %default)")
return parser.parse_args()

options, args = getopts()

# load client configuration (kubeconfig)
config.load_kube_config()
@@ -29,4 +32,4 @@ full_port_range = range(49152, 65536) # https://tools.ietf.org/html/rfc6335#sect
available_ports = list(set(full_port_range) - set(used_ports))

# pick available ports
print(' '.join(map(str, random.sample(available_ports, PORT_COUNT))))
print(' '.join(map(str, random.sample(available_ports, options.port_count))))

Loading…
Cancel
Save