Skip to content

Commit 6d896f2

Browse files
authored
fix: Simplify CLI debug command options and behavior (#68)
1 parent ac77397 commit 6d896f2

4 files changed

Lines changed: 22 additions & 40 deletions

File tree

AGENTS.md

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -66,14 +66,14 @@ Run: `cargo run -p plotnik-cli -- <command>`
6666

6767
Inputs: `-q/--query <Q>`, `--query-file <F>`, `--source <S>`, `-s/--source-file <F>`, `-l/--lang <L>`
6868

69-
Output: `--show-query`, `--show-source`, `--only-symbols`, `--cst`, `--raw`, `--spans`, `--cardinalities`
69+
Output (inferred from input): `--only-symbols`, `--cst`, `--raw`, `--spans`, `--cardinalities`
7070

7171
```sh
72-
cargo run -p plotnik-cli -- debug -q '(identifier) @id' --show-query
72+
cargo run -p plotnik-cli -- debug -q '(identifier) @id'
7373
cargo run -p plotnik-cli -- debug -q '(identifier) @id' --only-symbols
74-
cargo run -p plotnik-cli -- debug -s app.ts --show-source
75-
cargo run -p plotnik-cli -- debug -s app.ts --show-source --raw
76-
cargo run -p plotnik-cli -- debug -q '(function_declaration) @fn' -s app.ts -l typescript --show-query
74+
cargo run -p plotnik-cli -- debug -s app.ts
75+
cargo run -p plotnik-cli -- debug -s app.ts --raw
76+
cargo run -p plotnik-cli -- debug -q '(function_declaration) @fn' -s app.ts -l typescript
7777
```
7878

7979
## Syntax

crates/plotnik-cli/src/cli.rs

Lines changed: 5 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -32,11 +32,11 @@ pub struct Cli {
3232
pub enum Command {
3333
/// Debug and inspect queries and source files
3434
#[command(after_help = r#"EXAMPLES:
35-
plotnik debug -q '(identifier) @id' --show-query
35+
plotnik debug -q '(identifier) @id'
3636
plotnik debug -q '(identifier) @id' --only-symbols
37-
plotnik debug -s app.ts --show-source
38-
plotnik debug -s app.ts --show-source --raw
39-
plotnik debug -q '(function_declaration) @fn' -s app.ts -l typescript --show-query"#)]
37+
plotnik debug -s app.ts
38+
plotnik debug -s app.ts --raw
39+
plotnik debug -q '(function_declaration) @fn' -s app.ts -l typescript"#)]
4040
Debug {
4141
#[command(flatten)]
4242
query: QueryArgs,
@@ -88,19 +88,11 @@ pub struct SourceArgs {
8888

8989
#[derive(Args)]
9090
pub struct OutputArgs {
91-
/// Show query syntax tree
92-
#[arg(long = "show-query")]
93-
pub query: bool,
94-
9591
/// Colorize output (auto-detected by default)
9692
#[arg(long, default_value = "auto", value_name = "WHEN")]
9793
pub color: ColorChoice,
9894

99-
/// Show source syntax tree
100-
#[arg(long = "show-source")]
101-
pub source: bool,
102-
103-
/// Show only symbol table
95+
/// Show only symbol table (instead of query AST)
10496
#[arg(long = "only-symbols")]
10597
pub symbols: bool,
10698

crates/plotnik-cli/src/commands/debug/mod.rs

Lines changed: 12 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,6 @@ pub struct DebugArgs {
1313
pub source_text: Option<String>,
1414
pub source_file: Option<std::path::PathBuf>,
1515
pub lang: Option<String>,
16-
pub query: bool,
17-
pub source: bool,
1816
pub symbols: bool,
1917
pub raw: bool,
2018
pub cst: bool,
@@ -45,15 +43,11 @@ pub fn run(args: DebugArgs) {
4543
})
4644
});
4745

48-
let show_headers = [args.query, args.source, args.symbols]
49-
.iter()
50-
.filter(|&&x| x)
51-
.count()
52-
>= 2;
46+
let show_query = has_query_input && !args.symbols;
47+
let show_source = has_source_input;
48+
let show_headers = (show_query || args.symbols) && show_source;
5349

54-
if args.query
55-
&& let Some(ref q) = query
56-
{
50+
if show_query && let Some(ref q) = query {
5751
if show_headers {
5852
println!("=== QUERY ===");
5953
}
@@ -83,7 +77,7 @@ pub fn run(args: DebugArgs) {
8377
);
8478
}
8579

86-
if args.source {
80+
if show_source {
8781
let resolved_lang = resolve_lang(&args.lang, &args.source_text, &args.source_file);
8882
let source_code = load_source(&args.source_text, &args.source_file);
8983
let tree = parse_tree(&source_code, resolved_lang);
@@ -120,20 +114,18 @@ fn load_query(args: &DebugArgs) -> String {
120114
}
121115

122116
fn validate(args: &DebugArgs, has_query: bool, has_source: bool) -> Result<(), &'static str> {
123-
if (args.query || args.symbols) && !has_query {
124-
return Err("--query and --only-symbols require --query-text or --query-file");
117+
if !has_query && !has_source {
118+
return Err(
119+
"specify at least one input: -q/--query, --query-file, -s/--source-file, or --source",
120+
);
125121
}
126122

127-
if args.source && !has_source {
128-
return Err("--source requires --source-text or --source-file");
123+
if args.symbols && !has_query {
124+
return Err("--only-symbols requires -q/--query or --query-file");
129125
}
130126

131127
if args.source_text.is_some() && args.lang.is_none() {
132-
return Err("--lang is required when using --source-text");
133-
}
134-
135-
if !args.query && !args.source && !args.symbols {
136-
return Err("specify at least one output: --query, --source, or --only-symbols");
128+
return Err("--lang is required when using --source");
137129
}
138130

139131
Ok(())

crates/plotnik-cli/src/main.rs

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,8 +20,6 @@ fn main() {
2020
source_text: source.source_text,
2121
source_file: source.source_file,
2222
lang,
23-
query: output.query,
24-
source: output.source,
2523
symbols: output.symbols,
2624
raw: output.raw,
2725
cst: output.cst,

0 commit comments

Comments
 (0)