Skip to content

Commit 4e97972

Browse files
committed
cli: fix crash when dfu action is omitted
Before: $ pybricksdev dfu Traceback (most recent call last): File "/home/david/work/pybricks/pybricksdev/.venv/bin/pybricksdev", line 5, in <module> main() File "/home/david/work/pybricks/pybricksdev/pybricksdev/cli/__init__.py", line 347, in main asyncio.run(subparsers.choices[args.tool].tool.run(args)) File "/home/david/work/pybricks/pybricksdev/pybricksdev/cli/__init__.py", line 258, in run return self.subparsers.choices[args.action].tool.run(args) KeyError: None After: $ pybricksdev dfu usage: pybricksdev dfu [-h] <action> ... pybricksdev dfu: error: Missing name of action: backup|restore
1 parent 147c337 commit 4e97972

File tree

2 files changed

+10
-3
lines changed

2 files changed

+10
-3
lines changed

CHANGELOG.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,8 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
99
- `pybricksdev.ble.lwp3.bytecodes` module.
1010
- `pybricksdev.ble.lwp3.messages` module.
1111
- `pybricksdev lwp3 repl` command line tool.
12+
## Fixed
13+
- Crash when running `pybricksdev dfu` without args on command line.
1214

1315
## [1.0.0-alpha.8] - 2021-05-18
1416
## Added

pybricksdev/cli/__init__.py

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -242,19 +242,24 @@ async def run(self, args: argparse.Namespace):
242242

243243
class DFU(Tool):
244244
def add_parser(self, subparsers: argparse._SubParsersAction):
245-
parser = subparsers.add_parser(
245+
self.parser = subparsers.add_parser(
246246
"dfu",
247247
help="use DFU to backup or restore firmware",
248248
)
249-
parser.tool = self
250-
self.subparsers = parser.add_subparsers(
249+
self.parser.tool = self
250+
self.subparsers = self.parser.add_subparsers(
251251
metavar="<action>", dest="action", help="the action to perform"
252252
)
253253

254254
for tool in DFUBackup(), DFURestore():
255255
tool.add_parser(self.subparsers)
256256

257257
def run(self, args: argparse.Namespace):
258+
if args.action not in self.subparsers.choices:
259+
self.parser.error(
260+
f'Missing name of action: {"|".join(self.subparsers.choices.keys())}'
261+
)
262+
258263
return self.subparsers.choices[args.action].tool.run(args)
259264

260265

0 commit comments

Comments
 (0)