Skip to content
This repository has been archived by the owner on Jun 27, 2024. It is now read-only.

Commit

Permalink
refactor: switch parsing to clap v3
Browse files Browse the repository at this point in the history
  • Loading branch information
tranzystorekk committed Jan 2, 2022
1 parent 106b528 commit 6170233
Show file tree
Hide file tree
Showing 4 changed files with 84 additions and 76 deletions.
118 changes: 60 additions & 58 deletions Cargo.lock

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

3 changes: 2 additions & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,9 @@ edition = "2018"

bstr = "0.2"
confy = "0.4"
clap = { version = "3.0", features = ["derive"] }
clap_complete = "3.0"
itertools = "0.10"
log = "0.4"
serde = { version = "1.0", features = ["derive"] }
stderrlog = "0.5"
structopt = "0.3"
35 changes: 20 additions & 15 deletions src/cli.rs
Original file line number Diff line number Diff line change
@@ -1,18 +1,23 @@
use std::os::unix::process::ExitStatusExt;
use std::process::ExitStatus;

use structopt::clap::Shell;
use structopt::StructOpt;
use clap::{IntoApp, Parser, Subcommand};
use clap_complete::Shell;

pub const PKG_NAME: &str = env!("CARGO_PKG_NAME");

pub fn generate_completion(shell: Shell) -> std::io::Result<ExitStatus> {
Cli::clap().gen_completions_to(PKG_NAME, shell, &mut std::io::stdout());
clap_complete::generate(
shell,
&mut Cli::into_app(),
PKG_NAME,
&mut std::io::stdout(),
);

Ok(ExitStatus::from_raw(0))
}

#[derive(Copy, Clone, Debug, StructOpt)]
#[derive(Copy, Clone, Debug, Subcommand)]
pub enum Orphans {
/// List orphans
List,
Expand All @@ -27,30 +32,30 @@ impl Default for Orphans {
}
}

#[derive(Debug, StructOpt)]
#[derive(Debug, Subcommand)]
pub enum Command {
/// Install packages
Install {
/// Packages to be installed
#[structopt(value_name = "PKG", required = true, parse(from_str))]
#[clap(value_name = "PKG", required = true)]
packages: Vec<String>,

/// Mark installed packages as dependencies
#[structopt(long, short = "d")]
#[clap(long, short = 'd')]
as_deps: bool,
},

/// Remove packages
Uninstall {
/// Packages to be removed
#[structopt(value_name = "PKG", required = true, parse(from_str))]
#[clap(value_name = "PKG", required = true)]
packages: Vec<String>,
},

/// Display package info
Info {
/// Packages to be inspected
#[structopt(value_name = "PKG", required = true, parse(from_str))]
#[clap(value_name = "PKG", required = true)]
packages: Vec<String>,
},

Expand All @@ -65,14 +70,14 @@ pub enum Command {

/// Manage orphaned packages
Orphans {
#[structopt(subcommand)]
#[clap(subcommand)]
cmd: Option<Orphans>,
},

/// Generate a completion script
Completion {
/// Shell type
#[structopt(value_name = "SHELL", possible_values = &["bash", "zsh", "fish", "powershell", "elvish"])]
#[clap(value_name = "SHELL", arg_enum)]
shell: Shell,
},
}
Expand All @@ -87,13 +92,13 @@ pub enum Command {
/// requires_root = true # whether this wrapper needs root permissions (granted via sudo)
///
/// If this file is missing, a default configuration is created that runs `sudo pacman`.
#[derive(Debug, StructOpt)]
#[structopt(verbatim_doc_comment)]
#[derive(Debug, Parser)]
#[clap(verbatim_doc_comment, version)]
pub struct Cli {
#[structopt(subcommand)]
#[clap(subcommand)]
pub sub: Command,

/// Display verbose logs (debug etc.)
#[structopt(short, long, global = true)]
#[clap(short, long, global = true)]
pub verbose: bool,
}
4 changes: 2 additions & 2 deletions src/main.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use std::os::unix::process::ExitStatusExt;

use clap::Parser;
use log::{debug, error};
use structopt::StructOpt;

use pakr::cli::{Cli, Command, Orphans};
use pakr::wrapper::PacmanWrapper;
Expand All @@ -14,7 +14,7 @@ fn verbosity(verbose: bool) -> usize {
}

fn run_app() -> std::io::Result<i32> {
let cli = Cli::from_args();
let cli = Cli::parse();
let pac = PacmanWrapper::from_config()?;

stderrlog::new()
Expand Down

0 comments on commit 6170233

Please sign in to comment.