refact: overhaul arg parsing

This commit is contained in:
steven-omaha
2023-05-23 17:28:51 +02:00
parent 79c9e51e8c
commit a4639a1c3a
13 changed files with 222 additions and 258 deletions
+3 -19
View File
@@ -2,8 +2,7 @@ use std::collections::HashSet;
use std::iter::Peekable;
use std::vec::IntoIter;
use anyhow::{bail, Context, Result};
use clap::ArgMatches;
use anyhow::{bail, Result};
use regex::Regex;
use crate::grouping::{Group, Package, Section};
@@ -17,8 +16,8 @@ use crate::grouping::{Group, Package, Section};
/// This function will return an error if
/// - an invalid regex was provided, or
/// - no matching packages could be found.
pub fn search_packages(args: &ArgMatches, groups: &HashSet<Group>) -> Result<()> {
let re = get_regex_from_args(args)?;
pub fn search_packages(regex_str: &str, groups: &HashSet<Group>) -> Result<()> {
let re = Regex::new(regex_str)?;
let mut vec = vec![];
@@ -41,21 +40,6 @@ pub fn search_packages(args: &ArgMatches, groups: &HashSet<Group>) -> Result<()>
Ok(())
}
/// Extract the user-provided regex from the command-line args.
///
/// # Errors
///
/// This function will return an error if an invalid regex was provided.
fn get_regex_from_args(args: &ArgMatches) -> Result<Regex, anyhow::Error> {
let search_string = args
.get_one::<String>("regex")
.context("getting search string from arg")?;
let re = Regex::new(search_string)?;
Ok(re)
}
fn print_triples(mut vec: Vec<(&Group, &Section, &Package)>) {
vec.sort_unstable();