Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

How to Read a boolean flag pass through CLI in vanilla rust

I want to pass boolean flags to a program in rust. However, I do not want to use any external crates. How would I do something like that?

Example:

Command: cargo run -- -t
output: -t flag enabled

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

To hand-roll your CLI, you want to use the std::env module which has the args() function:

https://doc.rust-lang.org/book/ch12-01-accepting-command-line-arguments.html

use std::env;

fn main() {
    let args: Vec<String> = env::args().collect();
    dbg!(args);
}

So now all you want to do is whether or not the list of arguments contains -t Let me know if you have trouble with that part. 👍

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading