Skip to content

Commit

Permalink
feat: cli no-format (#3106)
Browse files Browse the repository at this point in the history
  • Loading branch information
aljazerzen authored Jul 26, 2023
1 parent ac39df8 commit 9743446
Show file tree
Hide file tree
Showing 7 changed files with 68 additions and 54 deletions.
13 changes: 8 additions & 5 deletions crates/prql-compiler/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -228,19 +228,22 @@ impl Default for Options {
}

impl Options {
pub fn no_format(mut self) -> Self {
self.format = false;
pub fn with_format(mut self, format: bool) -> Self {
self.format = format;
self
}

pub fn no_format(self) -> Self {
self.with_format(false)
}

pub fn with_signature_comment(mut self, signature_comment: bool) -> Self {
self.signature_comment = signature_comment;
self
}

pub fn no_signature(mut self) -> Self {
self.signature_comment = false;
self
pub fn no_signature(self) -> Self {
self.with_signature_comment(false)
}

pub fn with_target(mut self, target: Target) -> Self {
Expand Down
23 changes: 14 additions & 9 deletions crates/prqlc/src/cli.rs
Original file line number Diff line number Diff line change
Expand Up @@ -118,8 +118,12 @@ enum Command {
io_args: IoArgs,

/// Exclude the signature comment containing the PRQL version
#[arg(long, action = clap::ArgAction::SetFalse)]
hide_signature_comment: bool,
#[arg(long = "hide-signature-comment", action = clap::ArgAction::SetFalse)]
signature_comment: bool,

/// Emit unformatted, dense SQL
#[arg(long = "no-format", action = clap::ArgAction::SetFalse)]
format: bool,

/// Target to compile to
#[arg(short, long, default_value = "sql.any", env = "PRQLC_TARGET")]
Expand Down Expand Up @@ -339,15 +343,17 @@ impl Command {
}
}
Command::SQLCompile {
hide_signature_comment,
signature_comment,
format,
target,
..
} => {
semantic::load_std_lib(sources);

let opts = Options::default()
.with_target(Target::from_str(target).map_err(|e| downcast(e.into()))?)
.with_signature_comment(*hide_signature_comment);
.with_signature_comment(*signature_comment)
.with_format(*format);

prql_to_pl_tree(sources)
.and_then(|pl| pl_to_rq_tree(pl, &main_path))
Expand Down Expand Up @@ -569,7 +575,8 @@ group a_column (take 10 | sort b_column | derive {the_number = rank, last = lag
let result = Command::execute(
&Command::SQLCompile {
io_args: IoArgs::default(),
hide_signature_comment: true,
signature_comment: false,
format: true,
target: "sql.any".to_string(),
},
&mut "asdf".into(),
Expand All @@ -592,8 +599,8 @@ group a_column (take 10 | sort b_column | derive {the_number = rank, last = lag
let result = Command::execute(
&Command::SQLCompile {
io_args: IoArgs::default(),
// FIXME: this doesn't seem to be working?
hide_signature_comment: true,
signature_comment: false,
format: true,
target: "sql.any".to_string(),
},
&mut SourceTree::new([
Expand All @@ -618,8 +625,6 @@ group a_column (take 10 | sort b_column | derive {the_number = rank, last = lag
y
FROM
x
-- Generated by PRQL compiler version:0.9.2 (https://prql-lang.org)
"###);
}

Expand Down
5 changes: 3 additions & 2 deletions crates/prqlc/tests/snapshots/test__shell_completion-2.snap
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
---
source: prql-compiler/prqlc/tests/test.rs
source: crates/prqlc/tests/test.rs
info:
program: prqlc
args:
- "--color=never"
- shell-completion
- fish
env:
RUST_LOG: ""
CLICOLOR_FORCE: ""
RUST_LOG: ""
RUST_BACKTRACE: ""
---
success: true
Expand Down Expand Up @@ -64,6 +64,7 @@ complete -c prqlc -n "__fish_seen_subcommand_from sql:anchor" -s h -l help -d 'P
complete -c prqlc -n "__fish_seen_subcommand_from compile" -s t -l target -d 'Target to compile to' -r
complete -c prqlc -n "__fish_seen_subcommand_from compile" -l color -d 'Controls when to use color' -r -f -a "{auto ,always ,never }"
complete -c prqlc -n "__fish_seen_subcommand_from compile" -l hide-signature-comment -d 'Exclude the signature comment containing the PRQL version'
complete -c prqlc -n "__fish_seen_subcommand_from compile" -l no-format -d 'Emit unformatted, dense SQL'
complete -c prqlc -n "__fish_seen_subcommand_from compile" -s h -l help -d 'Print help (see more with \'--help\')'
complete -c prqlc -n "__fish_seen_subcommand_from watch" -l color -d 'Controls when to use color' -r -f -a "{auto ,always ,never }"
complete -c prqlc -n "__fish_seen_subcommand_from watch" -l no-format
Expand Down
7 changes: 4 additions & 3 deletions crates/prqlc/tests/snapshots/test__shell_completion-3.snap
Original file line number Diff line number Diff line change
@@ -1,15 +1,15 @@
---
source: prql-compiler/prqlc/tests/test.rs
source: crates/prqlc/tests/test.rs
info:
program: prqlc
args:
- "--color=never"
- shell-completion
- powershell
env:
RUST_BACKTRACE: ""
CLICOLOR_FORCE: ""
RUST_LOG: ""
CLICOLOR_FORCE: ""
RUST_BACKTRACE: ""
---
success: true
exit_code: 0
Expand Down Expand Up @@ -151,6 +151,7 @@ Register-ArgumentCompleter -Native -CommandName 'prqlc' -ScriptBlock {
[CompletionResult]::new('--target', 'target', [CompletionResultType]::ParameterName, 'Target to compile to')
[CompletionResult]::new('--color', 'color', [CompletionResultType]::ParameterName, 'Controls when to use color')
[CompletionResult]::new('--hide-signature-comment', 'hide-signature-comment', [CompletionResultType]::ParameterName, 'Exclude the signature comment containing the PRQL version')
[CompletionResult]::new('--no-format', 'no-format', [CompletionResultType]::ParameterName, 'Emit unformatted, dense SQL')
[CompletionResult]::new('-h', 'h', [CompletionResultType]::ParameterName, 'Print help (see more with ''--help'')')
[CompletionResult]::new('--help', 'help', [CompletionResultType]::ParameterName, 'Print help (see more with ''--help'')')
break
Expand Down
5 changes: 3 additions & 2 deletions crates/prqlc/tests/snapshots/test__shell_completion-4.snap
Original file line number Diff line number Diff line change
@@ -1,15 +1,15 @@
---
source: prql-compiler/prqlc/tests/test.rs
source: crates/prqlc/tests/test.rs
info:
program: prqlc
args:
- "--color=never"
- shell-completion
- zsh
env:
RUST_BACKTRACE: ""
RUST_LOG: ""
CLICOLOR_FORCE: ""
RUST_BACKTRACE: ""
---
success: true
exit_code: 0
Expand Down Expand Up @@ -194,6 +194,7 @@ _arguments "${_arguments_options[@]}" \
'--target=[Target to compile to]:TARGET: ' \
'--color=[Controls when to use color]:WHEN:(auto always never)' \
'--hide-signature-comment[Exclude the signature comment containing the PRQL version]' \
'--no-format[Emit unformatted, dense SQL]' \
'-h[Print help (see more with '\''--help'\'')]' \
'--help[Print help (see more with '\''--help'\'')]' \
'::input:_files' \
Expand Down
6 changes: 3 additions & 3 deletions crates/prqlc/tests/snapshots/test__shell_completion.snap
Original file line number Diff line number Diff line change
@@ -1,15 +1,15 @@
---
source: prql-compiler/prqlc/tests/test.rs
source: crates/prqlc/tests/test.rs
info:
program: prqlc
args:
- "--color=never"
- shell-completion
- bash
env:
RUST_BACKTRACE: ""
CLICOLOR_FORCE: ""
RUST_LOG: ""
RUST_BACKTRACE: ""
---
success: true
exit_code: 0
Expand Down Expand Up @@ -161,7 +161,7 @@ _prqlc() {
return 0
;;
prqlc__compile)
opts="-t -h --hide-signature-comment --target --color --help [INPUT] [OUTPUT] [MAIN_PATH]"
opts="-t -h --hide-signature-comment --no-format --target --color --help [INPUT] [OUTPUT] [MAIN_PATH]"
if [[ ${cur} == -* || ${COMP_CWORD} -eq 2 ]] ; then
COMPREPLY=( $(compgen -W "${opts}" -- "${cur}") )
return 0
Expand Down
63 changes: 33 additions & 30 deletions crates/prqlc/tests/test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -86,46 +86,49 @@ fn compile_help() {
cmd.args(["compile", "--help"]);

assert_cmd_snapshot!(cmd, @r###"
success: true
exit_code: 0
----- stdout -----
Parse, resolve, lower into RQ & compile to SQL
success: true
exit_code: 0
----- stdout -----
Parse, resolve, lower into RQ & compile to SQL
Only displays the main pipeline and does not handle loop.
Only displays the main pipeline and does not handle loop.
Usage: prqlc compile [OPTIONS] [INPUT] [OUTPUT] [MAIN_PATH]
Usage: prqlc compile [OPTIONS] [INPUT] [OUTPUT] [MAIN_PATH]
Arguments:
[INPUT]
[default: -]
Arguments:
[INPUT]
[default: -]
[OUTPUT]
[default: -]
[OUTPUT]
[default: -]
[MAIN_PATH]
Identifier of the main pipeline
[MAIN_PATH]
Identifier of the main pipeline
Options:
--hide-signature-comment
Exclude the signature comment containing the PRQL version
Options:
--hide-signature-comment
Exclude the signature comment containing the PRQL version
-t, --target <TARGET>
Target to compile to
[env: PRQLC_TARGET=]
[default: sql.any]
--no-format
Emit unformatted, dense SQL
--color <WHEN>
Controls when to use color
[default: auto]
[possible values: auto, always, never]
-t, --target <TARGET>
Target to compile to
[env: PRQLC_TARGET=]
[default: sql.any]
-h, --help
Print help (see a summary with '-h')
--color <WHEN>
Controls when to use color
[default: auto]
[possible values: auto, always, never]
----- stderr -----
"###);
-h, --help
Print help (see a summary with '-h')
----- stderr -----
"###);
}

#[test]
Expand Down

0 comments on commit 9743446

Please sign in to comment.