Skip to content

Commit

Permalink
feat(main): Add command for generating completion files
Browse files Browse the repository at this point in the history
  • Loading branch information
cauliyang committed Nov 7, 2023
1 parent 534d4cc commit 5ad06f6
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 12 deletions.
8 changes: 2 additions & 6 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -30,13 +30,9 @@ codegen-units = 1

[dependencies]
anyhow = { version = "1.0" }
clap = { version = "4.4.7", features = [
"wrap_help",
"derive",
"color",
"cargo",
] }
clap = { version = "4.4.7", features = ["wrap_help", "derive", "cargo"] }
clap-verbosity-flag = "2.1.0"
clap_complete = "4.4"
colored = "2"
env_logger = "0.10"
human-panic = "1.2.1"
Expand Down
37 changes: 31 additions & 6 deletions src/main.rs
Original file line number Diff line number Diff line change
@@ -1,34 +1,44 @@
use clap::{Parser, Subcommand};
use clap::{Command, CommandFactory, Parser, Subcommand, ValueHint};
use env_logger::Builder;
use human_panic::setup_panic;
use log::info;
use log::LevelFilter;
use std::path::PathBuf;

use clap_complete::{generate, Generator, Shell};
use std::io;

mod extract;
mod fa2fq;
mod fq2fa;
mod index;
mod rsoft;

#[derive(Parser)]
#[derive(Parser, Debug)]
#[command(author, version, about, long_about = None)]
#[command(propagate_version = true)]
struct Cli {
#[command(flatten)]
verbose: clap_verbosity_flag::Verbosity,
// If provided, outputs the completion file for given shell
#[arg(long = "generate", value_enum)]
generator: Option<Shell>,

#[command(subcommand)]
command: Option<Commands>,

#[command(flatten)]
verbose: clap_verbosity_flag::Verbosity,
}

#[derive(Subcommand)]
#[derive(Subcommand, Debug)]
enum Commands {
/// Extract reads from a BAM file
#[command(visible_alias = "e")]
Extract {
/// Read IDs
#[arg(value_hint = ValueHint::FilePath)]
readids: String,
/// Bam input file
#[arg(value_hint = ValueHint::FilePath)]
input: PathBuf,

/// Is the output file a BAM file
Expand All @@ -39,28 +49,32 @@ enum Commands {
/// Index a BAM file
Index {
/// Bam input file
#[arg(value_hint = ValueHint::FilePath)]
input: PathBuf,
},

/// Convert a FASTA file to FASTQ
Fa2fq {
/// fasta input file
#[arg(value_hint = ValueHint::FilePath)]
input: PathBuf,
},

/// Convert a FASTQ file to FASTA
Fq2fa {
/// fastq input file
#[arg(value_hint = ValueHint::FilePath)]
input: PathBuf,
},

/// Create softlinks to files with same suffix in one directory recursively
Rsoft {
/// The directory to search
#[arg(value_hint = ValueHint::FilePath)]
source: PathBuf,

/// The directory to create the softlinks. default is current directory
#[arg(short = 't')]
#[arg(short = 't', value_hint = ValueHint::FilePath)]
target: Option<PathBuf>,

/// The suffix of the files to link. default is all files
Expand All @@ -69,6 +83,10 @@ enum Commands {
},
}

fn print_completions<G: Generator>(gen: G, cmd: &mut Command) {
generate(gen, cmd, cmd.get_name().to_string(), &mut io::stdout());
}

fn main() {
setup_panic!();

Expand All @@ -88,6 +106,13 @@ fn main() {
}
log_builder.init();

if let Some(generator) = cli.generator {
let mut cmd = Cli::command();
info!("Generating completion file for {generator:?}...");
print_completions(generator, &mut cmd);
return;
}

// You can check for the existence of subcommands, and if found use their
// matches just as you would the top level cmd
match &cli.command {
Expand Down

0 comments on commit 5ad06f6

Please sign in to comment.