Skip to content

Commit d455e7a

Browse files
committed
wip
1 parent 1ac5a98 commit d455e7a

File tree

2 files changed

+77
-15
lines changed

2 files changed

+77
-15
lines changed

stm32cubeprogrammer-cli/src/main.rs

Lines changed: 39 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,37 @@
1+
// TODO: Add doctest for commands from readme
2+
// const CLI_COMMAND: &str = "unprotect ble-update --file stack.bin --address 0x123 flash-bin --file app.bin --address 0x456 protect reset";
3+
4+
// // Include the command in the docstring dynamically
5+
// #[doc = concat!(
6+
// "Example usage of the CLI.\n\n",
7+
// "```sh\n",
8+
// "my_cli_tool ", CLI_COMMAND, "\n",
9+
// "```\n\n",
10+
// "(The following test is hidden from the documentation.)"
11+
// )]
12+
// ///
13+
// /// ```
14+
// /// # use my_crate::{options, TargetCommand, BleStackInfo, BinFileInfo, HexAddress, ResetMode};
15+
// /// # use std::path::PathBuf;
16+
// /// # std::env::set_var("STM32_CUBE_PROGRAMMER_DIR", "some/dir");
17+
// ///
18+
// /// // Use the same command string in the test
19+
// /// let command = CLI_COMMAND;
20+
// ///
21+
// /// // Run the CLI parsing logic
22+
// /// let value = options()
23+
// /// .run_inner(command.split(' ').collect::<Vec<_>>().as_slice())
24+
// /// .unwrap();
25+
// /// ```
26+
// #[derive(Default)]
27+
// /// Track the connection state of the programmer
28+
// enum ConnectionState<'a> {
29+
// #[default]
30+
// Disconnected,
31+
// Connected(Option<stm32cubeprogrammer::ConnectedProgrammer<'a>>),
32+
// ConnectedFus(Option<stm32cubeprogrammer::ConnectedFusProgrammer<'a>>),
33+
// }
34+
135
//! This CLI aims to provide a simple interface for setting up stm32 targets.
236
//! # Supported commands
337
//! - Flashing bin and hex files
@@ -57,14 +91,8 @@ use log::{error, info};
5791
use std::sync::Mutex;
5892
use stm32cubeprogrammer::probe;
5993

60-
#[derive(Default)]
61-
/// Track the connection state of the programmer
62-
enum ConnectionState<'a> {
63-
#[default]
64-
Disconnected,
65-
Connected(Option<stm32cubeprogrammer::ConnectedProgrammer<'a>>),
66-
ConnectedFus(Option<stm32cubeprogrammer::ConnectedFusProgrammer<'a>>),
67-
}
94+
95+
6896

6997
/// Helper struct to manage the programmer connection
7098
struct ProgrammerConnection<'a> {
@@ -264,7 +292,7 @@ fn main_inner() -> Result<crate::output::Output, anyhow::Error> {
264292
if options
265293
.target_commands
266294
.iter()
267-
.any(|command| matches!(command, parse::TargetCommand::UpdateBleStack(_)))
295+
.any(|command| matches!(command, parse::TargetCommand::BleUpdate(_)))
268296
&& !programmer_connection
269297
.connection()?
270298
.general_information()
@@ -310,7 +338,7 @@ fn main_inner() -> Result<crate::output::Output, anyhow::Error> {
310338

311339
output::CommandOutput::FlashHex { file }
312340
}
313-
parse::TargetCommand::UpdateBleStack(ble_stack_info) => {
341+
parse::TargetCommand::BleUpdate(ble_stack_info) => {
314342
log::info!("Update BLE stack: {}", ble_stack_info);
315343

316344
display_handler
@@ -363,7 +391,7 @@ fn main_inner() -> Result<crate::output::Output, anyhow::Error> {
363391
.unwrap_or(output::BleStackUpdated::NotUpdated),
364392
}
365393
}
366-
parse::TargetCommand::BleStackInfo { compare } => {
394+
parse::TargetCommand::BleInfo { compare } => {
367395
display_handler
368396
.lock()
369397
.unwrap()

stm32cubeprogrammer-cli/src/parse.rs

Lines changed: 38 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ use bpaf::*;
22
use stm32cubeprogrammer::utility::HexAddress;
33

44
#[derive(Debug, Clone, Bpaf)]
5-
#[bpaf(options)]
5+
#[bpaf(options, version)]
66
/// Overall arguments for CLI
77
/// Note: Be careful with duplicate short or long flag names in the subcommands and the parent struct
88
pub struct Options {
@@ -58,11 +58,11 @@ pub enum TargetCommand {
5858

5959
#[bpaf(command, adjacent)]
6060
/// Update the BLE stack on the target
61-
UpdateBleStack(#[bpaf(external(ble_stack_info))] BleStackInfo),
61+
BleUpdate(#[bpaf(external(ble_stack_info))] BleStackInfo),
6262

6363
/// Read BLE stack information
6464
#[bpaf(command, adjacent)]
65-
BleStackInfo {
65+
BleInfo {
6666
#[bpaf(long, fallback(None))]
6767
/// The flash address in format 0x123 or 0X123 where file should be written
6868
compare: Option<stm32cubeprogrammer::fus::Version>,
@@ -261,7 +261,41 @@ mod test {
261261
value.target_commands,
262262
vec![
263263
TargetCommand::Unprotect,
264-
TargetCommand::UpdateBleStack(BleStackInfo {
264+
TargetCommand::BleUpdate(BleStackInfo {
265+
file: std::path::PathBuf::from("stack.bin"),
266+
address: HexAddress(0x123),
267+
version: None,
268+
force: false,
269+
}),
270+
TargetCommand::FlashBin(BinFileInfo {
271+
file: std::path::PathBuf::from("app.bin"),
272+
address: HexAddress(0x456),
273+
}),
274+
TargetCommand::Protect,
275+
TargetCommand::Reset(ResetMode::Hardware),
276+
]
277+
)
278+
}
279+
280+
#[test]
281+
fn parse_multi_two() {
282+
283+
std::env::set_var("STM32_CUBE_PROGRAMMER_DIR", "some/dir");
284+
285+
let command =
286+
"unprotect ble-update --file stack.bin --address 0x123 flash-bin --file app.bin --address 0x456 protect reset";
287+
288+
let value = options()
289+
.run_inner(command.split(' ').collect::<Vec<_>>().as_slice())
290+
.unwrap();
291+
292+
println!("{:?}", value);
293+
294+
assert_eq!(
295+
value.target_commands,
296+
vec![
297+
TargetCommand::Unprotect,
298+
TargetCommand::BleUpdate(BleStackInfo {
265299
file: std::path::PathBuf::from("stack.bin"),
266300
address: HexAddress(0x123),
267301
version: None,

0 commit comments

Comments
 (0)