Skip to content

Commit

Permalink
make both cli and clite expose a "clap" feature
Browse files Browse the repository at this point in the history
  • Loading branch information
cosmicexplorer committed Aug 14, 2024
1 parent 3f8a4ea commit d588c1b
Show file tree
Hide file tree
Showing 6 changed files with 49 additions and 12 deletions.
7 changes: 5 additions & 2 deletions cli/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -25,14 +25,16 @@ members = ["."]
name = "zip-cli"

[dependencies]
clap = { version = "4.5.15", features = ["derive"] }
clap = { version = "4.5.15", features = ["derive"], optional = true }
eyre = "0.6"

[dependencies.zip]
path = ".."
default-features = false

[features]
clap = ["dep:clap"]

aes-crypto = ["zip/aes-crypto"]
bzip2 = ["zip/bzip2"]
chrono = ["zip/chrono"]
Expand All @@ -46,7 +48,9 @@ lzma = ["zip/lzma"]
time = ["zip/time"]
xz = ["zip/xz"]
zstd = ["zip/zstd"]

default = [
"clap",
"aes-crypto",
"bzip2",
"deflate64",
Expand All @@ -58,7 +62,6 @@ default = [
]


# Reduce the size of the zip-cli binary.
[profile.release]
strip = true
lto = true
Expand Down
7 changes: 5 additions & 2 deletions cli/clite/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -23,15 +23,18 @@ members = ["."]
name = "zip-clite"

[dependencies]
clap = { version = "4.5.15", features = ["derive"] }
clap = { version = "4.5.15", features = ["derive"], optional = true }
eyre = "0.6"

[dependencies.zip-cli]
path = ".."
default-features = false
features = ["deflate-flate2", "deflate-zlib"]

# Reduce the size of the zip-cli binary.
[features]
clap = ["zip-cli/clap", "dep:clap"]
default = ["clap"]

[profile.release]
strip = true
lto = true
Expand Down
6 changes: 6 additions & 0 deletions cli/clite/src/main.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
use std::io;
#[cfg(not(feature = "clap"))]
use std::env;

#[cfg(feature = "clap")]
use clap::{error::ErrorKind, Parser};
use eyre::Report;

Expand All @@ -8,13 +11,16 @@ use zip_cli::compress::execute_compress;
use zip_cli::ErrHandle;

fn main() -> Result<(), Report> {
#[cfg(feature = "clap")]
let ZipCli { verbose, command } = match ZipCli::try_parse() {
Ok(args) => args,
Err(e) => match e.kind() {
ErrorKind::Format | ErrorKind::Io | ErrorKind::InvalidUtf8 => return Err(e.into()),
_ => e.exit(),
},
};
#[cfg(not(feature = "clap"))]
let ZipCli { verbose, command } = ZipCli::parse_argv(env::args_os())?;
let mut err = if verbose {
ErrHandle::Output(io::stderr())
} else {
Expand Down
33 changes: 26 additions & 7 deletions cli/src/args.rs
Original file line number Diff line number Diff line change
@@ -1,21 +1,36 @@
use std::{collections::VecDeque, ffi::OsString, num::ParseIntError, path::PathBuf};
#[cfg(not(feature = "clap"))]
use eyre::Report;

#[cfg(feature = "clap")]
use clap::{
builder::ValueParser, value_parser, Arg, ArgAction, ArgGroup, ArgMatches, Args, Command,
FromArgMatches, Parser, Subcommand, ValueEnum,
};

#[derive(Parser, Debug)]
#[command(version, about)]
#[cfg(feature = "clap")]
use std::collections::VecDeque;
use std::{ffi::OsString, num::ParseIntError, path::PathBuf};

#[derive(Debug)]
#[cfg_attr(feature = "clap", derive(Parser))]
#[cfg_attr(feature = "clap", command(version, about))]
pub struct ZipCli {
/// Write additional output to stderr.
#[arg(short, long)]
#[cfg_attr(feature = "clap", arg(short, long))]
pub verbose: bool,
#[command(subcommand)]
#[cfg_attr(feature = "clap", command(subcommand))]
pub command: ZipCommand,
}

#[derive(Subcommand, Debug)]
#[cfg(not(feature = "clap"))]
impl ZipCli {
pub fn parse_argv(_argv: impl IntoIterator<Item = OsString>) -> Result<Self, Report> {
todo!()
}
}

#[derive(Debug)]
#[cfg_attr(feature = "clap", derive(Subcommand))]
pub enum ZipCommand {
/// do a compress
///
Expand All @@ -28,7 +43,8 @@ pub enum ZipCommand {
Extract,
}

#[derive(ValueEnum, Debug, Copy, Clone, PartialEq, Eq, PartialOrd, Ord)]
#[derive(Debug, Copy, Clone, PartialEq, Eq, PartialOrd, Ord)]
#[cfg_attr(feature = "clap", derive(ValueEnum))]
pub enum CompressionMethodArg {
/// uncompressed
Stored,
Expand Down Expand Up @@ -79,6 +95,7 @@ pub struct Compress {
pub positional_paths: Vec<PathBuf>,
}

#[cfg(feature = "clap")]
impl FromArgMatches for Compress {
fn from_arg_matches(matches: &ArgMatches) -> Result<Self, clap::Error> {
let allow_stdout = matches.get_flag("stdout");
Expand Down Expand Up @@ -332,6 +349,7 @@ impl FromArgMatches for Compress {
}
}

#[cfg(feature = "clap")]
fn positional_only_arg(arg: Arg) -> Arg {
arg.action(ArgAction::Append)
.num_args(0)
Expand All @@ -340,6 +358,7 @@ fn positional_only_arg(arg: Arg) -> Arg {
.default_missing_value("true")
}

#[cfg(feature = "clap")]
impl Args for Compress {
fn augment_args(cmd: Command) -> Command {
cmd.group(ArgGroup::new("output").multiple(false))
Expand Down
6 changes: 6 additions & 0 deletions cli/src/main.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
use std::io;
#[cfg(not(feature = "clap"))]
use std::env;

#[cfg(feature = "clap")]
use clap::{error::ErrorKind, Parser};
use eyre::Report;

Expand All @@ -8,13 +11,16 @@ use zip_cli::compress::execute_compress;
use zip_cli::ErrHandle;

fn main() -> Result<(), Report> {
#[cfg(feature = "clap")]
let ZipCli { verbose, command } = match ZipCli::try_parse() {
Ok(args) => args,
Err(e) => match e.kind() {
ErrorKind::Format | ErrorKind::Io | ErrorKind::InvalidUtf8 => return Err(e.into()),
_ => e.exit(),
},
};
#[cfg(not(feature = "clap"))]
let ZipCli { verbose, command } = ZipCli::parse_argv(env::args_os())?;
let mut err = if verbose {
ErrHandle::Output(io::stderr())
} else {
Expand Down
2 changes: 1 addition & 1 deletion src/write.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ use crate::types::{
ZipRawValues, MIN_VERSION,
};
use crate::write::ffi::S_IFLNK;
#[cfg(any(feature = "_deflate-any", feature = "bzip2", feature = "zstd",))]
#[cfg(feature = "deflate-zopfli")]
use core::num::NonZeroU64;
use crc32fast::Hasher;
use indexmap::IndexMap;
Expand Down

0 comments on commit d588c1b

Please sign in to comment.