Skip to content

Commit

Permalink
feat(prqlc cli)!: prqlc with no argument will display help without …
Browse files Browse the repository at this point in the history
…error (#5133)
  • Loading branch information
eitsupi authored Jan 30, 2025
1 parent 566f438 commit 30f80f1
Show file tree
Hide file tree
Showing 2 changed files with 55 additions and 22 deletions.
42 changes: 23 additions & 19 deletions prqlc/prqlc/src/cli/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ mod watch;

/// Entrypoint called by [`crate::main`]
pub fn main() -> color_eyre::eyre::Result<()> {
let mut cli = Cli::parse();
let cli = Cli::parse();

// redirect all log messages into the [debug::DebugLog]
if has_debug_log(&cli) {
Expand All @@ -54,24 +54,28 @@ pub fn main() -> color_eyre::eyre::Result<()> {
color_eyre::install()?;
cli.color.write_global();

if let Err(error) = cli.command.run() {
eprintln!("{error}");
// Copied from
// https://doc.rust-lang.org/src/std/backtrace.rs.html#1-504, since it's private
fn backtrace_enabled() -> bool {
match env::var("RUST_LIB_BACKTRACE") {
Ok(s) => s != "0",
Err(_) => match env::var("RUST_BACKTRACE") {
if let Some(mut subcommand) = cli.command {
if let Err(error) = subcommand.run() {
eprintln!("{error}");
// Copied from
// https://doc.rust-lang.org/src/std/backtrace.rs.html#1-504, since it's private
fn backtrace_enabled() -> bool {
match env::var("RUST_LIB_BACKTRACE") {
Ok(s) => s != "0",
Err(_) => false,
},
Err(_) => match env::var("RUST_BACKTRACE") {
Ok(s) => s != "0",
Err(_) => false,
},
}
}
if backtrace_enabled() {
eprintln!("{:#}", error.backtrace());
}
}
if backtrace_enabled() {
eprintln!("{:#}", error.backtrace());
}

exit(1)
exit(1)
}
} else {
Cli::command().print_help()?;
}

Ok(())
Expand All @@ -80,7 +84,7 @@ pub fn main() -> color_eyre::eyre::Result<()> {
#[derive(Parser, Debug, Clone)]
struct Cli {
#[command(subcommand)]
command: Command,
command: Option<Command>,
#[command(flatten)]
color: colorchoice_clap::Color,
}
Expand Down Expand Up @@ -533,10 +537,10 @@ impl Command {
fn has_debug_log(cli: &Cli) -> bool {
matches!(
cli.command,
Command::Compile {
Some(Command::Compile {
debug_log: Some(_),
..
}
})
)
}

Expand Down
35 changes: 32 additions & 3 deletions prqlc/prqlc/src/cli/test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,11 +13,11 @@ use walkdir::WalkDir;
#[cfg(not(windows))] // Windows has slightly different output (e.g. `prqlc.exe`), so we exclude.
#[test]
fn help() {
assert_cmd_snapshot!(prqlc_command().arg("--help"), @r#"
assert_cmd_snapshot!(prqlc_command().arg("--help"), @r"
success: true
exit_code: 0
----- stdout -----
Usage: prqlc [OPTIONS] <COMMAND>
Usage: prqlc [OPTIONS] [COMMAND]
Commands:
parse Parse into PL AST
Expand All @@ -39,7 +39,36 @@ fn help() {
-V, --version Print version
----- stderr -----
"#);
");

// without arguments
assert_cmd_snapshot!(prqlc_command(), @r"
success: true
exit_code: 0
----- stdout -----
Usage: prqlc [OPTIONS] [COMMAND]
Commands:
parse Parse into PL AST
lex Lex into Lexer Representation
fmt Parse & generate PRQL code back
collect Parse the whole project and collect it into a single PRQL source file
debug Commands for meant for debugging, prone to change
experimental Experimental commands are prone to change
compile Parse, resolve, lower into RQ & compile to SQL
watch Watch a directory and compile .prql files to .sql files
list-targets Show available compile target names
shell-completion Print a shell completion for supported shells
help Print this message or the help of the given subcommand(s)
Options:
--color <WHEN> Controls when to use color [default: auto] [possible values: auto, always,
never]
-h, --help Print help
-V, --version Print version
----- stderr -----
");
}

#[test]
Expand Down

0 comments on commit 30f80f1

Please sign in to comment.