Skip to content

Commit

Permalink
pr: use clap to handle help & version
Browse files Browse the repository at this point in the history
  • Loading branch information
cakebaker committed Sep 5, 2022
1 parent e28301e commit 63b7634
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 30 deletions.
33 changes: 3 additions & 30 deletions src/uu/pr/src/pr.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ extern crate quick_error;

use chrono::offset::Local;
use chrono::DateTime;
use clap::{AppSettings, Arg, ArgMatches, Command};
use clap::{crate_version, Arg, ArgMatches, Command};
use itertools::Itertools;
use quick_error::ResultExt;
use regex::Regex;
Expand All @@ -22,9 +22,8 @@ use std::io::{stdin, stdout, BufRead, BufReader, Lines, Read, Write};
use std::os::unix::fs::FileTypeExt;

use uucore::display::Quotable;
use uucore::error::{set_exit_code, UResult};
use uucore::error::UResult;

const VERSION: &str = env!("CARGO_PKG_VERSION");
const ABOUT: &str =
"Write content of given file or standard input to standard output with pagination filter";
const AFTER_HELP: &str =
Expand Down Expand Up @@ -82,8 +81,6 @@ mod options {
pub const MERGE: &str = "merge";
pub const INDENT: &str = "indent";
pub const JOIN_LINES: &str = "join-lines";
pub const HELP: &str = "help";
pub const VERSION: &str = "version";
pub const FILES: &str = "files";
}

Expand Down Expand Up @@ -194,13 +191,11 @@ quick_error! {

pub fn uu_app<'a>() -> Command<'a> {
Command::new(uucore::util_name())
.version(VERSION)
.version(crate_version!())
.about(ABOUT)
.after_help(AFTER_HELP)
.infer_long_args(true)
.args_override_self(true)
.setting(AppSettings::NoAutoHelp)
.setting(AppSettings::NoAutoVersion)
.arg(
Arg::new(options::PAGES)
.long(options::PAGES)
Expand Down Expand Up @@ -357,17 +352,6 @@ pub fn uu_app<'a>() -> Command<'a> {
.help("merge full lines, turns off -W line truncation, no column
alignment, --sep-string[=STRING] sets separators")
)
.arg(
Arg::new(options::HELP)
.long(options::HELP)
.help("Show this help message")
)
.arg(
Arg::new(options::VERSION)
.short('V')
.long(options::VERSION)
.help("Show version information")
)
.arg(
Arg::new(options::FILES)
.multiple_occurrences(true)
Expand All @@ -387,21 +371,10 @@ pub fn uumain(args: impl uucore::Args) -> UResult<()> {
Ok(m) => m,
Err(e) => {
e.print()?;
set_exit_code(1);
return Ok(());
}
};

if matches.contains_id(options::VERSION) {
println!("{}", command.render_long_version());
return Ok(());
}

if matches.contains_id(options::HELP) {
command.print_help()?;
return Ok(());
}

let mut files = matches
.get_many::<String>(options::FILES)
.map(|v| v.map(|s| s.as_str()).collect::<Vec<_>>())
Expand Down
10 changes: 10 additions & 0 deletions tests/by-util/test_pr.rs
Original file line number Diff line number Diff line change
Expand Up @@ -461,3 +461,13 @@ fn test_value_for_number_lines() {
// Therefore, pr tries to access the file, which does not exist.
new_ucmd!().args(&["-n", "foo5.txt", "test.log"]).fails();
}

#[test]
fn test_help() {
new_ucmd!().arg("--help").succeeds();
}

#[test]
fn test_version() {
new_ucmd!().arg("--version").succeeds();
}

0 comments on commit 63b7634

Please sign in to comment.