Skip to content

Commit cea0dbe

Browse files
jettisonjoeKenadia
authored andcommitted
Add a 'config-help' CLI flag. (#717)
* Add a 'config-help' CLI flag. If enabled, this flag will cause the framework to print all config keys and their descriptions and exit rather than executing the test. Example output: ble_sniffer_download_port ------------------------- Port for HTTP server for downloading sniffer logs. ble_sniffer_enable ------------------ (no description found) ble_sniffer_host ---------------- Host running BLE sniffer software. ble_sniffer_local_path ---------------------- Local path to access BLE Sniffer logs. ble_sniffer_port ---------------- Port the BLE sniffer is running on. ble_sniffer_usb_hub_channel --------------------------- If the acroname_usb_hub_serial exists and can be commanded, this config key specifies the channel that the BLE Sniffer is connected to. * Add default value printing per reviewer feedback. Example output: ble_sniffer_download_port ------------------------- Port for HTTP server for downloading sniffer logs. default_value=55455 ble_sniffer_enable ------------------ (no description found) default_value=False ble_sniffer_host ---------------- Host running BLE sniffer software. default_value="localhost" ble_sniffer_local_path ---------------------- Local path to access BLE Sniffer logs. default_value="" ble_sniffer_port ---------------- Port the BLE sniffer is running on. default_value=22901 * Remove extra newline.
1 parent 250deaa commit cea0dbe

File tree

2 files changed

+34
-5
lines changed

2 files changed

+34
-5
lines changed

openhtf/__init__.py

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -190,7 +190,10 @@ def configure(self, **kwargs):
190190
"""Update test-wide configuration options. See TestOptions for docs."""
191191
# These internally ensure they are safe to call multiple times with no weird
192192
# side effects.
193-
create_arg_parser(add_help=True).parse_known_args()
193+
known_args, _ = create_arg_parser(add_help=True).parse_known_args()
194+
if known_args.config_help:
195+
sys.stdout.write(conf.help_text)
196+
sys.exit(0)
194197
logs.setup_logger()
195198
for key, value in kwargs.items():
196199
setattr(self._test_options, key, value)
@@ -328,9 +331,14 @@ def create_arg_parser(add_help=False):
328331
'My args title', parents=[openhtf.create_arg_parser()])
329332
>>> parser.parse_args()
330333
"""
331-
return argparse.ArgumentParser('OpenHTF-based testing', parents=[
334+
parser = argparse.ArgumentParser('OpenHTF-based testing', parents=[
332335
conf.ARG_PARSER, phase_executor.ARG_PARSER, logs.ARG_PARSER],
333336
add_help=add_help)
337+
parser.add_argument(
338+
'--config-help', action='store_true',
339+
help='Instead of executing the test, simply print all available config '
340+
'keys and their description strings.')
341+
return parser
334342

335343

336344
# Result of a phase.

openhtf/util/conf.py

Lines changed: 24 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -239,17 +239,17 @@ def __init__(self, logger, lock, parser, **kwargs):
239239
self._modules = kwargs
240240
self._declarations = {}
241241
self.ARG_PARSER = parser
242-
242+
243243
# Parse just the flags we care about, since this happens at import time.
244244
self._flags, _ = parser.parse_known_args()
245245
self._flag_values = {}
246-
246+
247247
# Populate flag_values from flags now.
248248
self.load_flag_values()
249249

250250
# Initialize self._loaded_values and load from --config-file if it's set.
251251
self.reset()
252-
252+
253253
def load_flag_values(self, flags=None):
254254
"""Load flag values given from command line flags.
255255
@@ -446,6 +446,27 @@ def _asdict(self):
446446
retval[key] = value
447447
return retval
448448

449+
@property
450+
def help_text(self):
451+
"""Return a string with all config keys and their descriptions."""
452+
result = []
453+
for name in sorted(self._declarations.keys()):
454+
result.append(name)
455+
result.append('-' * len(name))
456+
decl = self._declarations[name]
457+
if decl.description:
458+
result.append(decl.description.strip())
459+
else:
460+
result.append('(no description found)')
461+
if decl.has_default:
462+
result.append('')
463+
quotes = '"' if type(decl.default_value) is str else ''
464+
result.append(' default_value={quotes}{val}{quotes}'.format(
465+
quotes=quotes, val=decl.default_value))
466+
result.append('')
467+
result.append('')
468+
return '\n'.join(result)
469+
449470
def save_and_restore(self, _func=None, **config_values):
450471
"""Decorator for saving conf state and restoring it after a function.
451472

0 commit comments

Comments
 (0)