1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
use crate::{text_from_editor, Args, Entry};
use clap::{Arg, ArgMatches, Command};

pub fn get_args() -> ArgMatches {
    Command::new("Notebook")
        .about("CLI utility for plaintext notetaking.")
        .subcommand_required(true)
        .arg_required_else_help(true)
        .subcommand(
            Command::new("new")
                .short_flag('n')
                .long_flag("new")
                .about("Create a new note")
                .arg(Arg::new("entry")),
        )
        .arg(
            Arg::new("notebook_name")
                .short('j')
                .long("notebook")
                .default_value("default")
                .help("Specify a notebook name"),
        )
        .subcommand(
            Command::new("list")
                .short_flag('l')
                .long_flag("list")
                .about("List entries")
                .arg(Arg::new("list").default_value("5")),
        )
        .arg(
            Arg::new("verbose")
                .short('v')
                .long("verbose")
                .help("Quantity of information")
                .action(clap::ArgAction::Count),
        )
        .subcommand(
            Command::new("edit")
                .short_flag('e')
                .long_flag("edit")
                .about("Edit specific entry")
                .arg(Arg::new("edit")),
        )
        .subcommand(
            Command::new("read")
                .short_flag('r')
                .long_flag("read")
                .about("Display specific entry")
                .arg(Arg::new("read")),
        )
        .subcommand(
            Command::new("delete")
                .short_flag('X')
                .long_flag("delete")
                .about("Delete specific entry")
                .arg(Arg::new("delete")),
        )
        .subcommand(
            Command::new("date search")
                .short_flag('d')
                .long_flag("date-search")
                .about("Search for entries around a date")
                .arg(Arg::new("datesearch")),
        )
        .subcommand(
            Command::new("search")
                .short_flag('s')
                .long_flag("search")
                .about("Query to search, enclosed in quotations")
                .arg(Arg::new("search"))
                .subcommand(
                    Command::new("date")
                        .short_flag('d')
                        .long_flag("date")
                        .about("Filter results by date range")
                        .arg(Arg::new("entry")),
                ),
        )
        .arg(
            Arg::new("config")
                .short('c')
                .long("config")
                .help("Path of config file to read"),
        )
        .get_matches()
}

pub fn parse_args(matches: ArgMatches, dt_format: &str) -> Args {
    let verbose = matches.get_count("verbose");

    match matches.subcommand() {
        Some(("new", input)) => {
            let text: String = match input.get_many::<String>("entry") {
                Some(t) => t.fold(String::new(), |mut a, b| {
                    a.reserve(b.len() + 1);
                    a.push_str(b);
                    a.push(' ');
                    a
                }),

                None => text_from_editor(None).unwrap(),
            };
            let e = Entry::new(text, dt_format);
            Args::New(e)
        }

        Some(("list", input)) => {
            let n: usize = input.get_one::<String>("list").unwrap().parse().unwrap();
            Args::List(n, verbose)
        }

        Some(("read", input)) => {
            let n: usize = input.get_one::<String>("read").unwrap().parse().unwrap();
            Args::Read(n)
        }

        Some(("edit", input)) => {
            let n: usize = input.get_one::<String>("edit").unwrap().parse().unwrap();
            Args::Edit(n)
        }

        Some(("delete", input)) => {
            let n: usize = input.get_one::<String>("delete").unwrap().parse().unwrap();
            Args::Delete(n, true)
        }

        Some(("search", input)) => {
            let mut q = String::new();
            let search_command = input.subcommand().unwrap_or(("search", input));
            match search_command {
                ("date", _sub_matches) => Args::DateFilter(q),
                ("search", _sub_matches) => {
                    q = input.get_one::<String>("search").unwrap().into();
                    Args::Search(q)
                }
                (name, _) => {
                    unreachable!("Unsupported subcommand `{name}`")
                }
            }
        }

        Some(("date search", input)) => {
            let date = input.get_one::<String>("search").unwrap().into();
            Args::DateSearch(date)
        }

        _ => unreachable!(),
    }
}