Make help message generation robust.

This commit is contained in:
2024-06-30 00:37:18 +02:00
parent de30d7a4af
commit 854fba049d
4 changed files with 173 additions and 5 deletions

View File

@@ -1,3 +1,5 @@
from pathlib import Path
from iottb import definitions
import logging
from iottb.utils.user_interaction import tb_echo
@@ -9,3 +11,6 @@ log_dir = definitions.LOGDIR
# Ensure logs dir exists before new handlers are registered in main.py
if not log_dir.is_dir():
log_dir.mkdir()
DOCS_FOLDER = Path.cwd() / 'docs'

View File

@@ -46,6 +46,14 @@ def validate_sniff(ctx, param, value):
return value
def run_pre(pre):
pass
def run_post(post):
pass
@click.command('sniff', help='Sniff packets with tcpdump')
@optgroup.group('Testbed sources')
@optgroup.option('--db', '--database', type=str, envvar='IOTTB_DB', show_envvar=True,
@@ -79,10 +87,13 @@ def validate_sniff(ctx, param, value):
@click.argument('device', required=False)
@click.pass_context
def sniff(ctx, device, interface, print_pacno, ff, count, monitor_mode, print_ll, address, db, unsafe, guided,
app, tcpdump_args, **params):
app, tcpdump_args, pre, post, **params):
""" Sniff packets from a device """
logger.info('sniff command invoked')
# Step 0: run pre script:
if pre:
click.echo(f'Running pre command {pre}')
run_pre(pre)
# Step1: Load Config
config = ctx.obj['CONFIG']
logger.debug(f'Config loaded: {config}')
@@ -325,3 +336,6 @@ def sniff(ctx, device, interface, print_pacno, ff, count, monitor_mode, print_ll
json.dump(metadata, f, indent=4)
click.echo(f'END SNIFF SUBCOMMAND')
if post:
click.echo(f'Running post command {post}')
run_post(post)

View File

@@ -1,7 +1,9 @@
from pathlib import Path
import click
from io import StringIO
import sys
from iottb import DOCS_FOLDER
# Import your CLI app here
from iottb.main import cli
@@ -28,7 +30,7 @@ def get_help_text(command):
def write_help_to_file(cli, filename):
"""Write help messages of all commands and subcommands to a file."""
with open(filename, 'w') as f:
with open(filename, 'w+') as f:
# main
f.write(f"Main Command: iottb\n")
f.write(get_help_text(cli))
@@ -49,4 +51,9 @@ def write_help_to_file(cli, filename):
if __name__ == "__main__":
write_help_to_file(cli, "help_messages.md")
from iottb import DOCS_FOLDER
print('Must be in project root for this to work properly!')
print(f'CWD is {str(Path.cwd())}')
DOCS_FOLDER.mkdir(exist_ok=True)
write_help_to_file(cli, str(DOCS_FOLDER / "help_messages.md"))
print(f'Wrote help_messages.md to {str(DOCS_FOLDER / "help_messages.md")}')