use clap::{ArgMatches, App, SubCommand, Arg};
use serialport::{self, SerialPortType};
pub fn run(matches: &ArgMatches) -> Result<(), String> {
let verbose = matches.is_present("verbose");
let raw = matches.is_present("raw");
let ports = serialport::available_ports().unwrap();
for port in ports {
if verbose == false {
match port.port_type {
SerialPortType::UsbPort(ref info) if info.product.is_some() && raw == false => {
println!("{} ({})", port.port_name, info.product.as_ref().unwrap())
},
_ => println!("{}", port.port_name)
}
continue;
}
println!("{}", port.port_name);
match port.port_type {
SerialPortType::UsbPort(info) => {
println!(" Type: USB");
println!(" VID: {:04x}", info.vid);
println!(" PID: {:04x}", info.pid);
println!(" Serial Number: {}", info.serial_number.as_ref().map_or("", String::as_str));
println!(" Manufacturer: {}", info.manufacturer.as_ref().map_or("", String::as_str));
println!(" Product: {}", info.product.as_ref().map_or("", String::as_str));
}
SerialPortType::BluetoothPort => {
println!(" Type: Bluetooth");
}
SerialPortType::PciPort => {
println!(" Type: PCI");
}
SerialPortType::Unknown => {
println!(" Type: Unknown");
}
}
println!();
}
Ok(())
}
pub fn command<'a>() -> App<'a, 'a> {
SubCommand::with_name("list")
.about("List all available serial ports")
.arg(Arg::with_name("verbose")
.long("verbose")
.short("v")
.help("Print detailed information about each serial port"))
.arg(Arg::with_name("raw")
.long("raw")
.short("r")
.help("Print raw information (without product name after port)"))
}