Skip to content

Parse argument in the form "1234:5678" #128

Answered by potto216
potto216 asked this question in Q&A
Discussion options

You must be logged in to vote

Thanks for the advice. I tried implementing your suggestion below:


fn parse_device(input: &str) -> std::result::Result<(u16, u16),  &'static str> {
    let mut split = input.split(":");
    
    //TODO: switch to an iterator
    let vec: Vec<&str> = split.collect();
    println!("Device is {}:{}", vec[0], vec[1]);

    let vid =u16::from_str_radix(vec[0], 16);
    if vid.is_err()  {
        return Err("TODO: Error message about vid");
    }


    let pid =u16::from_str_radix(vec[1], 16);
    if pid.is_err()  {
        return Err("TODO: Error message about pid");
    }

    Ok((vid.unwrap(), pid.unwrap()))
}

...

    let device = short('d') // start with a short name
        .long("devic…

Replies: 3 comments 1 reply

Comment options

You must be logged in to vote
0 replies
Comment options

You must be logged in to vote
1 reply
@pacak
Comment options

Answer selected by potto216
Comment options

You must be logged in to vote
0 replies
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
Labels
None yet
2 participants