Skip to main content

asa/
args.rs

1//! Parses command line arguments
2use core::fmt;
3
4use clap::Parser;
5use once_cell::sync::OnceCell;
6
7static ARGS: OnceCell<Args> = OnceCell::new();
8
9/// Wrapper type for log::LevelFilter
10#[derive(clap::ValueEnum, Clone, Debug, Default)]
11pub enum FeedbackLevel {
12    Debug,
13    #[default]
14    Note,
15    Warn,
16    Error,
17}
18
19impl FeedbackLevel {
20    pub fn to_log_level(&self) -> log::LevelFilter {
21        match self {
22            FeedbackLevel::Debug => log::LevelFilter::Debug,
23            FeedbackLevel::Note => log::LevelFilter::Info,
24            FeedbackLevel::Warn => log::LevelFilter::Warn,
25            FeedbackLevel::Error => log::LevelFilter::Error,
26        }
27    }
28}
29
30impl fmt::Display for FeedbackLevel {
31    fn fmt(&self, fmt: &mut fmt::Formatter<'_>) -> fmt::Result {
32        let s = match self {
33            FeedbackLevel::Debug => "debug".to_owned(),
34            FeedbackLevel::Note => "note".to_owned(),
35            FeedbackLevel::Warn => "warn".to_owned(),
36            FeedbackLevel::Error => "error".to_owned(),
37        };
38        write!(fmt, "{s}")
39    }
40}
41
42#[derive(Parser, Debug)]
43#[command(version, about, long_about = None)]
44pub struct Args {
45    /// File to assemble or execute.
46    ///
47    /// If not given it will search for ./Main.sbl in the
48    /// current working directory.
49    ///
50    ///  If it's a folder it will look for ./folder/Main.sbl
51    ///
52    /// If a file with a .SBL extension is given it will be assembled.
53    ///
54    /// .BIN or .SBLX files will be executed
55    pub target: Option<String>,
56
57    /// Level of assembler and runtime feedback. DEBUG is used for assembler debugging!
58    ///
59    /// When at DEBUG level a dump.sbl file is generated during the assembly process
60    #[arg(short,long, default_value_t = FeedbackLevel::Note)]
61    pub feedback_level: FeedbackLevel,
62
63    /// Disable program execution, files will still be assembled and possibly written to disk
64    #[arg(short = 'e', long, default_value_t = false)]
65    pub disable_execution: bool,
66
67    /// Run program with the debugger
68    #[arg(short, long, default_value_t = false)]
69    pub debugger: bool,
70
71    /// Folder that stores libraries
72    #[arg(short = 'l', long, default_value = "./subleq/libs")]
73    pub libs_path: String,
74
75    /// Disables type checking for macro arguments. Not recommended
76    #[arg(short = 't', long, default_value_t = false)]
77    pub disable_type_checking: bool,
78
79    /// Output file for assembled subleq, if not given no output be will generated.
80    ///
81    /// If given the file type can be:
82    ///
83    /// * Nothing in which case a .bin file with the module name will be generated
84    ///
85    /// * BIN or SBLX which will output with that file extension and the module name
86    ///
87    /// * A file name with extension
88    #[arg(short, long, num_args = 0..=1)]
89    pub output: Option<Option<String>>,
90
91    /// Suppresses all assembler output except for errors, overrides --feedback-level. Program output will still be shown.
92    #[arg(short, long, default_value_t = false)]
93    pub silent: bool,
94
95    /// Shows more notes and warning. Recommended for release builds
96    #[arg(short, long, default_value_t = false)]
97    pub pedantic: bool,
98
99    /// Treat warnings as errors
100    #[arg(short = 'w', long, default_value_t = false)]
101    pub warnings_are_errors: bool,
102}
103
104pub fn get() -> &'static Args {
105    ARGS.get().unwrap_or_else(|| {
106        crate::error!("No arguments have been parsed");
107    })
108}
109
110pub fn exist() -> bool {
111    ARGS.get().is_some()
112}
113
114pub fn parse() {
115    ARGS.set(Args::parse()).expect("Could not read args");
116}