feat: arguments with clip

This commit is contained in:
Didier Slof 2023-07-17 11:50:19 +02:00
parent 1c758fa0dd
commit 867d91ffab
Signed by: didier
GPG key ID: 01E71F18AA4398E5
6 changed files with 85 additions and 46 deletions

View file

@ -7,7 +7,7 @@ edition = "2021"
[dependencies]
fern = "0.6.2"
getopts = "0.2.21"
log = "0.4.19"
regex = "1.9.1"
manifest = { path = "../manifest" }
clap = { version = "4.3.12", features = ["derive"] }

View file

@ -1,9 +1,63 @@
use crate::package::identifier::PackageIdentifier;
use crate::prelude::Arguments;
use clap::{Parser, Subcommand};
use log::error;
#[derive(Parser, Debug)]
#[clap(name = "pkgr", version = "0.1.0", author = "")]
pub struct Cli {
#[command(subcommand)]
pub command: Command,
}
#[derive(Subcommand, Debug)]
pub enum Command {
Install(Arguments, PackageIdentifier), // install a package from a remote source
Remove(PackageIdentifier), // remove a package from the local source
List, // list all packages in the local source
/// Install a package
Install {
#[arg(short, long, default_value_t = false)]
build: bool,
package_identifier: PackageIdentifier,
},
/// Remove a package from the system
Remove {
package_identifier: PackageIdentifier,
},
/// List packages
List {
/// List installed packages
#[arg(short, long, default_value_t = true)]
installed: bool,
},
/// Update packages on the system
Update,
}
#[command(hide = true)]
None,
}
impl Command {
pub fn execute(&self) {
match self {
Command::Install {
build,
package_identifier,
} => {
if *build {
error!("Build is not yet implemented.");
} else {
error!("Install is not yet implemented.");
}
}
Command::Remove { package_identifier } => {
error!("Remove is not yet implemented.");
}
Command::List { installed } => {
error!("List is not yet implemented.");
}
Command::Update => {
error!("Update is not yet implemented.");
}
Command::None => {
error!("No command was specified.");
}
}
}
}

View file

@ -1,45 +1,19 @@
use clap::Parser;
use std::env;
use std::process::exit;
use std::str::FromStr;
use getopts::Options;
use crate::commands::{Cli, Command};
use log::{info, SetLoggerError};
mod prelude;
mod commands;
mod package;
fn main() {
setup_logger()
.expect("Unable to setup logger.");
setup_logger().expect("Unable to setup logger.");
let args: Vec<String> = env::args().collect();
let program = args[0].clone();
let mut opts = Options::new();
opts.optflag("h", "help", "Show help message.");
let matches = opts.parse(&args[1..]).unwrap_or_else(|e| panic!("{}", e.to_string()));
if matches.opt_present("h") {
print_usage(&program, opts);
exit(1);
}
let command = if !matches.free.is_empty() {
matches.free[0].clone()
} else {
print_usage(&program, opts);
exit(1);
};
info!("Identifier: {}", package::identifier::PackageIdentifier::from_str(&command).unwrap());
}
fn print_usage(program: &str, opts: Options) {
let mut brief = format!("Usage: {} <COMMAND> [options]\n\n", program);
brief += &*format!("Commands: grab \n");
brief += &*format!(" remove\n");
print!("{}", opts.usage(&brief.trim_end()));
let c = Cli::parse();
info!("Command: {:?}", c);
}
fn setup_logger() -> Result<(), SetLoggerError> {
@ -57,4 +31,4 @@ fn setup_logger() -> Result<(), SetLoggerError> {
.level(log::LevelFilter::Info)
.chain(std::io::stdout())
.apply()
}
}

View file

@ -1,8 +1,9 @@
use std::error::Error;
use std::str::FromStr;
use regex::Regex;
#[derive(Debug)]
#[derive(Debug, Clone)]
pub enum PackageIdentifierError {
InvalidPackageLocator(String),
InvalidURI(String),
@ -11,13 +12,17 @@ pub enum PackageIdentifierError {
impl std::fmt::Display for PackageIdentifierError {
fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
match self {
PackageIdentifierError::InvalidPackageLocator(s) => write!(f, "Invalid package locator: {}", s),
PackageIdentifierError::InvalidPackageLocator(s) => {
write!(f, "Invalid package locator: {}", s)
}
PackageIdentifierError::InvalidURI(s) => write!(f, "Invalid URI: {}", s),
}
}
}
#[derive(Debug)]
impl Error for PackageIdentifierError {}
#[derive(Debug, Clone)]
pub enum PackageIdentifier {
PackageLocator(PackageLocator),
URI(String),
@ -32,7 +37,7 @@ impl std::fmt::Display for PackageIdentifier {
}
}
#[derive(Debug)]
#[derive(Debug, Clone)]
pub struct PackageLocator {
pub name: String,
pub version: Option<u32>,
@ -102,7 +107,14 @@ impl FromStr for PackageLocator {
}
if let Some(caps) = tags_re.captures(s) {
tags = Some(caps.get(1).unwrap().as_str().split(",").map(|s| s.to_string()).collect());
tags = Some(
caps.get(1)
.unwrap()
.as_str()
.split(",")
.map(|s| s.to_string())
.collect(),
);
}
Ok(PackageLocator {
@ -111,4 +123,4 @@ impl FromStr for PackageLocator {
tags,
})
}
}
}

View file

@ -1 +1 @@
pub mod identifier;
pub mod identifier;

View file

@ -1 +0,0 @@
pub struct Arguments(Vec<String>);