Smart-er argument checking (-Sa, -Ssr, etc.)

i18n
Michal 2 years ago committed by GitHub
parent d860f35594
commit e93bc7931c
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

@ -28,7 +28,7 @@ pub struct Args {
#[derive(Debug, Clone, Subcommand)]
pub enum Operation {
/// Installs a package from either the AUR or the Pacman-defined repositories
#[clap(bin_name = "ame", name = "install", visible_aliases = & ["-S"])]
#[clap(bin_name = "ame", name = "install", visible_aliases = & ["-S"], aliases = & ["-Sa", "-Sr"])]
Install(InstallArgs),
/// Removes a previously installed package
@ -36,11 +36,11 @@ pub enum Operation {
Remove(RemoveArgs),
/// Searches for packages matching a regex-supported pattern in the AUR and/or the repos
#[clap(bin_name = "ame", name = "search", visible_aliases = & ["-Ss"])]
#[clap(bin_name = "ame", name = "search", visible_aliases = & ["-Ss"], aliases = & ["-Ssa", "-Ssr"])]
Search(SearchArgs),
/// Queries installed packages
#[clap(bin_name = "ame", name = "query", visible_aliases = & ["-Q"])]
#[clap(bin_name = "ame", name = "query", visible_aliases = & ["-Q"], aliases = & ["-Qa", "-Qr", "-Qm", "-Qn"])]
Query(QueryArgs),
/// Gets info about a package
@ -76,11 +76,11 @@ pub struct InstallArgs {
#[clap(required = true)]
pub packages: Vec<String>,
/// Installs only from the AUR
/// Installs only from the AUR [-Sa]
#[clap(long, short)]
pub aur: bool,
/// Install the packages from the pacman-defined repositories
/// Install the packages only from the pacman-defined repositories [-Sr]
#[clap(long, short)]
pub repo: bool,
}
@ -94,11 +94,11 @@ pub struct RemoveArgs {
#[derive(Default, Debug, Clone, Parser)]
pub struct SearchArgs {
/// Searches for the relevant packages in both the AUR and repos
/// Searches for the relevant packages in both the AUR and repos [-Ssa]
#[clap(long, short)]
pub aur: bool,
/// Searches only local repos for the package
/// Searches only pacman repos for the package [-Ssr]
#[clap(long, short)]
pub repo: bool,

@ -7,6 +7,7 @@ use clap_complete::{Generator, Shell};
use internal::commands::ShellCommand;
use internal::error::SilentUnwrap;
use std::fs;
use std::env;
use std::path::Path;
use std::str::FromStr;
@ -99,6 +100,17 @@ fn main() {
fn cmd_install(args: InstallArgs, options: Options, cachedir: &str) {
// Initialise variables
let packages = args.packages;
if args.aur && args.repo {
crash!(
AppExitCode::Other,
"Cannot specify both --aur and --repo"
);
}
let aur = args.aur || env::args().collect::<Vec<String>>()[1] == "-Sa";
let repo = args.repo || env::args().collect::<Vec<String>>()[1] == "-Sr";
let sorted = sort(&packages, options);
let config = internal::config::read();
@ -113,11 +125,11 @@ fn cmd_install(args: InstallArgs, options: Options, cachedir: &str) {
);
}
if !sorted.repo.is_empty() {
if !repo && !aur && !sorted.repo.is_empty() || repo && !sorted.repo.is_empty() {
// If repo packages found, install them
operations::install(&sorted.repo, options);
}
if !sorted.aur.is_empty() {
if !repo && !aur && !sorted.aur.is_empty() || aur && !sorted.aur.is_empty() {
// If AUR packages found, install them
operations::aur_install(sorted.aur, options, cachedir);
}
@ -155,12 +167,12 @@ fn cmd_search(args: &SearchArgs, options: Options) {
let query_string = args.search.join(" ");
// Logic for searching
let both = !args.repo && !args.aur;
let repo = args.repo || both;
let aur = args.aur || both;
let repo = args.repo || env::args().collect::<Vec<String>>()[1] == "-Ssr";
let aur = args.aur || env::args().collect::<Vec<String>>()[1] == "-Ssa";
let both = !repo && !aur;
// Start repo spinner
let repo_results = if repo {
let repo_results = if repo || both {
let rsp = spinner!("Searching repos for {}", query_string);
// Search repos
@ -173,7 +185,7 @@ fn cmd_search(args: &SearchArgs, options: Options) {
};
// Start AUR spinner
let aur_results = if aur {
let aur_results = if aur || both {
// Strip query of any non-alphanumeric characters
let query_string = query_string.replace(|c: char| !c.is_alphanumeric() && c != '-', "");

@ -91,6 +91,12 @@ pub fn repo_search(query: &str, options: Options) -> String {
// Initialise results vector
let mut results_vec: Vec<SearchResult> = vec![];
let clone = lines.clone().collect::<Vec<&str>>();
if clone.len() == 1 && clone[0] == "" {
// If no results, return empty string
return "".to_string();
}
// Iterate over lines
for line in lines {
let parts: Vec<&str> = line.split('\\').collect();

Loading…
Cancel
Save