Skip to content

Commit 42f83c1

Browse files
committed
✨ Add the ability to pass a file on the command line
Closes #1.
1 parent 15f71fd commit 42f83c1

2 files changed

Lines changed: 27 additions & 8 deletions

File tree

src/dhv/__main__.py

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
from argparse import ArgumentParser, BooleanOptionalAction, Namespace
66
from inspect import cleandoc
77
from operator import attrgetter
8+
from pathlib import Path
89

910
##############################################################################
1011
# Local imports.
@@ -66,6 +67,14 @@ def get_args() -> Namespace:
6667
help="Set the theme for the application (set to ? to list available themes)",
6768
)
6869

70+
# An optional file to open.
71+
parser.add_argument(
72+
"source",
73+
nargs="?",
74+
type=Path,
75+
help="A Python source file to disassemble",
76+
)
77+
6978
# The remainder is going to be the initial command.
7079
parser.add_argument(
7180
"command",

src/dhv/screens/main.py

Lines changed: 18 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -99,10 +99,26 @@ def compose(self) -> ComposeResult:
9999
yield Disassembly()
100100
yield Footer()
101101

102+
def _show_source(self, source: Path) -> None:
103+
"""Load up the content of a Python source file.
104+
105+
Args:
106+
source: The path to the source file to load.
107+
"""
108+
try:
109+
self.query_one(Source).load_text(source.read_text())
110+
except IOError as error:
111+
self.notify(str(error), title=f"Unable to load {source}", severity="error")
112+
return
113+
with update_configuration() as config:
114+
config.last_load_location = str(source.absolute().parent)
115+
102116
def on_mount(self) -> None:
103117
"""Configure the display once the DOM is mounted."""
104118
self.query_one(Disassembly).show_offset = load_configuration().show_offsets
105119
self.query_one(Disassembly).show_opcodes = load_configuration().show_opcodes
120+
if isinstance(to_open := self._arguments.source, Path):
121+
self._show_source(to_open)
106122

107123
def _watch_horizontal_layout(self) -> None:
108124
"""React to the horizontal layout setting being changed."""
@@ -133,7 +149,7 @@ def action_new_code_command(self) -> None:
133149

134150
@work
135151
async def action_load_file_command(self) -> None:
136-
"""Load the content of a file."""
152+
"""Browse for and open a Python source file."""
137153
if not (
138154
start_location := Path(load_configuration().last_load_location or ".")
139155
).is_dir():
@@ -153,13 +169,7 @@ async def action_load_file_command(self) -> None:
153169
),
154170
)
155171
):
156-
try:
157-
self.query_one(Source).load_text(python_file.read_text())
158-
except IOError as error:
159-
self.notify(str(error), title="Unable to load that file")
160-
return
161-
with update_configuration() as config:
162-
config.last_load_location = str(python_file.absolute().parent)
172+
self._show_source(python_file)
163173

164174
def action_switch_layout_command(self) -> None:
165175
"""Switch the layout of the window."""

0 commit comments

Comments
 (0)