From 867d91ffab7c6aab65536249eb4a626a39b1ba79 Mon Sep 17 00:00:00 2001 From: Didier Date: Mon, 17 Jul 2023 11:50:19 +0200 Subject: [PATCH] feat: arguments with clip --- pkgr/Cargo.toml | 2 +- pkgr/src/commands/mod.rs | 64 +++++++++++++++++++++++++++++++--- pkgr/src/main.rs | 38 ++++---------------- pkgr/src/package/identifier.rs | 24 +++++++++---- pkgr/src/package/mod.rs | 2 +- pkgr/src/prelude.rs | 1 - 6 files changed, 85 insertions(+), 46 deletions(-) delete mode 100644 pkgr/src/prelude.rs diff --git a/pkgr/Cargo.toml b/pkgr/Cargo.toml index 92a0e91..9f719ea 100644 --- a/pkgr/Cargo.toml +++ b/pkgr/Cargo.toml @@ -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"] } diff --git a/pkgr/src/commands/mod.rs b/pkgr/src/commands/mod.rs index 7f18ebd..b5d698c 100644 --- a/pkgr/src/commands/mod.rs +++ b/pkgr/src/commands/mod.rs @@ -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, -} \ No newline at end of file + #[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."); + } + } + } +} diff --git a/pkgr/src/main.rs b/pkgr/src/main.rs index 9029074..50b9007 100644 --- a/pkgr/src/main.rs +++ b/pkgr/src/main.rs @@ -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 = 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: {} [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() -} \ No newline at end of file +} diff --git a/pkgr/src/package/identifier.rs b/pkgr/src/package/identifier.rs index 01f9f6b..07b2bcc 100644 --- a/pkgr/src/package/identifier.rs +++ b/pkgr/src/package/identifier.rs @@ -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, @@ -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, }) } -} \ No newline at end of file +} diff --git a/pkgr/src/package/mod.rs b/pkgr/src/package/mod.rs index 8f83cb4..34cdf0a 100644 --- a/pkgr/src/package/mod.rs +++ b/pkgr/src/package/mod.rs @@ -1 +1 @@ -pub mod identifier; \ No newline at end of file +pub mod identifier; diff --git a/pkgr/src/prelude.rs b/pkgr/src/prelude.rs deleted file mode 100644 index d7ac579..0000000 --- a/pkgr/src/prelude.rs +++ /dev/null @@ -1 +0,0 @@ -pub struct Arguments(Vec); \ No newline at end of file