cl_repl/
args.rs

1//! Handles argument parsing (currently using the [argwerk] crate)
2
3use std::{io::IsTerminal, path::PathBuf, str::FromStr};
4
5argwerk::define! {
6    ///
7    ///The Conlang prototype debug interface
8    #[usage = "conlang [<file>] [-I <include...>] [-m <mode>] [-r <repl>]"]
9    #[derive(Clone, PartialEq, Eq, PartialOrd, Ord)]
10    pub struct Args {
11        pub file: Option<PathBuf>,
12        pub include: Vec<PathBuf>,
13        pub mode: Mode,
14        pub repl: bool = is_terminal(),
15    }
16
17    ///files to include
18    ["-I" | "--include", path] => {
19        include.push(path.into());
20    }
21    ///the CLI operating mode (`f`mt | `l`ex | `r`un)
22    ["-m" | "--mode", flr] => {
23        mode = flr.parse()?;
24    }
25    ///whether to start the repl (`true` or `false`)
26    ["-r" | "--repl", bool] => {
27        repl = bool.parse()?;
28    }
29    ///display usage information
30    ["-h" | "--help"] => {
31        println!("{}", Args::help());
32        if true { std::process::exit(0); }
33    }
34    ///the main source file
35    [#[option] path] if file.is_none() => {
36        file = path.map(Into::into);
37    }
38
39    [path] if file.is_some() => {
40        include.push(path.into());
41    }
42}
43
44/// gets whether stdin AND stdout are a terminal, for pipelining
45pub fn is_terminal() -> bool {
46    std::io::stdin().is_terminal() && std::io::stdout().is_terminal()
47}
48
49/// The CLI's operating mode
50#[derive(Clone, Copy, Debug, Default, PartialEq, Eq, PartialOrd, Ord)]
51pub enum Mode {
52    #[default]
53    Menu,
54    Lex,
55    Fmt,
56    Run,
57}
58
59impl FromStr for Mode {
60    type Err = &'static str;
61    fn from_str(s: &str) -> Result<Self, &'static str> {
62        Ok(match s {
63            "f" | "fmt" | "p" | "pretty" => Mode::Fmt,
64            "l" | "lex" | "tokenize" | "token" => Mode::Lex,
65            "r" | "run" => Mode::Run,
66            _ => Err("Recognized modes are: 'r' \"run\", 'f' \"fmt\", 'l' \"lex\"")?,
67        })
68    }
69}