Skip to content

Commit ed7a3be

Browse files
m-spanmax-sixty
andauthored
feat: add logging verbosity flags to cli (#4471)
Co-authored-by: Maximilian Roos <[email protected]>
1 parent eabd17f commit ed7a3be

File tree

8 files changed

+742
-444
lines changed

8 files changed

+742
-444
lines changed

Cargo.lock

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

prqlc/prqlc/Cargo.toml

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ cli = [
1616
"anyhow",
1717
"clap_complete_command",
1818
"clap_complete",
19+
"clap-verbosity-flag",
1920
"clap",
2021
"clio",
2122
"color-eyre",
@@ -69,6 +70,8 @@ strum_macros = "0.26.2"
6970
anyhow = {version = "1.0.86", features = ["backtrace"], optional = true}
7071
clap = {version = "4.4.18", features = ["derive", "env", "wrap_help"], optional = true}
7172
clap_complete_command = {version = "0.5.1", optional = true}
73+
# Pinned for MSRV
74+
clap-verbosity-flag = {version = "=2.1.2", optional = true}
7275
clio = {version = "0.3.3", features = ['clap-parse'], optional = true}
7376
color-eyre = {version = "0.6.3", optional = true}
7477
colorchoice-clap = {version = "1.0.0", optional = true}
@@ -79,7 +82,9 @@ walkdir = {version = "2.5.0", optional = true}
7982

8083
# Not direct dependencies, but pinning because of bugs in previous versions. Can
8184
# remove when dependencies no longer use it. (If CI passes, it can be removed.)
82-
clap_complete = {version = "4.4.9", optional = true}
85+
clap_builder = {version = "=4.4.18", optional = true}
86+
clap_complete = {version = "=4.4.10", optional = true}
87+
clap_complete_fig = {version = "=4.4.2", optional = true}
8388

8489
# We use minijinja just for the Jinja lexer, which is not part of the
8590
# public interface which is covered by semver guarantees.

prqlc/prqlc/src/cli/mod.rs

Lines changed: 51 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,26 +1,24 @@
11
#![cfg(not(target_family = "wasm"))]
22

3-
mod docs_generator;
4-
mod jinja;
5-
mod watch;
3+
use std::collections::HashMap;
4+
use std::env;
5+
use std::io::{Read, Write};
6+
use std::ops::Range;
7+
use std::path::Path;
8+
use std::process::exit;
9+
use std::str::FromStr;
610

711
use anstream::{eprintln, println};
812
use anyhow::anyhow;
913
use anyhow::bail;
1014
use anyhow::Result;
1115
use ariadne::Source;
1216
use clap::{CommandFactory, Parser, Subcommand, ValueHint};
17+
use clap_verbosity_flag::LogLevel;
1318
use clio::has_extension;
1419
use clio::Output;
1520
use is_terminal::IsTerminal;
1621
use itertools::Itertools;
17-
use std::collections::HashMap;
18-
use std::env;
19-
use std::io::{Read, Write};
20-
use std::ops::Range;
21-
use std::path::Path;
22-
use std::process::exit;
23-
use std::str::FromStr;
2422

2523
use prqlc::semantic;
2624
use prqlc::semantic::reporting::{collect_frames, label_references};
@@ -30,11 +28,18 @@ use prqlc::{ir::pl::Lineage, ir::Span};
3028
use prqlc::{pl_to_prql, pl_to_rq_tree, prql_to_pl, prql_to_pl_tree, rq_to_sql, SourceTree};
3129
use prqlc::{Options, Target};
3230

31+
mod docs_generator;
32+
mod jinja;
33+
mod watch;
34+
3335
/// Entrypoint called by [`crate::main`]
3436
pub fn main() -> color_eyre::eyre::Result<()> {
35-
env_logger::builder().format_timestamp(None).init();
36-
color_eyre::install()?;
3737
let mut cli = Cli::parse();
38+
env_logger::builder()
39+
.filter_level(cli.verbose.log_level_filter())
40+
.format_timestamp(None)
41+
.init();
42+
color_eyre::install()?;
3843
cli.color.write_global();
3944

4045
if let Err(error) = cli.command.run() {
@@ -66,6 +71,9 @@ struct Cli {
6671
command: Command,
6772
#[command(flatten)]
6873
color: colorchoice_clap::Color,
74+
75+
#[command(flatten)]
76+
verbose: clap_verbosity_flag::Verbosity<LoggingHelp>,
6977
}
7078

7179
#[derive(Subcommand, Debug, Clone)]
@@ -207,6 +215,37 @@ pub struct IoArgs {
207215
main_path: Option<String>,
208216
}
209217

218+
#[derive(Copy, Clone, Debug, Default)]
219+
struct LoggingHelp;
220+
221+
impl LogLevel for LoggingHelp {
222+
/// By default, this will only report errors.
223+
fn default() -> Option<log::Level> {
224+
Some(log::Level::Error)
225+
}
226+
fn verbose_help() -> Option<&'static str> {
227+
Some("Increase logging verbosity")
228+
}
229+
230+
fn verbose_long_help() -> Option<&'static str> {
231+
Some(
232+
r#"More v's, More vebose logging:
233+
-v shows warnings
234+
-vv shows info
235+
-vvv shows debug
236+
-vvvv shows trace"#,
237+
)
238+
}
239+
240+
fn quiet_help() -> Option<&'static str> {
241+
Some("Silences logging output")
242+
}
243+
244+
fn quiet_long_help() -> Option<&'static str> {
245+
Some("Silences logging output")
246+
}
247+
}
248+
210249
#[derive(clap::ValueEnum, Clone, Debug)]
211250
enum Format {
212251
Json,

prqlc/prqlc/tests/integration/cli.rs

Lines changed: 31 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -32,10 +32,27 @@ fn help() {
3232
help Print this message or the help of the given subcommand(s)
3333
3434
Options:
35-
--color <WHEN> Controls when to use color [default: auto] [possible values: auto, always,
36-
never]
37-
-h, --help Print help
38-
-V, --version Print version
35+
--color <WHEN>
36+
Controls when to use color
37+
38+
[default: auto]
39+
[possible values: auto, always, never]
40+
41+
-v, --verbose...
42+
More v's, More vebose logging:
43+
-v shows warnings
44+
-vv shows info
45+
-vvv shows debug
46+
-vvvv shows trace
47+
48+
-q, --quiet...
49+
Silences logging output
50+
51+
-h, --help
52+
Print help (see a summary with '-h')
53+
54+
-V, --version
55+
Print version
3956
4057
----- stderr -----
4158
"###);
@@ -123,6 +140,16 @@ fn compile_help() {
123140
[default: auto]
124141
[possible values: auto, always, never]
125142
143+
-v, --verbose...
144+
More v's, More vebose logging:
145+
-v shows warnings
146+
-vv shows info
147+
-vvv shows debug
148+
-vvvv shows trace
149+
150+
-q, --quiet...
151+
Silences logging output
152+
126153
-h, --help
127154
Print help (see a summary with '-h')
128155

0 commit comments

Comments
 (0)