-
I want to parse a command line argument that contains a USB device's vendor and product ID in hexadecimal that is in the form "1234:5678" similar to Linux's
Is this the most efficient way to parse this value? |
Beta Was this translation helpful? Give feedback.
Replies: 3 comments 1 reply
-
I'd use So have a function that takes a string and returns a pair of numbers or an error and call that. let parse_device(input: &str) -> Result<&'static str, (u16, u16)> {
// it should be possible to parse this without allocations - just remember that split you get is an iterator and you can call
// next to get the next value, then call `from_str_radix` on that but transform the error to something helpful.
} then use this function inside let device = short('d')
.long("device")
.help("[vendor]:[product] Open a device with the specified vendor and product ID. Both IDs are given in hexadecimal.")
.argument::<String>("Device")
.parse(parse_device); This way you will get a better error message if something goes wrong. |
Beta Was this translation helpful? Give feedback.
-
Thanks for the advice. I tried implementing your suggestion below:
But I get the following compiler errors. It seems like the type in
|
Beta Was this translation helpful? Give feedback.
-
Thanks that worked (below).
|
Beta Was this translation helpful? Give feedback.
Thanks for the advice. I tried implementing your suggestion below: