Skip to content

Commit

Permalink
change clap for structopt
Browse files Browse the repository at this point in the history
  • Loading branch information
ChrisDavison committed Mar 5, 2023
1 parent 21867cd commit 8796ffb
Show file tree
Hide file tree
Showing 3 changed files with 92 additions and 112 deletions.
134 changes: 58 additions & 76 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion Cargo.toml
Expand Up @@ -17,7 +17,7 @@ path = "bin/main.rs"
glob = "0.3.0"
lazy_static = "1.4.0"
rayon = "1.5.0"
clap = { version = "3.1.2", features = ["derive", "cargo"] }
structopt = "0.3.26"

[dev-dependencies]
criterion = "0.3"
Expand Down
68 changes: 33 additions & 35 deletions bin/main.rs
Expand Up @@ -2,85 +2,83 @@ use std::io::Write;

use tagsearch::{filter::Filter, utility::*, Tag};

use clap::{Parser, Subcommand};
use structopt::StructOpt;

#[derive(Parser)]
#[clap(author, version, about, long_about = None)]
#[clap(propagate_version = true)]
#[derive(StructOpt,Debug)]
struct Cli {
#[clap(subcommand)]
#[structopt(subcommand)]
command: Commands,
#[clap(long)]
#[structopt(long)]
root: Option<String>,
}

#[derive(Subcommand)]
#[derive(StructOpt,Debug)]
enum Commands {
/// Show files that have tags matching filter words
#[clap(aliases=&["f"])]
#[structopt(aliases=&["f"])]
Files {
/// Keywords to match
good: Vec<String>,
#[clap(long, require_value_delimiter(true))]
#[structopt(long, require_delimiter(true))]
/// Keywords to NOT match
not: Vec<String>,
/// Output in format suitable for vimgrep
#[clap(long)]
#[structopt(long)]
vim: bool,
/// Match ANY, not ALL, tags
#[clap(short, long)]
#[structopt(short, long)]
or: bool,
},
/// Show all tags from files with tags that match filter words
#[clap(aliases=&["t"])]
#[structopt(aliases=&["t"])]
Tags {
/// Keywords to match
good: Vec<String>,
#[clap(long, require_value_delimiter(true))]
#[structopt(long, require_delimiter(true))]
/// Keywords to NOT match
not: Vec<String>,
#[clap(short, long)]
#[structopt(short, long)]
/// Match ANY, not ALL, tags
or: bool,
/// Show how many times tag used
#[clap(short, long)]
#[structopt(short, long)]
count: bool,
/// Output in long format (tree-like)
#[clap(short, long)]
#[structopt(short, long)]
long: bool,
/// Stop 'tree' output in long list
#[clap(short, long)]
#[structopt(short, long)]
no_tree: bool,
},
/// Show tags from specific files
#[clap(aliases=&["ft"])]
#[structopt(aliases=&["ft"])]
FileTags {
/// Show how many times tag used
#[clap(short, long)]
#[structopt(short, long)]
count: bool,
/// Output in long format (tree-like)
#[clap(short, long)]
#[structopt(short, long)]
long: bool,
/// Stop 'tree' output in long list
#[clap(short, long)]
#[structopt(short, long)]
no_tree: bool,
/// Files to extract tags from
files: Vec<String>,
},
/// Show files without tags
#[clap(aliases=&["u"])]
#[structopt(aliases=&["u"])]
Untagged {
/// Output in format suitable for vimgrep
#[clap(long)]
#[structopt(long)]
vim: bool,
},
/// Show tags that may be typos/slight differences
#[clap(aliases=&["similar", "related", "s"])]
#[structopt(aliases=&["similar", "related", "s"])]
SimilarTags,
}

fn try_main() -> Result<(), std::io::Error> {
let cli = Cli::parse();
let cli = Cli::from_args();
let files = match get_files(cli.root) {
Ok(files) => files,
Err(e) => {
Expand All @@ -89,10 +87,10 @@ fn try_main() -> Result<(), std::io::Error> {
}
};

match &cli.command {
match cli.command {
Commands::Files { good, not, vim, or } => {
let f = Filter::new(good.as_slice(), not.as_slice(), *or);
display_files_matching_query(f, &files, *vim)
let f = Filter::new(good.as_slice(), not.as_slice(), or);
display_files_matching_query(f, &files, vim)
}
Commands::Tags {
good,
Expand All @@ -102,11 +100,11 @@ fn try_main() -> Result<(), std::io::Error> {
long,
no_tree,
} => {
let f = Filter::new(good.as_slice(), not.as_slice(), *or);
if *count {
let f = Filter::new(good.as_slice(), not.as_slice(), or);
if count {
display_tag_count(f, &files)
} else {
display_tags(f, &files, *long, *no_tree)
display_tags(f, &files, long, no_tree)
}
}
Commands::FileTags {
Expand All @@ -116,13 +114,13 @@ fn try_main() -> Result<(), std::io::Error> {
files,
} => {
let f: Filter = Default::default();
if *count {
display_tag_count(f, files)
if count {
display_tag_count(f, &files)
} else {
display_tags(f, files, *long, *no_tree)
display_tags(f, &files, long, no_tree)
}
}
Commands::Untagged { vim } => display_untagged(&files, *vim),
Commands::Untagged { vim } => display_untagged(&files, vim),
Commands::SimilarTags => display_similar_tags(&files),
}
}
Expand Down

0 comments on commit 8796ffb

Please sign in to comment.