Skip to content

Commit

Permalink
Tweak how command line options are parsed.
Browse files Browse the repository at this point in the history
  • Loading branch information
gareth-palmer committed Mar 22, 2021
1 parent 4499f71 commit 02c5ee7
Show file tree
Hide file tree
Showing 6 changed files with 50 additions and 34 deletions.
12 changes: 7 additions & 5 deletions cgiexecute
Original file line number Diff line number Diff line change
Expand Up @@ -87,16 +87,18 @@ def main():

for option, argument in options:
if option in ('-h', '--host'):
if not re.search(r'(?xi) ^ (?: [a-z0-9\-]+ \.)* [a-z0-9\-]+ $', argument):
raise ProgramError(f'Invalid host: {argument}')

hostname = argument

if not re.search(r'(?xi) ^ (?: [a-z0-9\-]+ \.)* [a-z0-9\-]+ $', hostname):
raise ProgramError(f'Invalid host: {hostname}')

elif option in ('-t', '--timeout'):
timeout = argument

try:
timeout = int(argument)
timeout = int(timeout)
except ValueError:
raise ProgramError(f'Invalid timeout: {argument}')
raise ProgramError(f'Invalid timeout: {timeout}')

elif option in ('-u', '--username'):
username = argument
Expand Down
24 changes: 15 additions & 9 deletions mediastream
Original file line number Diff line number Diff line change
Expand Up @@ -275,39 +275,45 @@ def main():
wav_file = argument

elif option in ('-t', '--timeout'):
timeout = argument

try:
timeout = int(argument)
timeout = int(timeout)
except ValueError:
raise ProgramError(f'Invalid timeout: {argument}')
raise ProgramError(f'Invalid timeout: {timeout}')

elif option in ('-m', '--multicast'):
multicast_address = argument

try:
multicast_address = ipaddress.IPv4Address(argument)
multicast_address = ipaddress.IPv4Address(multicast_address)

if not multicast_address.is_multicast:
raise ipaddress.AddressValueError

except ipaddress.AddressValueError:
raise ProgramError(f'Invalid multicast IP-address: {argument}')
raise ProgramError(f'Invalid multicast IP-address: {multicast_address}')

multicast_address = multicast_address.compressed

elif option in ('-v', '--volume'):
volume = argument

try:
volume = int(argument)
volume = int(volume)

if volume < 0 or volume > 100:
raise ValueError

except ValueError:
raise ProgramError(f'Invalid volume: {argument}')
raise ProgramError(f'Invalid volume: {volume}')

elif option in ('-c', '--codec'):
if argument not in ('g711', 'g722'):
raise ProgramError(f'Invalid codec: {argument}')

codec = argument

if codec not in ('g711', 'g722'):
raise ProgramError(f'Invalid codec: {codec}')

elif option in ('-u', '--username'):
username = argument

Expand Down
12 changes: 7 additions & 5 deletions screenshot
Original file line number Diff line number Diff line change
Expand Up @@ -59,16 +59,18 @@ def main():

for option, argument in options:
if option in ('-h', '--host'):
if not re.search(r'(?xi) ^ (?: [a-z0-9\-]+ \.)* [a-z0-9\-]+ $', argument):
raise ProgramError(f'Invalid host: {argument}')

hostname = argument

if not re.search(r'(?xi) ^ (?: [a-z0-9\-]+ \.)* [a-z0-9\-]+ $', hostname):
raise ProgramError(f'Invalid host: {hostname}')

elif option in ('-t', '--timeout'):
timeout = argument

try:
timeout = int(argument)
timeout = int(timeout)
except ValueError:
raise ProgramError(f'Invalid timeout: {argument}')
raise ProgramError(f'Invalid timeout: {timeout}')

elif option in ('-u', '--username'):
username = argument
Expand Down
12 changes: 7 additions & 5 deletions setbackground
Original file line number Diff line number Diff line change
Expand Up @@ -61,16 +61,18 @@ def main():

for option, argument in options:
if option in ('-h', '--host'):
if not re.search(r'(?xi) ^ (?: [a-z0-9\-]+ \.)* [a-z0-9\-]+ $', argument):
raise ProgramError(f'Invalid host: {argument}')

hostname = argument

if not re.search(r'(?xi) ^ (?: [a-z0-9\-]+ \.)* [a-z0-9\-]+ $', hostname):
raise ProgramError(f'Invalid host: {hostname}')

elif option in ('-t', '--timeout'):
timeout = argument

try:
timeout = int(argument)
timeout = int(timeout)
except ValueError:
raise ProgramError(f'Invalid timeout: {argument}')
raise ProgramError(f'Invalid timeout: {timeout}')

elif option in ('-u', '--username'):
username = argument
Expand Down
12 changes: 7 additions & 5 deletions setringtone
Original file line number Diff line number Diff line change
Expand Up @@ -58,16 +58,18 @@ def main():

for option, argument in options:
if option in ('-h', '--host'):
if not re.search(r'(?xi) ^ (?: [a-z0-9\-]+ \.)* [a-z0-9\-]+ $', argument):
raise ProgramError(f'Invalid host: {argument}')

hostname = argument

if not re.search(r'(?xi) ^ (?: [a-z0-9\-]+ \.)* [a-z0-9\-]+ $', hostname):
raise ProgramError(f'Invalid host: {hostname}')

elif option in ('-t', '--timeout'):
timeout = argument

try:
timeout = int(argument)
timeout = int(timeout)
except ValueError:
raise ProgramError(f'Invalid timeout: {argument}')
raise ProgramError(f'Invalid timeout: {timeout}')

elif option in ('-u', '--username'):
username = argument
Expand Down
12 changes: 7 additions & 5 deletions xmlinfo
Original file line number Diff line number Diff line change
Expand Up @@ -63,16 +63,18 @@ def main():

for option, argument in options:
if option in ('-h', '--host'):
if not re.search(r'(?xi) ^ (?: [a-z0-9\-]+ \.)* [a-z0-9\-]+ $', argument):
raise ProgramError(f'Invalid host: {argument}')

hostname = argument

if not re.search(r'(?xi) ^ (?: [a-z0-9\-]+ \.)* [a-z0-9\-]+ $', hostname):
raise ProgramError(f'Invalid host: {hostname}')

elif option in ('-t', '--timeout'):
timeout = argument

try:
timeout = int(argument)
timeout = int(timeout)
except ValueError:
raise ProgramError(f'Invalid timeout: {argument}')
raise ProgramError(f'Invalid timeout: {timeout}')

elif option in ('-u', '--username'):
username = argument
Expand Down

0 comments on commit 02c5ee7

Please sign in to comment.