pub struct InputPathHandler {
pub paths: Vec<PathBuf>,
pub recursive: bool,
pub file_extensions: Vec<String>,
}
Expand description
Universal input path processing structure for CLI commands.
InputPathHandler
provides a unified interface for processing file and directory
inputs across different SubX CLI commands. It supports multiple input sources,
recursive directory scanning, and file extension filtering.
This handler is used by commands like match
, convert
, sync
, and detect-encoding
to provide consistent -i
parameter functionality and directory processing behavior.
§Features
- Multiple Input Sources: Supports multiple files and directories via
-i
parameter - Recursive Processing: Optional recursive directory scanning with
--recursive
flag - File Filtering: Filter files by extension for command-specific processing
- Path Validation: Validates all input paths exist before processing
- Cross-Platform: Handles both absolute and relative paths correctly
§Examples
§Basic Usage
use subx_cli::cli::InputPathHandler;
use std::path::PathBuf;
// Create handler from multiple paths
let paths = vec![file1, file2];
let handler = InputPathHandler::from_args(&paths, false)?
.with_extensions(&["srt", "ass"]);
// Collect all matching files
let files = handler.collect_files()?;
assert_eq!(files.len(), 2);
§Directory Processing
use subx_cli::cli::InputPathHandler;
use std::path::PathBuf;
// Flat directory scanning (non-recursive)
let handler_flat = InputPathHandler::from_args(&[test_dir.to_path_buf()], false)?
.with_extensions(&["srt"]);
let files_flat = handler_flat.collect_files()?;
assert_eq!(files_flat.len(), 1); // Only finds file1
// Recursive directory scanning
let handler_recursive = InputPathHandler::from_args(&[test_dir.to_path_buf()], true)?
.with_extensions(&["srt"]);
let files_recursive = handler_recursive.collect_files()?;
assert_eq!(files_recursive.len(), 2); // Finds both file1 and file2
§Command Integration
use subx_cli::cli::{InputPathHandler, MatchArgs};
// Example of how commands use InputPathHandler
let handler = args.get_input_handler()?;
let files = handler.collect_files()?;
// Process files...
Fields§
§paths: Vec<PathBuf>
List of input paths (files and directories) to process
recursive: bool
Whether to recursively scan subdirectories
file_extensions: Vec<String>
File extension filters (lowercase, without dot)
Implementations§
Source§impl InputPathHandler
impl InputPathHandler
Sourcepub fn merge_paths_from_multiple_sources(
optional_paths: &[Option<PathBuf>],
multiple_paths: &[PathBuf],
string_paths: &[String],
) -> Result<Vec<PathBuf>, SubXError>
pub fn merge_paths_from_multiple_sources( optional_paths: &[Option<PathBuf>], multiple_paths: &[PathBuf], string_paths: &[String], ) -> Result<Vec<PathBuf>, SubXError>
Merge paths from multiple sources to create a unified path list
This method provides a unified interface for CLI commands to merge different types of path parameters into a single PathBuf vector.
§Arguments
optional_paths
- Optional path list (e.g.,path
,input
,video
,subtitle
, etc.)multiple_paths
- Multiple path list (e.g.,input_paths
)string_paths
- String format path list (e.g.,file_paths
)
§Returns
Returns the merged PathBuf vector, or an error if all inputs are empty
§Examples
use subx_cli::cli::InputPathHandler;
use std::path::PathBuf;
// Merge paths from different sources
let optional = vec![Some(PathBuf::from("single.srt"))];
let multiple = vec![PathBuf::from("dir1"), PathBuf::from("dir2")];
let strings = vec!["file1.srt".to_string(), "file2.ass".to_string()];
let merged = InputPathHandler::merge_paths_from_multiple_sources(
&optional,
&multiple,
&strings
)?;
// merged now contains all paths
assert_eq!(merged.len(), 5);
Sourcepub fn from_args(
input_args: &[PathBuf],
recursive: bool,
) -> Result<Self, SubXError>
pub fn from_args( input_args: &[PathBuf], recursive: bool, ) -> Result<Self, SubXError>
Create InputPathHandler from command line arguments
Sourcepub fn with_extensions(self, extensions: &[&str]) -> Self
pub fn with_extensions(self, extensions: &[&str]) -> Self
Set supported file extensions (without dot)
Sourcepub fn get_directories(&self) -> Vec<PathBuf>
pub fn get_directories(&self) -> Vec<PathBuf>
Get all specified directory paths
This method returns all specified directory paths for commands that need to process directories one by one. If the specified path contains files, it will return the directory containing that file.
§Returns
Deduplicated list of directory paths
§Examples
use subx_cli::cli::InputPathHandler;
use std::path::PathBuf;
let paths = vec![file1.clone(), test_dir.to_path_buf()];
let handler = InputPathHandler::from_args(&paths, false)?;
let directories = handler.get_directories();
// Should contain test_dir (after deduplication)
assert_eq!(directories.len(), 1);
assert_eq!(directories[0], test_dir);
Trait Implementations§
Source§impl Clone for InputPathHandler
impl Clone for InputPathHandler
Source§fn clone(&self) -> InputPathHandler
fn clone(&self) -> InputPathHandler
1.0.0 · Source§fn clone_from(&mut self, source: &Self)
fn clone_from(&mut self, source: &Self)
source
. Read more