-
Notifications
You must be signed in to change notification settings - Fork 6.5k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[rfc-dtsh 28/29] cli: devicetree shell CLI
Run the devicetree shell without West. Signed-off-by: Christophe Dufaza <chris@openmarl.org>
- Loading branch information
Showing
1 changed file
with
98 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,98 @@ | ||
# Copyright (c) 2023 Christophe Dufaza <chris@openmarl.org> | ||
# | ||
# 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() |