From 9868adaa0b11671d7f69e36f986e65f2fc9dc0b2 Mon Sep 17 00:00:00 2001 From: Kevin Hester Date: Thu, 25 Mar 2021 09:37:17 +0800 Subject: [PATCH] use reflection to list valid device settings --- meshtastic/__main__.py | 27 +++++++++++++++++---------- 1 file changed, 17 insertions(+), 10 deletions(-) diff --git a/meshtastic/__main__.py b/meshtastic/__main__.py index 8f6ac01..4263f10 100644 --- a/meshtastic/__main__.py +++ b/meshtastic/__main__.py @@ -169,17 +169,24 @@ def printNodes(nodes, myId): def setPref(attributes, name, valStr): """Set a channel or preferences value""" val = fromStr(valStr) - try: - try: - setattr(attributes, name, val) - except TypeError as ex: - # The setter didn't like our arg type guess try again as a string - setattr(attributes, name, valStr) - # succeeded! - print(f"Set {name} to {valStr}") - except Exception as ex: - print(f"Can't set {name} due to {ex}") + if not hasattr(attributes, name): + print(f"{attributes.__class__.__name__} doesn't have an attribute called {name}, so you can not set it.") + print(f"Choices are:") + for f in attributes.DESCRIPTOR.fields: + print(f" {f.name}") + else: + try: + try: + setattr(attributes, name, val) + except TypeError as ex: + # The setter didn't like our arg type guess try again as a string + setattr(attributes, name, valStr) + + # succeeded! + print(f"Set {name} to {valStr}") + except Exception as ex: + print(f"Can't set {name} due to {ex}") targetNode = None