· 5 years ago · Dec 02, 2020, 06:22 AM
1use structopt::StructOpt;
2use rusqlite::{params, Connection, Result};
3use rusqlite::NO_PARAMS;
4
5#[derive(Debug, StructOpt)]
6struct Cli {
7 #[structopt(name = "thought", short = "a", long = "add", help = "Add a thought to the database")]
8 thought: Option<String>,
9
10 #[structopt(short, long, conflicts_with("thought"), help = "Print a random thought")]
11 random: bool
12}
13
14fn main() -> Result<()> {
15 println!("Hello, world!");
16 let args = Cli::from_args();
17 let conn = Connection::open("test.db")?;
18 conn.execute(
19 "CREATE TABLE IF NOT EXISTS thoughts (
20 id INTEGER PRIMARY KEY,
21 thought TEXT NOT NULL
22 )",
23 NO_PARAMS,
24 )?;
25
26 if args.thought.is_some() {
27 conn.execute (
28 "INSERT INTO thoughts (thought) values (?1)",
29 params![&args.thought],
30 )?;
31 }
32 else if args.random {
33 // Make this print a random thought
34 println!("Random switch thrown!");
35 }
36
37
38 Ok(())
39}