Repository: esp-rs/espflash (language: rust)

Issue:
-p option does not work as expected
Without `--list-all-ports`, `detect_usb_serial_ports()` returns only `SerialPortType::UsbPort`. When the device is not recognized as `SerialPortType::UsbPort` for some reasons, `--port` will not work.

The help says "`--list-all-ports` List all available ports" but it is confusing. It does not "list ports". Instead, the option does different things. When a port is specified in `espflash.toml` or with `-p` and the port is found, it uses the found port without asking users. If not specified, it asks users to choose ports found by an underlying library. 

What I expect :

```console
> espflash board-info -p /dev/cuaU0     
```

should work.

The actual result:

```console
> espflash board-info -p /dev/cuaU0                 
Error: espflash::serial_not_found

  × The serial port '/dev/cuaU0' could not be found
  help: Make sure the correct device is connected to the host system

> ls /dev/cuaU0
/dev/cuaU0
```

The workaround is, always use `--list-all-ports`.

If you cannot change the behavior, there should be an entry for it in `espflash.toml`.

```
[connection]
serial = "/dev/cuaU0"
list_all_ports = true
```


Discussion / hints:

Fix patch:
diff --git a/CHANGELOG.md b/CHANGELOG.md
index e54bfb56..de5c4a5e 100644
--- a/CHANGELOG.md
+++ b/CHANGELOG.md
@@ -13,6 +13,8 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
 
 ### Fixed
 
+- Only filter the list of available serial ports if a port has not been specified via CLI option or configuration file (#693)
+
 ### Removed
 
 ## [3.2.0]
diff --git a/espflash/src/cli/serial.rs b/espflash/src/cli/serial.rs
index 993efd5d..ab71630b 100644
--- a/espflash/src/cli/serial.rs
+++ b/espflash/src/cli/serial.rs
@@ -33,13 +33,14 @@ pub fn get_serial_port_info(
     // doesn't work (on Windows) with "dummy" device paths like `COM4`. That's
     // the reason we need to handle Windows/Posix differently.
 
-    let ports = detect_usb_serial_ports(matches.list_all_ports).unwrap_or_default();
-
     if let Some(serial) = &matches.port {
+        let ports = detect_usb_serial_ports(true).unwrap_or_default();
         find_serial_port(&ports, serial)
     } else if let Some(serial) = &config.connection.serial {
+        let ports = detect_usb_serial_ports(true).unwrap_or_default();
         find_serial_port(&ports, serial)
     } else {
+        let ports = detect_usb_serial_ports(matches.list_all_ports).unwrap_or_default();
         let (port, matches) = select_serial_port(ports, config, matches.confirm_port)?;
 
         match &port.port_type {


Test patch: