mirror of https://github.com/tuhiproject/tuhi.git
kete/prompt: use argparse instead of home made parser
parent
b33afcce0c
commit
60252f8649
|
@ -580,26 +580,29 @@ class TuhiKeteShell(cmd.Cmd):
|
||||||
'''list known devices'''
|
'''list known devices'''
|
||||||
self.start_worker(Printer)
|
self.start_worker(Printer)
|
||||||
|
|
||||||
_listen_usage = 'Usage: listen 12:34:56:AB:CD:EF [on|off]'
|
def help_listen(self):
|
||||||
|
self.do_listen('-h')
|
||||||
|
|
||||||
def do_listen(self, args):
|
def do_listen(self, args):
|
||||||
'''Listen to a specific device:
|
'''Listen to a specific device'''
|
||||||
Usage: listen 12:34:56:AB:CD:EF [on|off]'''
|
parser = argparse.ArgumentParser(prog='listen',
|
||||||
|
description='Listen to a specific device',
|
||||||
if args is '':
|
add_help=False)
|
||||||
print(self._listen_usage)
|
parser.add_argument('-h', action='help', help=argparse.SUPPRESS)
|
||||||
return
|
parser.add_argument('address', metavar='12:34:56:AB:CD:EF',
|
||||||
|
type=TuhiKeteDevice.is_device_address,
|
||||||
args = args.split(' ')
|
default=None,
|
||||||
address = args[0]
|
help='the address of the device to listen to')
|
||||||
|
parser.add_argument('mode', choices=['on', 'off'], nargs='?',
|
||||||
|
const='on', default='on')
|
||||||
try:
|
try:
|
||||||
mode = args[1]
|
parsed_args = parser.parse_args(args.split())
|
||||||
except IndexError:
|
except SystemExit:
|
||||||
mode = 'on'
|
|
||||||
|
|
||||||
if mode != 'on' and mode != 'off':
|
|
||||||
print(self._listen_usage)
|
|
||||||
return
|
return
|
||||||
|
|
||||||
|
address = parsed_args.address
|
||||||
|
mode = parsed_args.mode
|
||||||
|
|
||||||
for d in self._manager.devices:
|
for d in self._manager.devices:
|
||||||
if d.address == address:
|
if d.address == address:
|
||||||
if mode == 'on' and d.listening:
|
if mode == 'on' and d.listening:
|
||||||
|
|
Loading…
Reference in New Issue