Skip to content

Commit d588c1b

Browse files
make both cli and clite expose a "clap" feature
1 parent 3f8a4ea commit d588c1b

File tree

6 files changed

+49
-12
lines changed

6 files changed

+49
-12
lines changed

cli/Cargo.toml

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,14 +25,16 @@ members = ["."]
2525
name = "zip-cli"
2626

2727
[dependencies]
28-
clap = { version = "4.5.15", features = ["derive"] }
28+
clap = { version = "4.5.15", features = ["derive"], optional = true }
2929
eyre = "0.6"
3030

3131
[dependencies.zip]
3232
path = ".."
3333
default-features = false
3434

3535
[features]
36+
clap = ["dep:clap"]
37+
3638
aes-crypto = ["zip/aes-crypto"]
3739
bzip2 = ["zip/bzip2"]
3840
chrono = ["zip/chrono"]
@@ -46,7 +48,9 @@ lzma = ["zip/lzma"]
4648
time = ["zip/time"]
4749
xz = ["zip/xz"]
4850
zstd = ["zip/zstd"]
51+
4952
default = [
53+
"clap",
5054
"aes-crypto",
5155
"bzip2",
5256
"deflate64",
@@ -58,7 +62,6 @@ default = [
5862
]
5963

6064

61-
# Reduce the size of the zip-cli binary.
6265
[profile.release]
6366
strip = true
6467
lto = true

cli/clite/Cargo.toml

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,15 +23,18 @@ members = ["."]
2323
name = "zip-clite"
2424

2525
[dependencies]
26-
clap = { version = "4.5.15", features = ["derive"] }
26+
clap = { version = "4.5.15", features = ["derive"], optional = true }
2727
eyre = "0.6"
2828

2929
[dependencies.zip-cli]
3030
path = ".."
3131
default-features = false
3232
features = ["deflate-flate2", "deflate-zlib"]
3333

34-
# Reduce the size of the zip-cli binary.
34+
[features]
35+
clap = ["zip-cli/clap", "dep:clap"]
36+
default = ["clap"]
37+
3538
[profile.release]
3639
strip = true
3740
lto = true

cli/clite/src/main.rs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,8 @@
11
use std::io;
2+
#[cfg(not(feature = "clap"))]
3+
use std::env;
24

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

@@ -8,13 +11,16 @@ use zip_cli::compress::execute_compress;
811
use zip_cli::ErrHandle;
912

1013
fn main() -> Result<(), Report> {
14+
#[cfg(feature = "clap")]
1115
let ZipCli { verbose, command } = match ZipCli::try_parse() {
1216
Ok(args) => args,
1317
Err(e) => match e.kind() {
1418
ErrorKind::Format | ErrorKind::Io | ErrorKind::InvalidUtf8 => return Err(e.into()),
1519
_ => e.exit(),
1620
},
1721
};
22+
#[cfg(not(feature = "clap"))]
23+
let ZipCli { verbose, command } = ZipCli::parse_argv(env::args_os())?;
1824
let mut err = if verbose {
1925
ErrHandle::Output(io::stderr())
2026
} else {

cli/src/args.rs

Lines changed: 26 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,36 @@
1-
use std::{collections::VecDeque, ffi::OsString, num::ParseIntError, path::PathBuf};
1+
#[cfg(not(feature = "clap"))]
2+
use eyre::Report;
23

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

8-
#[derive(Parser, Debug)]
9-
#[command(version, about)]
10+
#[cfg(feature = "clap")]
11+
use std::collections::VecDeque;
12+
use std::{ffi::OsString, num::ParseIntError, path::PathBuf};
13+
14+
#[derive(Debug)]
15+
#[cfg_attr(feature = "clap", derive(Parser))]
16+
#[cfg_attr(feature = "clap", command(version, about))]
1017
pub struct ZipCli {
1118
/// Write additional output to stderr.
12-
#[arg(short, long)]
19+
#[cfg_attr(feature = "clap", arg(short, long))]
1320
pub verbose: bool,
14-
#[command(subcommand)]
21+
#[cfg_attr(feature = "clap", command(subcommand))]
1522
pub command: ZipCommand,
1623
}
1724

18-
#[derive(Subcommand, Debug)]
25+
#[cfg(not(feature = "clap"))]
26+
impl ZipCli {
27+
pub fn parse_argv(_argv: impl IntoIterator<Item = OsString>) -> Result<Self, Report> {
28+
todo!()
29+
}
30+
}
31+
32+
#[derive(Debug)]
33+
#[cfg_attr(feature = "clap", derive(Subcommand))]
1934
pub enum ZipCommand {
2035
/// do a compress
2136
///
@@ -28,7 +43,8 @@ pub enum ZipCommand {
2843
Extract,
2944
}
3045

31-
#[derive(ValueEnum, Debug, Copy, Clone, PartialEq, Eq, PartialOrd, Ord)]
46+
#[derive(Debug, Copy, Clone, PartialEq, Eq, PartialOrd, Ord)]
47+
#[cfg_attr(feature = "clap", derive(ValueEnum))]
3248
pub enum CompressionMethodArg {
3349
/// uncompressed
3450
Stored,
@@ -79,6 +95,7 @@ pub struct Compress {
7995
pub positional_paths: Vec<PathBuf>,
8096
}
8197

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

352+
#[cfg(feature = "clap")]
335353
fn positional_only_arg(arg: Arg) -> Arg {
336354
arg.action(ArgAction::Append)
337355
.num_args(0)
@@ -340,6 +358,7 @@ fn positional_only_arg(arg: Arg) -> Arg {
340358
.default_missing_value("true")
341359
}
342360

361+
#[cfg(feature = "clap")]
343362
impl Args for Compress {
344363
fn augment_args(cmd: Command) -> Command {
345364
cmd.group(ArgGroup::new("output").multiple(false))

cli/src/main.rs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,8 @@
11
use std::io;
2+
#[cfg(not(feature = "clap"))]
3+
use std::env;
24

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

@@ -8,13 +11,16 @@ use zip_cli::compress::execute_compress;
811
use zip_cli::ErrHandle;
912

1013
fn main() -> Result<(), Report> {
14+
#[cfg(feature = "clap")]
1115
let ZipCli { verbose, command } = match ZipCli::try_parse() {
1216
Ok(args) => args,
1317
Err(e) => match e.kind() {
1418
ErrorKind::Format | ErrorKind::Io | ErrorKind::InvalidUtf8 => return Err(e.into()),
1519
_ => e.exit(),
1620
},
1721
};
22+
#[cfg(not(feature = "clap"))]
23+
let ZipCli { verbose, command } = ZipCli::parse_argv(env::args_os())?;
1824
let mut err = if verbose {
1925
ErrHandle::Output(io::stderr())
2026
} else {

src/write.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ use crate::types::{
1515
ZipRawValues, MIN_VERSION,
1616
};
1717
use crate::write::ffi::S_IFLNK;
18-
#[cfg(any(feature = "_deflate-any", feature = "bzip2", feature = "zstd",))]
18+
#[cfg(feature = "deflate-zopfli")]
1919
use core::num::NonZeroU64;
2020
use crc32fast::Hasher;
2121
use indexmap::IndexMap;

0 commit comments

Comments
 (0)