Skip to content

Commit 0222fba

Browse files
feat: adding verbose CLI option (#192)
1 parent 11f54f4 commit 0222fba

File tree

5 files changed

+150
-6
lines changed

5 files changed

+150
-6
lines changed

Cargo.lock

Lines changed: 120 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Cargo.toml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,9 @@ categories = ["command-line-utilities", "development-tools", "compilers", "text-
1616
log = "0.4.21"
1717
pretty_env_logger = "0.5.0"
1818

19+
# For CLI argument parsing.
20+
clap = { version = "4.5.0", features = ["derive"] }
21+
1922
# For custom errors.
2023
thiserror = "2.0.0"
2124

README.md

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -18,19 +18,18 @@ Monkey has a C-like syntax, supports **variable bindings**, **prefix** and **inf
1818

1919
## Content
2020
* [Usage](#usage)
21-
+ [Usage - Logging](#usage-logging)
2221
* [Compiling](#compiling)
2322
* [Unit Testing](#unit-testing)
2423
* [Issues/Feature Requests](#issuesfeature-requests)
2524

2625

2726
## Usage
27+
Run the interpreter via
2828

29+
```sh
30+
monkey_interpreter
31+
```
2932

30-
### Usage - Logging
31-
The crates `pretty_env_logger` and `log` are used to provide logging.
32-
The environment variable `RUST_LOG` can be used to set the logging level.
33-
See [https://crates.io/crates/pretty_env_logger](https://crates.io/crates/pretty_env_logger) for more detailed documentation.
3433

3534

3635
## Compiling

src/cli/mod.rs

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
use clap::Parser;
2+
3+
#[derive(Parser, Debug)]
4+
#[command(version, about, long_about = None)]
5+
pub(crate) struct Arguments {
6+
#[arg(
7+
long,
8+
help = "Enable verbose output, respects RUST_LOG environment variable if set."
9+
)]
10+
pub(crate) verbose: bool,
11+
}

src/main.rs

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,18 +5,29 @@ extern crate pretty_env_logger;
55
use std::io::{stdin, stdout, Write};
66

77
use anyhow::{Context, Result};
8+
use clap::Parser;
89

10+
use crate::cli::Arguments;
911
use crate::evaluator::Evaluator;
1012
use crate::lexical_analysis::LexicalAnalysis;
1113
use crate::syntax_analysis::SyntaxAnalysis;
1214

15+
mod cli;
1316
mod evaluator;
1417
mod lexical_analysis;
1518
mod syntax_analysis;
1619

1720
fn main() {
21+
info!("Version {}.", env!("CARGO_PKG_VERSION"));
22+
let arguments = Arguments::parse();
23+
debug!("The command line arguments provided are {arguments:?}.");
24+
25+
// Set up logging: if verbose is true and RUST_LOG is not set, default to info level
26+
if arguments.verbose && std::env::var("RUST_LOG").is_err() {
27+
std::env::set_var("RUST_LOG", "info");
28+
}
29+
1830
pretty_env_logger::init();
19-
trace!("Version {}.", env!("CARGO_PKG_VERSION"));
2031
let mut evaluator = crate::evaluator::Evaluator::new();
2132

2233
loop {

0 commit comments

Comments
 (0)