|
1 |
| -// This file is part of the uutils sed package. |
| 1 | +// Program entry point and CLI processing |
| 2 | +// |
| 3 | +// SPDX-License-Identifier: MIT |
| 4 | +// Copyright (c) 2025 Diomidis Spinellis |
2 | 5 | //
|
| 6 | +// This file is part of the uutils sed package. |
| 7 | +// It is licensed under the MIT License. |
3 | 8 | // For the full copyright and license information, please view the LICENSE
|
4 | 9 | // file that was distributed with this source code.
|
5 | 10 |
|
6 | 11 | pub mod command;
|
7 | 12 | pub mod compiler;
|
8 | 13 | pub mod processor;
|
9 | 14 |
|
10 |
| -use crate::command::ScriptValue; |
| 15 | +use crate::command::{Context, ScriptValue}; |
11 | 16 | use crate::compiler::compile;
|
12 | 17 | use crate::processor::process;
|
13 | 18 | use clap::{arg, Arg, ArgMatches, Command};
|
@@ -96,6 +101,8 @@ fn get_scripts_files(matches: &ArgMatches) -> UResult<(Vec<ScriptValue>, Vec<Pat
|
96 | 101 | pub fn uumain(args: impl uucore::Args) -> UResult<()> {
|
97 | 102 | let matches = uu_app().try_get_matches_from(args)?;
|
98 | 103 | let (scripts, files) = get_scripts_files(&matches)?;
|
| 104 | + let _context = build_context(&matches); |
| 105 | + |
99 | 106 | let executable = compile(scripts)?;
|
100 | 107 | process(executable, files)?;
|
101 | 108 | Ok(())
|
@@ -123,7 +130,8 @@ pub fn uu_app() -> Command {
|
123 | 130 | .short('E')
|
124 | 131 | .long("regexp-extended")
|
125 | 132 | .short_alias('r')
|
126 |
| - .help("Use extended regular expressions."), |
| 133 | + .help("Use extended regular expressions.") |
| 134 | + .action(clap::ArgAction::SetTrue), |
127 | 135 | arg!(-e --expression <SCRIPT> "Add script to executed commands.")
|
128 | 136 | .action(clap::ArgAction::Append),
|
129 | 137 | // Access with .get_many::<PathBuf>("file")
|
@@ -160,6 +168,38 @@ pub fn uu_app() -> Command {
|
160 | 168 | ])
|
161 | 169 | }
|
162 | 170 |
|
| 171 | +// Parse CLI flag arguments and return a Context struct based on them |
| 172 | +fn build_context(matches: &ArgMatches) -> Context { |
| 173 | + Context { |
| 174 | + input: String::new(), |
| 175 | + line: 1, |
| 176 | + |
| 177 | + // Flags |
| 178 | + all_output_files: matches.get_flag("all-output-files"), |
| 179 | + debug: matches.get_flag("debug"), |
| 180 | + regexp_extended: matches.get_flag("regexp-extended"), |
| 181 | + follow_symlinks: matches.get_flag("follow-symlinks"), |
| 182 | + in_place: matches.contains_id("in-place"), |
| 183 | + in_place_suffix: matches.get_one::<String>("in-place").and_then(|s| { |
| 184 | + if s.is_empty() { |
| 185 | + None |
| 186 | + } else { |
| 187 | + Some(s.clone()) |
| 188 | + } |
| 189 | + }), |
| 190 | + length: matches |
| 191 | + .get_one::<u32>("length") |
| 192 | + .map(|v| *v as usize) |
| 193 | + .unwrap_or(70), |
| 194 | + quiet: matches.get_flag("quiet"), |
| 195 | + posix: matches.get_flag("posix"), |
| 196 | + separate: matches.get_flag("separate"), |
| 197 | + sandbox: matches.get_flag("sandbox"), |
| 198 | + unbuffered: matches.get_flag("unbuffered"), |
| 199 | + null_data: matches.get_flag("null-data"), |
| 200 | + } |
| 201 | +} |
| 202 | + |
163 | 203 | #[cfg(test)]
|
164 | 204 | mod tests {
|
165 | 205 | use super::*; // Allows access to private functions/items in this module
|
@@ -257,4 +297,87 @@ mod tests {
|
257 | 297 | );
|
258 | 298 | assert_eq!(files, vec![PathBuf::from("-")]); // Stdin should be used
|
259 | 299 | }
|
| 300 | + |
| 301 | + fn test_matches(args: &[&str]) -> ArgMatches { |
| 302 | + uu_app().get_matches_from(["sed"].into_iter().chain(args.iter().copied())) |
| 303 | + } |
| 304 | + |
| 305 | + #[test] |
| 306 | + fn test_defaults() { |
| 307 | + let matches = test_matches(&[]); |
| 308 | + let ctx = build_context(&matches); |
| 309 | + |
| 310 | + assert_eq!(ctx.input.len(), 0); |
| 311 | + assert_eq!(ctx.line, 1); |
| 312 | + assert!(!ctx.all_output_files); |
| 313 | + assert!(!ctx.debug); |
| 314 | + assert!(!ctx.regexp_extended); |
| 315 | + assert!(!ctx.follow_symlinks); |
| 316 | + assert!(!ctx.in_place); |
| 317 | + assert_eq!(ctx.in_place_suffix, None); |
| 318 | + assert_eq!(ctx.length, 70); |
| 319 | + assert!(!ctx.quiet); |
| 320 | + assert!(!ctx.posix); |
| 321 | + assert!(!ctx.separate); |
| 322 | + assert!(!ctx.sandbox); |
| 323 | + assert!(!ctx.unbuffered); |
| 324 | + assert!(!ctx.null_data); |
| 325 | + } |
| 326 | + |
| 327 | + #[test] |
| 328 | + fn test_all_flags() { |
| 329 | + let matches = test_matches(&[ |
| 330 | + "--all-output-files", |
| 331 | + "--debug", |
| 332 | + "-E", |
| 333 | + "--follow-symlinks", |
| 334 | + "-i", |
| 335 | + "-l", |
| 336 | + "80", |
| 337 | + "-n", |
| 338 | + "--posix", |
| 339 | + "-s", |
| 340 | + "--sandbox", |
| 341 | + "-u", |
| 342 | + "-z", |
| 343 | + ]); |
| 344 | + |
| 345 | + let ctx = build_context(&matches); |
| 346 | + |
| 347 | + assert_eq!(ctx.input.len(), 0); |
| 348 | + assert!(ctx.all_output_files); |
| 349 | + assert!(ctx.debug); |
| 350 | + assert!(ctx.regexp_extended); |
| 351 | + assert!(ctx.follow_symlinks); |
| 352 | + assert!(ctx.in_place); |
| 353 | + assert!(ctx.in_place_suffix.is_none()); |
| 354 | + assert_eq!(ctx.length, 80); |
| 355 | + assert!(ctx.quiet); |
| 356 | + assert!(ctx.posix); |
| 357 | + assert!(ctx.separate); |
| 358 | + assert!(ctx.sandbox); |
| 359 | + assert!(ctx.unbuffered); |
| 360 | + assert!(ctx.null_data); |
| 361 | + } |
| 362 | + |
| 363 | + #[test] |
| 364 | + fn test_in_place_with_suffix() { |
| 365 | + let matches = test_matches(&["-i", ".bak"]); |
| 366 | + let ctx = build_context(&matches); |
| 367 | + |
| 368 | + assert!(ctx.in_place); |
| 369 | + assert_eq!(ctx.in_place_suffix, Some(".bak".to_string())); |
| 370 | + } |
| 371 | + |
| 372 | + #[test] |
| 373 | + fn test_length_default_and_custom() { |
| 374 | + let matches_default = test_matches(&[]); |
| 375 | + let matches_custom = test_matches(&["-l", "120"]); |
| 376 | + |
| 377 | + let ctx_default = build_context(&matches_default); |
| 378 | + let ctx_custom = build_context(&matches_custom); |
| 379 | + |
| 380 | + assert_eq!(ctx_default.length, 70); |
| 381 | + assert_eq!(ctx_custom.length, 120); |
| 382 | + } |
260 | 383 | }
|
0 commit comments