Skip to content

Commit

Permalink
[rfc-dtsh 27/28] cli: devicetree shell CLI
Browse files Browse the repository at this point in the history
Run the devicetree shell without West.

Signed-off-by: Christophe Dufaza <[email protected]>
  • Loading branch information
dottspina committed Aug 2, 2023
1 parent 593aa39 commit 7501ebd
Showing 1 changed file with 98 additions and 0 deletions.
98 changes: 98 additions & 0 deletions scripts/dts/dtsh/src/dtsh/cli.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,98 @@
# Copyright (c) 2023 Christophe Dufaza <[email protected]>
#
# SPDX-License-Identifier: Apache-2.0

"""Devicetree shell CLI.
Run the devicetree shell without West.
"""

from typing import Optional, List

import argparse

from dtsh.session import DtShSession
from dtsh.rich.session import DtShRichSession
from dtsh.shell import DtShError


class DtShCliArgv:
"""Command line arguments parser."""

_parser: argparse.ArgumentParser
_argv: argparse.Namespace

def __init__(self) -> None:
"""Initialize parser.
Command line arguments:
- "--bindings-search-path": Explicitly set the directories to search
for binding files
- "--proletarian": fail-safe to proletarian (aka not rich) session
Command line parameter: path to a DTS file in Devicetree source format
"""
self._parser = argparse.ArgumentParser(
prog="dtsh",
description="shell-like interface with a devicetree",
# See e.g. https://github.com/zephyrproject-rtos/zephyr/issues/53495
allow_abbrev=False,
)
self._parser.add_argument(
"--proletarian",
help="fail-safe to proletarian (aka not rich) session",
action="store_true",
)
self._parser.add_argument(
"--bindings-search-path",
help="directories to search for binding files",
required=False,
nargs="+",
metavar="PATH",
)
self._parser.add_argument(
"dts",
help="path to the Devicetree source file",
nargs=1,
metavar="DTS",
)
self._argv = self._parser.parse_args()

@property
def proletarian(self) -> bool:
"""Whether to fail-safe to a basic (aka not rich) session."""
return self._argv.proletarian

@property
def bindings_search_path(self) -> Optional[List[str]]:
"""Directories to search for binding files."""
return self._argv.bindings_search_path

@property
def dts(self) -> str:
"""Path to the Devicetree source file."""
return self._argv.dts[0]


def run() -> None:
"""Open a devicetree shell session and run its interactive loop."""
argv = DtShCliArgv()

session = None
try:
if argv.proletarian:
session = DtShSession.create(argv.dts, argv.bindings_search_path)
else:
session = DtShRichSession.create(
argv.dts, argv.bindings_search_path
)
except DtShError as e:
print(e)

if session:
session.run()


if __name__ == "__main__":
run()

0 comments on commit 7501ebd

Please sign in to comment.