From d8f3d70193cc8fad2b75adc6a30542f19d2b61eb Mon Sep 17 00:00:00 2001 From: Paul-Christian Volkmer Date: Mon, 11 Aug 2025 21:00:05 +0200 Subject: [PATCH] feat: add optional input file argument --- README.md | 18 +++++++++++++++--- src/cli.rs | 5 ++++- src/main.rs | 29 ++++++++++++++++++++++++----- 3 files changed, 43 insertions(+), 9 deletions(-) diff --git a/README.md b/README.md index 4dcd62e..952cc81 100644 --- a/README.md +++ b/README.md @@ -15,9 +15,10 @@ Commands: help Print this message or the help of the given subcommand(s) Options: - -d, --decompress decompress input as gzip compressed data - -h, --help Print help - -V, --version Print version + -i, --input Input file (optional) + -d, --decompress Decompress input as gzip compressed data + -h, --help Print help + -V, --version Print version ``` ### Info @@ -33,6 +34,11 @@ To use build-in decompression of input data, use the `--decompress`/`-d` option: ```shell cat file_fastq.gz | fastq-tools --decompress info ``` +Using optional input file argument: + +```shell +fastq-tools --decompress --input file_fastq.gz info +``` This will result in output like @@ -52,4 +58,10 @@ To use build-in decompression of input data, use the `--decompress`/`-d` option: ```shell cat file_fastq.gz | fastq-tools -d scramble | gzip > scrambled_fastq.gz +``` + +Using optional input file argument: + +```shell +fastq-tools -d -i file_fastq.gz scramble | gzip > scrambled_fastq.gz ``` \ No newline at end of file diff --git a/src/cli.rs b/src/cli.rs index cf554da..780eb9b 100644 --- a/src/cli.rs +++ b/src/cli.rs @@ -1,4 +1,5 @@ use clap::{Parser, Subcommand}; +use std::path::PathBuf; #[derive(Parser)] #[command(author, version, about, long_about = None)] @@ -6,10 +7,12 @@ use clap::{Parser, Subcommand}; pub struct Args { #[command(subcommand)] pub(crate) command: Command, + #[arg(short = 'i', long = "input", help = "Input file (optional)")] + pub(crate) input_file: Option, #[arg( short = 'd', long = "decompress", - help = "decompress input as gzip compressed data" + help = "Decompress input as gzip compressed data" )] pub(crate) decompress: bool, } diff --git a/src/main.rs b/src/main.rs index 35326fa..48a7cb7 100644 --- a/src/main.rs +++ b/src/main.rs @@ -8,6 +8,7 @@ use console::Style; use flate2::read::GzDecoder; use itertools::Itertools; use regex::Regex; +use std::fs::File; use std::io::{BufRead, BufReader}; fn scramble_sequence(value: &str, seed: u32) -> String { @@ -55,25 +56,43 @@ fn scramble_sequence(value: &str, seed: u32) -> String { fn main() { let args = Args::parse(); - let stdin = std::io::stdin(); + let input: Box = match args.input_file { + Some(input_file) => { + let file = match File::open(input_file) { + Ok(file) => file, + _ => { + println!( + "{}\n", + Style::new() + .bold() + .red() + .apply_to("🔥 Cannot open input file") + ); + return; + } + }; + Box::new(BufReader::new(file)) + } + _ => Box::new(BufReader::new(std::io::stdin())), + }; match &args.command { Command::Info => { if args.decompress { - let gz_decoder = GzDecoder::new(stdin); + let gz_decoder = GzDecoder::new(input); let buf_reader = BufReader::new(gz_decoder); info(buf_reader) } else { - info(BufReader::new(stdin)) + info(BufReader::new(input)) } } Command::Scramble => { if args.decompress { - let gz_decoder = GzDecoder::new(stdin); + let gz_decoder = GzDecoder::new(input); let buf_reader = BufReader::new(gz_decoder); scramble(buf_reader) } else { - scramble(BufReader::new(stdin)) + scramble(BufReader::new(input)) } } }