mirror of
https://github.com/no2chem/wideq.git
synced 2025-05-16 07:10:09 -07:00
Use argparse for example
This commit is contained in:
parent
8615be9722
commit
0317c81bdf
32
example.py
32
example.py
@ -1,7 +1,7 @@
|
|||||||
import wideq
|
import wideq
|
||||||
import json
|
import json
|
||||||
import time
|
import time
|
||||||
import sys
|
import argparse
|
||||||
|
|
||||||
STATE_FILE = 'wideq_state.json'
|
STATE_FILE = 'wideq_state.json'
|
||||||
|
|
||||||
@ -134,15 +134,12 @@ EXAMPLE_COMMANDS = {
|
|||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
def example_command(client, args):
|
def example_command(client, cmd, args):
|
||||||
if not args:
|
func = EXAMPLE_COMMANDS[cmd]
|
||||||
ls(client)
|
func(client, *args)
|
||||||
else:
|
|
||||||
func = EXAMPLE_COMMANDS[args[0]]
|
|
||||||
func(client, *args[1:])
|
|
||||||
|
|
||||||
|
|
||||||
def example(args):
|
def example(cmd, args):
|
||||||
# Load the current state for the example.
|
# Load the current state for the example.
|
||||||
try:
|
try:
|
||||||
with open(STATE_FILE) as f:
|
with open(STATE_FILE) as f:
|
||||||
@ -159,7 +156,7 @@ def example(args):
|
|||||||
# Loop to retry if session has expired.
|
# Loop to retry if session has expired.
|
||||||
while True:
|
while True:
|
||||||
try:
|
try:
|
||||||
example_command(client, args)
|
example_command(client, cmd, args)
|
||||||
break
|
break
|
||||||
|
|
||||||
except wideq.NotLoggedInError:
|
except wideq.NotLoggedInError:
|
||||||
@ -172,5 +169,20 @@ def example(args):
|
|||||||
json.dump(state, f)
|
json.dump(state, f)
|
||||||
|
|
||||||
|
|
||||||
|
def main():
|
||||||
|
"""The main command-line entry point.
|
||||||
|
"""
|
||||||
|
parser = argparse.ArgumentParser(
|
||||||
|
description='Interact with the LG SmartThinQ API.'
|
||||||
|
)
|
||||||
|
parser.add_argument('cmd', metavar='CMD', nargs='?', default='ls',
|
||||||
|
help='one of {}'.format(', '.join(EXAMPLE_COMMANDS)))
|
||||||
|
parser.add_argument('args', metavar='ARGS', nargs='*',
|
||||||
|
help='subcommand arguments')
|
||||||
|
|
||||||
|
args = parser.parse_args()
|
||||||
|
example(args.cmd, args.args)
|
||||||
|
|
||||||
|
|
||||||
if __name__ == '__main__':
|
if __name__ == '__main__':
|
||||||
example(sys.argv[1:])
|
main()
|
||||||
|
Loading…
x
Reference in New Issue
Block a user