Skip to content

Commit 4535b21

Browse files
committed
Upgrade to clap 3
1 parent aa9e46f commit 4535b21

File tree

6 files changed

+141
-31
lines changed

6 files changed

+141
-31
lines changed

cargo-idf/Cargo.toml

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,9 +11,10 @@ embuild = { version = "0.27", path = "..", features = ["espidf"] }
1111
log = "0.4"
1212
env_logger = "0.8"
1313
cargo_metadata = "0.14"
14-
structopt = "0.3"
15-
clap-cargo = { version = "0.7", features = ["cargo_metadata"] }
14+
clap = { version = "3", features = ["derive"] }
15+
clap-cargo = { version = "0.8", features = ["cargo_metadata"] }
1616
escargot = "0.5"
1717
serde_json = "1"
1818
thiserror = "1"
19-
anyhow = "1"
19+
anyhow = "1"
20+
strum = { version = "0.23", features = ["derive"] }

cargo-idf/README.md

Lines changed: 27 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -17,9 +17,7 @@ All other options are applied only to the build.
1717
TODO: Add caution about setting optimization options in sdkconfig.
1818

1919
<details>
20-
<summary>
21-
Commands used
22-
</summary>
20+
<summary>Commands used</summary>
2321

2422
```console
2523
python esp-idf/tools/kconfig_new/prepare_kconfig_files.py
@@ -43,6 +41,32 @@ python -m menuconfig esp-idf/Kconfig
4341
</details>
4442

4543
## `cargo idf flash`
44+
45+
46+
<details>
47+
<summary>Commands used</summary>
48+
49+
```console
50+
cmake.exe
51+
-D IDF_PATH="..."
52+
-D SERIAL_TOOL="python esp-idf-v4.3.1/components/esptool_py/esptool/esptool.py --chip esp32c3"
53+
-D SERIAL_TOOL_ARGS="--before=default_reset --after=hard_reset write_flash @flash_args"
54+
-D WORKING_DIRECTORY=<out-dir>/build
55+
-P esp-idf-v4.3.1/components/esptool_py/run_serial_tool.cmake
56+
```
57+
58+
</details>
59+
4660
## `cargo idf monitor`
4761
## `cargo idf erase-flash`
62+
63+
<details>
64+
<summary>Commands used</summary>
65+
66+
```console
67+
COMMAND = cmd.exe /C "cd /D C:\Users\n3xed\.espressif\esp-idf-v4.3.1\components\esptool_py && C:\Users\n3xed\.espressif\tools\cmake\3.20.3\bin\cmake.exe -D IDF_PATH="C:/Users/n3xed/.espressif/esp-idf-v4.3.1" -D SERIAL_TOOL="python C:/Users/n3xed/.espressif/esp-idf-v4.3.1/components/esptool_py/esptool/esptool.py --chip esp32c3" -D SERIAL_TOOL_ARGS="erase_flash" -P run_serial_tool.cmake"
68+
```
69+
70+
</details>
71+
4872
## `cargo idf size`

cargo-idf/src/build.rs

Lines changed: 14 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,13 @@
11
use std::env;
2-
use std::ffi::{OsStr, OsString};
2+
use std::ffi::OsStr;
33
use std::io::BufReader;
44
use std::path::PathBuf;
55
use std::process::{Command, Stdio};
66

77
use anyhow::anyhow;
88
use cargo_metadata::{Message, Version};
9+
use clap::Args;
910
use embuild::utils::CmdError;
10-
use structopt::StructOpt;
1111

1212
#[derive(Debug, thiserror::Error)]
1313
pub enum BuildError {
@@ -27,12 +27,16 @@ pub enum BuildError {
2727
CargoMsgError(#[source] std::io::Error),
2828
}
2929

30-
#[derive(Debug, StructOpt)]
30+
#[derive(Debug, Args)]
31+
#[clap(help_heading = "BUILD OPTIONS")]
3132
pub struct BuildOpts {
32-
#[structopt(flatten)]
33+
#[clap(flatten)]
3334
manifest: clap_cargo::Manifest,
34-
#[structopt(flatten)]
35+
#[clap(flatten)]
3536
features: clap_cargo::Features,
37+
/// Build with release profile
38+
#[clap(long)]
39+
release: bool,
3640
}
3741

3842
pub struct BuildInfo {
@@ -65,19 +69,22 @@ pub fn run(opts: BuildOpts) -> Result<BuildInfo, BuildError> {
6569
drop(meta);
6670

6771
// Build the crate
68-
let mut cmd = Command::new(env::var_os("CARGO").unwrap_or_else(|| OsString::from("cargo")));
72+
let mut cmd = Command::new(env::var_os("CARGO").unwrap_or_else(|| "cargo".into()));
6973
cmd.args(&["build", "--message-format=json-diagnostic-rendered-ansi"])
7074
.stdout(Stdio::piped())
7175
.stderr(Stdio::inherit());
7276
if let Some(ref manifest) = opts.manifest.manifest_path {
73-
cmd.args(&[OsStr::new("--manifest-path"), manifest.as_os_str()]);
77+
cmd.args([OsStr::new("--manifest-path"), manifest.as_os_str()]);
7478
}
7579
if opts.features.all_features {
7680
cmd.arg("--all-features");
7781
}
7882
if opts.features.no_default_features {
7983
cmd.arg("--no-default-features");
8084
}
85+
if opts.release {
86+
cmd.arg("--release");
87+
}
8188
if !opts.features.features.is_empty() {
8289
cmd.args(&["--features", &opts.features.features.join(",")]);
8390
}

cargo-idf/src/flash.rs

Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
use std::ffi::OsStr;
2+
use std::path::PathBuf;
3+
use std::str::FromStr;
4+
5+
use anyhow::bail;
6+
use clap::{AppSettings, ArgEnum, Args};
7+
use strum::{Display, EnumString};
8+
9+
use crate::build;
10+
11+
#[derive(Args)]
12+
#[clap(global_setting = AppSettings::DisableVersionFlag)]
13+
pub struct FlashOpts {
14+
/// Which bootloader to flash [possible values: esp-idf, none, <file>]
15+
///
16+
/// - `esp-idf` will flash the bootloader compiled locally from the esp-idf.
17+
/// - `none` prevents flashing a bootloader.
18+
/// - `<file>` will flash the user provided binary file if it exists.
19+
#[clap(
20+
long,
21+
default_value_t = Bootloader::EspIdf,
22+
parse(try_from_os_str = Bootloader::try_from_os_str),
23+
verbatim_doc_comment
24+
)]
25+
bootloader: Bootloader,
26+
27+
/// How to flash the binary
28+
#[clap(long, arg_enum, default_value_t = Mode::Esptool)]
29+
mode: Mode,
30+
31+
#[clap(flatten)]
32+
build_opts: build::BuildOpts,
33+
}
34+
35+
#[derive(Debug, ArgEnum, Clone, Copy)]
36+
pub enum Mode {
37+
Esptool,
38+
Dfu,
39+
Uf2,
40+
}
41+
42+
#[derive(Debug, Clone, EnumString, Display)]
43+
#[strum(serialize_all = "kebab-case")]
44+
pub enum Bootloader {
45+
EspIdf,
46+
None,
47+
#[strum(default)]
48+
#[strum(to_string = "<file>")]
49+
File(PathBuf),
50+
}
51+
52+
impl Bootloader {
53+
pub fn try_from_os_str(arg: &OsStr) -> Result<Bootloader, anyhow::Error> {
54+
let val = if let Some(arg) = arg.to_str() {
55+
Bootloader::from_str(arg).unwrap()
56+
} else {
57+
Bootloader::File(arg.into())
58+
};
59+
60+
if let Bootloader::File(ref path) = val {
61+
if !path.is_file() {
62+
bail!("'{}' is not a file", path.display())
63+
}
64+
}
65+
Ok(val)
66+
}
67+
}
68+
69+
pub fn run(opts: FlashOpts) -> anyhow::Result<()> {
70+
Ok(())
71+
}

cargo-idf/src/main.rs

Lines changed: 17 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,30 +1,35 @@
1+
use clap::Subcommand;
12
use log::LevelFilter;
2-
use structopt::clap::AppSettings;
3-
use structopt::StructOpt;
3+
use clap::AppSettings;
4+
use clap::Parser;
45

56
mod build;
67
mod menuconfig;
8+
mod flash;
79

8-
#[derive(StructOpt)]
9-
#[structopt(global_setting = AppSettings::GlobalVersion)]
10-
#[structopt(bin_name = "cargo")]
10+
#[derive(Parser)]
11+
#[clap(global_setting = AppSettings::PropagateVersion)]
12+
#[clap(global_setting = AppSettings::DeriveDisplayOrder)]
13+
#[clap(version)]
14+
#[clap(bin_name = "cargo")]
1115
struct Opts {
12-
#[structopt(subcommand)]
16+
#[clap(subcommand)]
1317
sub_cmd: CargoSubCommand,
1418
}
1519

16-
#[derive(StructOpt)]
20+
#[derive(Subcommand)]
1721
enum CargoSubCommand {
22+
#[clap(subcommand)]
1823
Idf(CargoIdfOpts),
1924
}
2025

21-
#[derive(StructOpt)]
26+
#[derive(Subcommand)]
2227
enum CargoIdfOpts {
2328
Menuconfig(menuconfig::MenuconfigOpts),
24-
Flash,
25-
EraseFlash,
29+
Flash(flash::FlashOpts),
2630
Monitor,
2731
Size,
32+
EraseFlash,
2833
}
2934

3035
fn main() -> anyhow::Result<()> {
@@ -39,9 +44,10 @@ fn main() -> anyhow::Result<()> {
3944
.format_timestamp(None)
4045
.init();
4146

42-
let CargoSubCommand::Idf(opts) = Opts::from_args().sub_cmd;
47+
let CargoSubCommand::Idf(opts) = Opts::parse().sub_cmd;
4348
match opts {
4449
CargoIdfOpts::Menuconfig(opts) => menuconfig::run(opts)?,
50+
CargoIdfOpts::Flash(opts) => flash::run(opts)?,
4551
_ => unimplemented!(),
4652
};
4753

cargo-idf/src/menuconfig.rs

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ use cargo_metadata::Version;
88
use embuild::espidf::{self, EspIdfBuildInfo};
99
use embuild::utils::CmdError;
1010
use embuild::{cmd, path_buf};
11-
use structopt::StructOpt;
11+
use clap::{Args, AppSettings};
1212

1313
use crate::build::{self, BuildError, BuildInfo};
1414

@@ -30,16 +30,17 @@ pub enum MenuconfigError {
3030
EspIdfSysTooOld(Version),
3131
}
3232

33-
#[derive(StructOpt)]
33+
#[derive(Args)]
34+
#[clap(global_setting = AppSettings::DeriveDisplayOrder)]
35+
#[clap(global_setting = AppSettings::DisableVersionFlag)]
3436
pub struct MenuconfigOpts {
35-
#[structopt(flatten)]
36-
build_opts: build::BuildOpts,
37-
/// Path to the esp-idf build info json file.
37+
/// Optional path to the esp-idf build info json file.
3838
///
39-
/// If this option is not specified cargo-idf will perform a `cargo build` in the
39+
/// If this argument is not specified cargo-idf will perform a `cargo build` in the
4040
/// current directory.
41-
#[structopt(long)]
4241
idf_build_info: Option<PathBuf>,
42+
#[clap(flatten)]
43+
build_opts: build::BuildOpts,
4344
}
4445

4546
pub fn run(opts: MenuconfigOpts) -> Result<(), MenuconfigError> {

0 commit comments

Comments
 (0)