Skip to content

Commit ac5a481

Browse files
authored
Error when both --erase and --format are specified in flash
1 parent d3227ca commit ac5a481

2 files changed

Lines changed: 20 additions & 0 deletions

File tree

mpflash/cli_flash.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -270,6 +270,10 @@ def _create_worklist_or_fail(*create_args, **create_kwargs) -> FlashTaskList:
270270
params.bootloader = BootloaderMethod(params.bootloader)
271271
reflash_commands: List[str] = []
272272

273+
# --erase and --format are mutually exclusive; only one makes sense at a time.
274+
if params.erase and params.format_fs:
275+
raise click.UsageError("--erase and --format cannot be used together; specify only one.")
276+
273277
# make it simple for the user to flash one board by asking for the serial port if not specified
274278
if params.boards == ["?"] or params.serial == "?": # or params.variant == "?":
275279
params.serial = ["?"]

tests/cli/test_cli_flash.py

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -210,6 +210,22 @@ def test_mpflash_flash_no_matching_serial_ports_returns_usage_error(mocker: Mock
210210
assert "No serial ports matched" in result.output
211211

212212

213+
def test_mpflash_flash_erase_and_format_are_mutually_exclusive(mocker: MockerFixture):
214+
"""Reject using --erase and --format together with a user-friendly error."""
215+
args = ["flash", "--board", "RPI_PICO2", "--serial", "COM8", "--erase", "--format"]
216+
217+
mocker.patch(
218+
"mpflash.ask_input.ask_missing_params",
219+
Mock(side_effect=fake_ask_missing_params),
220+
)
221+
222+
runner = CliRunner()
223+
result = runner.invoke(cli_main.cli, args, standalone_mode=True)
224+
225+
assert result.exit_code == 2
226+
assert "--erase and --format cannot be used together" in result.output
227+
228+
213229
def test_mpflash_flash_format_option_passed_to_flash_tasks(mocker: MockerFixture):
214230
"""The --format flag is forwarded to flash_tasks as format_fs=True."""
215231
args = ["flash", "--board", "RPI_PICO2", "--serial", "COM8", "--volume", "D:\\", "--format"]

0 commit comments

Comments
 (0)