mirror of
https://github.com/pcvolkmer/fastq-tools.git
synced 2025-09-13 13:12:52 +00:00
feat: add optional input file argument
This commit is contained in:
14
README.md
14
README.md
@@ -15,7 +15,8 @@ Commands:
|
|||||||
help Print this message or the help of the given subcommand(s)
|
help Print this message or the help of the given subcommand(s)
|
||||||
|
|
||||||
Options:
|
Options:
|
||||||
-d, --decompress decompress input as gzip compressed data
|
-i, --input <INPUT_FILE> Input file (optional)
|
||||||
|
-d, --decompress Decompress input as gzip compressed data
|
||||||
-h, --help Print help
|
-h, --help Print help
|
||||||
-V, --version Print version
|
-V, --version Print version
|
||||||
```
|
```
|
||||||
@@ -33,6 +34,11 @@ To use build-in decompression of input data, use the `--decompress`/`-d` option:
|
|||||||
```shell
|
```shell
|
||||||
cat file_fastq.gz | fastq-tools --decompress info
|
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
|
This will result in output like
|
||||||
|
|
||||||
@@ -53,3 +59,9 @@ To use build-in decompression of input data, use the `--decompress`/`-d` option:
|
|||||||
```shell
|
```shell
|
||||||
cat file_fastq.gz | fastq-tools -d scramble | gzip > scrambled_fastq.gz
|
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
|
||||||
|
```
|
@@ -1,4 +1,5 @@
|
|||||||
use clap::{Parser, Subcommand};
|
use clap::{Parser, Subcommand};
|
||||||
|
use std::path::PathBuf;
|
||||||
|
|
||||||
#[derive(Parser)]
|
#[derive(Parser)]
|
||||||
#[command(author, version, about, long_about = None)]
|
#[command(author, version, about, long_about = None)]
|
||||||
@@ -6,10 +7,12 @@ use clap::{Parser, Subcommand};
|
|||||||
pub struct Args {
|
pub struct Args {
|
||||||
#[command(subcommand)]
|
#[command(subcommand)]
|
||||||
pub(crate) command: Command,
|
pub(crate) command: Command,
|
||||||
|
#[arg(short = 'i', long = "input", help = "Input file (optional)")]
|
||||||
|
pub(crate) input_file: Option<PathBuf>,
|
||||||
#[arg(
|
#[arg(
|
||||||
short = 'd',
|
short = 'd',
|
||||||
long = "decompress",
|
long = "decompress",
|
||||||
help = "decompress input as gzip compressed data"
|
help = "Decompress input as gzip compressed data"
|
||||||
)]
|
)]
|
||||||
pub(crate) decompress: bool,
|
pub(crate) decompress: bool,
|
||||||
}
|
}
|
||||||
|
29
src/main.rs
29
src/main.rs
@@ -8,6 +8,7 @@ use console::Style;
|
|||||||
use flate2::read::GzDecoder;
|
use flate2::read::GzDecoder;
|
||||||
use itertools::Itertools;
|
use itertools::Itertools;
|
||||||
use regex::Regex;
|
use regex::Regex;
|
||||||
|
use std::fs::File;
|
||||||
use std::io::{BufRead, BufReader};
|
use std::io::{BufRead, BufReader};
|
||||||
|
|
||||||
fn scramble_sequence(value: &str, seed: u32) -> String {
|
fn scramble_sequence(value: &str, seed: u32) -> String {
|
||||||
@@ -55,25 +56,43 @@ fn scramble_sequence(value: &str, seed: u32) -> String {
|
|||||||
|
|
||||||
fn main() {
|
fn main() {
|
||||||
let args = Args::parse();
|
let args = Args::parse();
|
||||||
let stdin = std::io::stdin();
|
let input: Box<dyn BufRead> = 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 {
|
match &args.command {
|
||||||
Command::Info => {
|
Command::Info => {
|
||||||
if args.decompress {
|
if args.decompress {
|
||||||
let gz_decoder = GzDecoder::new(stdin);
|
let gz_decoder = GzDecoder::new(input);
|
||||||
let buf_reader = BufReader::new(gz_decoder);
|
let buf_reader = BufReader::new(gz_decoder);
|
||||||
info(buf_reader)
|
info(buf_reader)
|
||||||
} else {
|
} else {
|
||||||
info(BufReader::new(stdin))
|
info(BufReader::new(input))
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
Command::Scramble => {
|
Command::Scramble => {
|
||||||
if args.decompress {
|
if args.decompress {
|
||||||
let gz_decoder = GzDecoder::new(stdin);
|
let gz_decoder = GzDecoder::new(input);
|
||||||
let buf_reader = BufReader::new(gz_decoder);
|
let buf_reader = BufReader::new(gz_decoder);
|
||||||
scramble(buf_reader)
|
scramble(buf_reader)
|
||||||
} else {
|
} else {
|
||||||
scramble(BufReader::new(stdin))
|
scramble(BufReader::new(input))
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
Reference in New Issue
Block a user