Skip to content

Add usage_column and usage_width #121

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
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
34 changes: 30 additions & 4 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -131,6 +131,8 @@ pub struct Options {
grps: Vec<OptGroup>,
parsing_style: ParsingStyle,
long_only: bool,
usage_column: usize,
usage_width: usize,
}

impl Default for Options {
Expand All @@ -146,6 +148,8 @@ impl Options {
grps: Vec::new(),
parsing_style: ParsingStyle::FloatingFrees,
long_only: false,
usage_column: 24,
usage_width: 54,
}
}

Expand Down Expand Up @@ -604,7 +608,7 @@ impl Options {

/// Derive usage items from a set of options.
fn usage_items<'a>(&'a self) -> Box<dyn Iterator<Item = String> + 'a> {
let desc_sep = format!("\n{}", repeat(" ").take(24).collect::<String>());
let desc_sep = format!("\n{}", repeat(" ").take(self.usage_column).collect::<String>());

let any_short = self.grps.iter().any(|optref| !optref.short_name.is_empty());

Expand Down Expand Up @@ -664,22 +668,44 @@ impl Options {
}

let rowlen = row.width();
if rowlen < 24 {
for _ in 0..24 - rowlen {
if rowlen < self.usage_column {
for _ in 0..self.usage_column - rowlen {
row.push(' ');
}
} else {
row.push_str(&desc_sep)
}

let desc_rows = each_split_within(&desc, 54);
let desc_rows = each_split_within(&desc, self.usage_width);
row.push_str(&desc_rows.join(&desc_sep));

row
});

Box::new(rows)
}

/// Get the display column of usage
pub fn usage_column(&self) -> usize {
self.usage_column.clone()
}

/// Set the display column of usage
pub fn set_usage_column(&mut self, column: usize) -> &mut Options {
self.usage_column = column;
self
}

/// Get the display width of usage
pub fn usage_width(&self) -> usize {
self.usage_width.clone()
}

/// Set the display width of usage
pub fn set_usage_width(&mut self, width: usize) -> &mut Options {
self.usage_width = width;
self
}
}

fn validate_names(short_name: &str, long_name: &str) {
Expand Down
70 changes: 70 additions & 0 deletions src/tests/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -921,6 +921,41 @@ Options:
assert!(usage == expected)
}

#[test]
fn test_usage_description_newline_handling_custom_width() {
let mut opts = Options::new();
opts.set_usage_width(30);
opts.optflag(
"k",
"k\u{2013}w\u{2013}",
"The word kiwi is normally spelled with two i's",
);
opts.optflag(
"a",
"apple",
"This description forces a new line.\n Here is a premature\n\
newline",
);

let expected = "Usage: fruits

Options:
-k, --k–w– The word kiwi is normally
spelled with two i's
-a, --apple This description forces a new
line.
Here is a premature
newline
";

let usage = opts.usage("Usage: fruits");

debug!("expected: <<{}>>", expected);
debug!("generated: <<{}>>", usage);
assert!(usage == expected)
}


#[test]
fn test_usage_description_newline_handling() {
let mut opts = Options::new();
Expand Down Expand Up @@ -952,6 +987,41 @@ Options:
assert!(usage == expected)
}


#[test]
fn test_usage_description_newline_handling_custom_column() {
let mut opts = Options::new();
opts.set_usage_column(10);
opts.optflag(
"k",
"k\u{2013}w\u{2013}",
"The word kiwi is normally spelled with two i's",
);
opts.optflag(
"a",
"apple",
"This description forces a new line.\n Here is a premature\n\
newline",
);

let expected = "Usage: fruits

Options:
-k, --k–w–\u{0020}
The word kiwi is normally spelled with two i's
-a, --apple\u{0020}
This description forces a new line.
Here is a premature
newline
";

let usage = opts.usage("Usage: fruits");

debug!("expected: <<{}>>", expected);
debug!("generated: <<{}>>", usage);
assert!(usage == expected)
}

#[test]
fn test_usage_multiwidth() {
let mut opts = Options::new();
Expand Down