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
//! Handles argument parsing (currently using the [argwerk] crate)

use std::{io::IsTerminal, path::PathBuf, str::FromStr};

argwerk::define! {
    ///
    ///The Conlang prototype debug interface
    #[usage = "conlang [<file>] [-I <include...>] [-m <mode>] [-r <repl>]"]
    #[derive(Clone, PartialEq, Eq, PartialOrd, Ord)]
    pub struct Args {
        pub file: Option<PathBuf>,
        pub include: Vec<PathBuf>,
        pub mode: Mode,
        pub repl: bool = is_terminal(),
    }

    ///files to include
    ["-I" | "--include", path] => {
        include.push(path.into());
    }
    ///the CLI operating mode (`f`mt | `l`ex | `r`un)
    ["-m" | "--mode", flr] => {
        mode = flr.parse()?;
    }
    ///whether to start the repl (`true` or `false`)
    ["-r" | "--repl", bool] => {
        repl = bool.parse()?;
    }
    ///display usage information
    ["-h" | "--help"] => {
        println!("{}", Args::help());
        if true { std::process::exit(0); }
    }
    ///the main source file
    [#[option] path] if file.is_none() => {
        file = path.map(Into::into);
    }
}

/// gets whether stdin AND stdout are a terminal, for pipelining
pub fn is_terminal() -> bool {
    std::io::stdin().is_terminal() && std::io::stdout().is_terminal()
}

/// The CLI's operating mode
#[derive(Clone, Copy, Debug, Default, PartialEq, Eq, PartialOrd, Ord)]
pub enum Mode {
    #[default]
    Menu,
    Lex,
    Fmt,
    Run,
}

impl FromStr for Mode {
    type Err = &'static str;
    fn from_str(s: &str) -> Result<Self, &'static str> {
        Ok(match s {
            "f" | "fmt" | "p" | "pretty" => Mode::Fmt,
            "l" | "lex" | "tokenize" | "token" => Mode::Lex,
            "r" | "run" => Mode::Run,
            _ => Err("Recognized modes are: 'r' \"run\", 'f' \"fmt\", 'l' \"lex\"")?,
        })
    }
}