use clap::Parser;
use std::fs::File;
use std::io::Write;
use std::path::PathBuf;
use crate::output::{DefaultFormatter, MarkdownFormatter, XmlFormatter};
use crate::utils::read_paths_from_stdin;
use crate::{FileProcessor, Result};
#[derive(Parser)]
#[command(name = "fuse")]
#[command(about = "Turn many files -> single file, useful for LLM prompting.")]
#[command(
long_about = r#"Turn many files -> single file, useful for LLM prompting.
Usage:
fuse [path/to/file_or_directory] [options]
Here's a few samples to get started:
fuse src/ # All files in src/
fuse src/ test/ -e ts # All ts files in src/ and test
fuse hello.text test/ -e rs -e toml # hello file and .rs and .toml files in test
fuse . --ignore "*.log" --ignore "test_*" # Skip logs and files that start with "test_"
fuse . -o output.txt # Save to file instead of printing or use >
For a full list of options, run `fuse help`."#
)]
#[command(version)]
#[command(disable_help_flag = true)]
#[command(disable_version_flag = true)]
pub struct Cli {
#[arg(value_name = "PATHS")]
pub paths: Vec<PathBuf>,
#[arg(long = "help", short = 'h')]
pub help: bool,
#[arg(short = 'e', long = "extension", action = clap::ArgAction::Append, value_name = "EXT", help_heading = "Input Control")]
pub extensions: Vec<String>,
#[arg(long = "include-hidden", help_heading = "Input Control")]
pub include_hidden: bool,
#[arg(long = "ignore-files-only", help_heading = "Input Control")]
pub ignore_files_only: bool,
#[arg(long = "ignore-gitignore", help_heading = "Input Control")]
pub ignore_gitignore: bool,
#[arg(long = "ignore", action = clap::ArgAction::Append, value_name = "PATTERN", help_heading = "Input Control")]
pub ignore_patterns: Vec<String>,
#[arg(short = 'c', long = "cxml", help_heading = "Output Format")]
pub claude_xml: bool,
#[arg(short = 'm', long = "markdown", help_heading = "Output Format")]
pub markdown: bool,
#[arg(short = 'n', long = "line-numbers", help_heading = "Output Format")]
pub line_numbers: bool,
#[arg(
short = 'o',
long = "output",
value_name = "FILE",
help_heading = "Output Format"
)]
pub output_file: Option<PathBuf>,
#[arg(long = "toc", help_heading = "Output Format")]
pub table_of_contents: bool,
#[arg(long = "toc-dirs-only", help_heading = "Output Format")]
pub toc_dirs_only: bool,
#[arg(long = "toc-files", help_heading = "Output Format")]
pub toc_files: bool,
#[arg(short = '0', long = "null", help_heading = "Other")]
pub null_separator: bool,
#[arg(short = 'V', long = "version", action = clap::ArgAction::Version, help_heading = "Other")]
pub version: Option<bool>,
}
const SHORT_HELP_TEXT: &str = r#"Turn many files -> single file, useful for LLM prompting.
Usage:
fuse [path/to/file_or_directory] [options]
Here's a few samples to get started:
fuse src/ # All files in src/
fuse src/ test/ -e ts # All ts files in src/ and test
fuse hello.text test/ -e rs -e toml # hello file and .rs and .toml files in test
fuse . --ignore "*.log" --ignore "test_*" # Skip logs and files that start with "test_"
fuse . -o output.txt # Save to file instead of printing or use >
For a full list of options, run `fuse --help`."#;
const FULL_HELP_TEXT: &str = r#"Turn many files -> single file, useful for LLM prompting.
Usage:
fuse [path/to/file_or_directory] [options]
Here's a few samples to get started:
fuse src/ # All files in src/
fuse src/ test/ -e ts # All ts files in src/ and test
fuse hello.text test/ -e rs -e toml # hello file and .rs and .toml files in test
fuse . --ignore "*.log" --ignore "test_*" # Skip logs and files that start with "test_"
fuse . -o output.txt # Save to file instead of printing or use >
For a full list of options, run `fuse --help`.
OPTIONS
Input Control:
-e, --extension <EXT> Only include these extensions (e.g. -e py -e js)
--include-hidden Include hidden files (starting with .)
--ignore-files-only Make --ignore patterns skip files only, not directories
--ignore-gitignore Don't use .gitignore rules
--ignore <PATTERN> Skip files matching pattern (*.log, test_*, *foo*, __pycache__)
Output Format:
-c, --cxml Output in Claude XML format
-m, --markdown Output as Markdown code blocks
-n, --line-numbers Add line numbers
-o, --output <FILE> Save to file instead of printing
--toc Include table of contents tree (auto: files+dirs if <100 lines, dirs only if ≥100)
--toc-dirs-only Table of contents shows directories only
--toc-files Table of contents shows files and directories
Other:
-0, --null Read null-separated paths from stdin
-h, --help Print help
-V, --version Print version
Pattern Usage:
--ignore "test_*" → Matches: test_utils.py, test_data.json
--ignore "*.log" → Matches: debug.log, error.log
--ignore "*foo*" → Matches: foo.txt, config_foo_bar.xml
--ignore "__init__.py" → Matches: any file/folder named exactly "__init__.py""#;
fn print_short_help() {
println!("{}", SHORT_HELP_TEXT);
}
fn print_full_help() {
println!("{}", FULL_HELP_TEXT);
}
pub fn run() -> Result<()> {
let raw_args: Vec<String> = std::env::args().collect();
if raw_args.len() == 1 {
print_short_help();
return Ok(());
}
if raw_args
.iter()
.any(|arg| arg == "help" || arg == "--help" || arg == "-h")
{
print_full_help();
return Ok(());
}
if raw_args.iter().any(|arg| arg == "--version" || arg == "-V") {
println!("{}", env!("CARGO_PKG_VERSION"));
return Ok(());
}
let args = Cli::parse();
let mut all_paths = args.paths.clone();
if all_paths.is_empty() {
let stdin_paths = read_paths_from_stdin(args.null_separator)?;
for path in stdin_paths {
all_paths.push(PathBuf::from(path));
}
}
if all_paths.is_empty() {
print_short_help();
std::process::exit(1);
}
for path in &all_paths {
if !path.exists() {
eprintln!("Path does not exist: {}", path.display());
std::process::exit(1);
}
}
if args.toc_dirs_only && args.toc_files {
eprintln!("Error: Cannot specify both --toc-dirs-only and --toc-files");
std::process::exit(1);
}
let toc_mode = if args.table_of_contents || args.toc_dirs_only || args.toc_files {
if args.toc_files {
Some(crate::TocMode::FilesAndDirs)
} else if args.toc_dirs_only {
Some(crate::TocMode::DirsOnly)
} else {
Some(crate::TocMode::Auto)
}
} else {
None
};
let processor = FileProcessor::new(
args.extensions,
args.include_hidden,
args.ignore_files_only,
args.ignore_gitignore,
args.ignore_patterns,
args.line_numbers,
toc_mode,
)?;
let output = if args.claude_xml {
let mut formatter = XmlFormatter::new();
processor.process_paths(&all_paths, &mut formatter)?
} else if args.markdown {
let mut formatter = MarkdownFormatter::new();
processor.process_paths(&all_paths, &mut formatter)?
} else {
let mut formatter = DefaultFormatter::new();
processor.process_paths(&all_paths, &mut formatter)?
};
if let Some(output_path) = args.output_file {
let mut file = File::create(output_path)?;
file.write_all(output.as_bytes())?;
} else {
print!("{}", output);
}
Ok(())
}