Skip to content

Commit 6b50276

Browse files
refactor: move context related options to single args
1 parent 6b133d2 commit 6b50276

File tree

5 files changed

+37
-18
lines changed

5 files changed

+37
-18
lines changed

crates/cli/src/lib.rs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -218,6 +218,9 @@ mod test_cli {
218218
ok("scan --globs '*.js' --globs '*.ts'");
219219
ok("scan -j 12");
220220
ok("scan --threads 12");
221+
ok("scan -A 12");
222+
ok("scan --after 12");
223+
ok("scan --context 1");
221224
error("scan -i --json dir"); // conflict
222225
error("scan --report-style rich --json dir"); // conflict
223226
error("scan -r test.yml --inline-rules '{}'"); // conflict

crates/cli/src/run.rs

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ use crate::config::register_custom_language;
1111
use crate::lang::SgLang;
1212
use crate::print::{ColoredPrinter, Diff, Heading, InteractivePrinter, JSONPrinter, Printer};
1313
use crate::utils::ErrorContext as EC;
14-
use crate::utils::{filter_file_pattern, InputArgs, MatchUnit, OutputArgs};
14+
use crate::utils::{filter_file_pattern, ContextArgs, InputArgs, MatchUnit, OutputArgs};
1515
use crate::utils::{DebugFormat, FileTrace, RunTrace};
1616
use crate::utils::{Items, PathWorker, StdInWorker, Worker};
1717

@@ -108,6 +108,10 @@ pub struct RunArg {
108108
#[clap(flatten)]
109109
output: OutputArgs,
110110

111+
/// context related options
112+
#[clap(flatten)]
113+
context: ContextArgs,
114+
111115
/// Controls whether to print the file name as heading.
112116
///
113117
/// If heading is used, the file name will be printed as heading before all matches of that file.
@@ -137,11 +141,7 @@ impl RunArg {
137141
// Every run will include Search or Replace
138142
// Search or Replace by arguments `pattern` and `rewrite` passed from CLI
139143
pub fn run_with_pattern(arg: RunArg) -> Result<()> {
140-
let context = if arg.output.context != 0 {
141-
(arg.output.context, arg.output.context)
142-
} else {
143-
(arg.output.before, arg.output.after)
144-
};
144+
let context = arg.context.get();
145145
if let Some(json) = arg.output.json {
146146
let printer = JSONPrinter::stdout(json).context(context);
147147
return run_pattern_with_printer(arg, printer);
@@ -372,6 +372,8 @@ mod test {
372372
json: None,
373373
update_all: false,
374374
tracing: Default::default(),
375+
},
376+
context: ContextArgs {
375377
before: 0,
376378
after: 0,
377379
context: 0,

crates/cli/src/scan.rs

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ use crate::print::{
1818
};
1919
use crate::utils::ErrorContext as EC;
2020
use crate::utils::RuleOverwrite;
21-
use crate::utils::{filter_file_interactive, InputArgs, OutputArgs, SeverityArg};
21+
use crate::utils::{filter_file_interactive, ContextArgs, InputArgs, OutputArgs, SeverityArg};
2222
use crate::utils::{FileTrace, RuleTrace, ScanTrace};
2323
use crate::utils::{Items, PathWorker, StdInWorker, Worker};
2424

@@ -70,15 +70,14 @@ pub struct ScanArg {
7070
/// output related options
7171
#[clap(flatten)]
7272
output: OutputArgs,
73+
/// context related options
74+
#[clap(flatten)]
75+
context: ContextArgs,
7376
}
7477

7578
pub fn run_with_config(arg: ScanArg) -> Result<()> {
7679
register_custom_language(arg.config.clone())?;
77-
let context = if arg.output.context != 0 {
78-
(arg.output.context, arg.output.context)
79-
} else {
80-
(arg.output.before, arg.output.after)
81-
};
80+
let context = arg.context.get();
8281
if let Some(_format) = &arg.format {
8382
let printer = CloudPrinter::stdout();
8483
return run_scan(arg, printer);
@@ -390,6 +389,8 @@ rule:
390389
update_all: false,
391390
color: ColorArg::Never,
392391
tracing: Default::default(),
392+
},
393+
context: ContextArgs {
393394
before: 0,
394395
after: 0,
395396
context: 0,

crates/cli/src/utils/args.rs

Lines changed: 18 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -153,8 +153,18 @@ pub struct OutputArgs {
153153
/// tracing information outputs to stderr and does not affect the result of the search.
154154
#[clap(long, default_value = "nothing", value_name = "LEVEL")]
155155
pub tracing: Tracing,
156+
}
157+
158+
impl OutputArgs {
159+
// either explicit interactive or implicit update_all
160+
pub fn needs_interactive(&self) -> bool {
161+
self.interactive || self.update_all
162+
}
163+
}
156164

157-
// context related options
165+
/// context related options
166+
#[derive(Args)]
167+
pub struct ContextArgs {
158168
/// Show NUM lines after each match.
159169
///
160170
/// It conflicts with both the -C/--context flag.
@@ -188,10 +198,13 @@ pub struct OutputArgs {
188198
pub context: u16,
189199
}
190200

191-
impl OutputArgs {
192-
// either explicit interactive or implicit update_all
193-
pub fn needs_interactive(&self) -> bool {
194-
self.interactive || self.update_all
201+
impl ContextArgs {
202+
pub fn get(&self) -> (u16, u16) {
203+
if self.context > 0 {
204+
(self.context, self.context)
205+
} else {
206+
(self.before, self.after)
207+
}
195208
}
196209
}
197210

crates/cli/src/utils/mod.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ mod rule_overwrite;
55
mod tracing;
66
mod worker;
77

8-
pub use args::{InputArgs, OutputArgs, SeverityArg};
8+
pub use args::{ContextArgs, InputArgs, OutputArgs, SeverityArg};
99
pub use debug_query::DebugFormat;
1010
pub use error_context::{exit_with_error, ErrorContext};
1111
pub use rule_overwrite::RuleOverwrite;

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy