Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .github/workflows/main.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ jobs:
with:
key: ${{ matrix.target }}
- run: cargo test
- run: cargo test --no-default-features

doc_fmt:
name: Document and check formatting
Expand Down
6 changes: 4 additions & 2 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,14 @@ edition = "2021"
rust-version = "1.66"

[dependencies]
unicode-width = "0.2.0"
unicode-width = { version = "0.2.0", optional = true }
std = { version = "1.0", package = "rustc-std-workspace-std", optional = true }
core = { version = "1.0", package = "rustc-std-workspace-core", optional = true }

[dev-dependencies]
log = "0.4"

[features]
rustc-dep-of-std = ["unicode-width/rustc-dep-of-std", "std", "core"]
default = ["unicode"]
rustc-dep-of-std = ["std", "core"]
unicode = ["dep:unicode-width"]
14 changes: 13 additions & 1 deletion src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,6 @@
#[cfg(test)]
#[macro_use]
extern crate log;
extern crate unicode_width;

use self::Fail::*;
use self::HasArg::*;
Expand All @@ -119,8 +118,21 @@ use std::iter::{repeat, IntoIterator};
use std::result;
use std::str::FromStr;

#[cfg(feature = "unicode")]
use unicode_width::UnicodeWidthStr;

#[cfg(not(feature = "unicode"))]
trait UnicodeWidthStr {
fn width(&self) -> usize;
}

#[cfg(not(feature = "unicode"))]
impl UnicodeWidthStr for str {
fn width(&self) -> usize {
self.len()
}
}

#[cfg(test)]
mod tests;

Expand Down
27 changes: 15 additions & 12 deletions src/tests/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -50,9 +50,9 @@ fn test_reqopt() {
let short_args = vec!["-t".to_string(), "20".to_string()];
match opts.parse(&short_args) {
Ok(ref m) => {
assert!((m.opt_present("test")));
assert!(m.opt_present("test"));
assert_eq!(m.opt_str("test").unwrap(), "20");
assert!((m.opt_present("t")));
assert!(m.opt_present("t"));
assert_eq!(m.opt_str("t").unwrap(), "20");
}
_ => {
Expand Down Expand Up @@ -111,17 +111,17 @@ fn test_optopt() {
Ok(ref m) => {
assert!(m.opt_present("test"));
assert_eq!(m.opt_str("test").unwrap(), "20");
assert!((m.opt_present("t")));
assert!(m.opt_present("t"));
assert_eq!(m.opt_str("t").unwrap(), "20");
}
_ => panic!(),
}
let short_args = vec!["-t".to_string(), "20".to_string()];
match opts.parse(&short_args) {
Ok(ref m) => {
assert!((m.opt_present("test")));
assert!(m.opt_present("test"));
assert_eq!(m.opt_str("test").unwrap(), "20");
assert!((m.opt_present("t")));
assert!(m.opt_present("t"));
assert_eq!(m.opt_str("t").unwrap(), "20");
}
_ => panic!(),
Expand Down Expand Up @@ -443,19 +443,19 @@ fn test_optmulti() {
opts.optmulti("t", "test", "testing", "TEST");
match opts.parse(&long_args) {
Ok(ref m) => {
assert!((m.opt_present("test")));
assert!(m.opt_present("test"));
assert_eq!(m.opt_str("test").unwrap(), "20");
assert!((m.opt_present("t")));
assert!(m.opt_present("t"));
assert_eq!(m.opt_str("t").unwrap(), "20");
}
_ => panic!(),
}
let short_args = vec!["-t".to_string(), "20".to_string()];
match opts.parse(&short_args) {
Ok(ref m) => {
assert!((m.opt_present("test")));
assert!(m.opt_present("test"));
assert_eq!(m.opt_str("test").unwrap(), "20");
assert!((m.opt_present("t")));
assert!(m.opt_present("t"));
assert_eq!(m.opt_str("t").unwrap(), "20");
}
_ => panic!(),
Expand Down Expand Up @@ -576,16 +576,16 @@ fn test_combined() {
assert!(m.free[1] == "free1");
assert_eq!(m.opt_str("s").unwrap(), "20");
assert!(m.free[2] == "free2");
assert!((m.opt_present("flag")));
assert!(m.opt_present("flag"));
assert_eq!(m.opt_str("long").unwrap(), "30");
assert!((m.opt_present("f")));
assert!(m.opt_present("f"));
let pair = m.opt_strs("m");
assert!(pair[0] == "40");
assert!(pair[1] == "50");
let pair = m.opt_strs("n");
assert!(pair[0] == "-A B");
assert!(pair[1] == "-60 70");
assert!((!m.opt_present("notpresent")));
assert!(!m.opt_present("notpresent"));
}
_ => panic!(),
}
Expand Down Expand Up @@ -888,6 +888,7 @@ Options:
}

#[test]
#[cfg(feature = "unicode")]
fn test_usage_description_multibyte_handling() {
let mut opts = Options::new();
opts.optflag(
Expand Down Expand Up @@ -919,6 +920,7 @@ Options:
}

#[test]
#[cfg(feature = "unicode")]
fn test_usage_description_newline_handling() {
let mut opts = Options::new();
opts.optflag(
Expand Down Expand Up @@ -950,6 +952,7 @@ Options:
}

#[test]
#[cfg(feature = "unicode")]
fn test_usage_multiwidth() {
let mut opts = Options::new();
opts.optflag("a", "apple", "apple description");
Expand Down