mirror of
https://github.com/RGBCube/uutils-coreutils
synced 2025-07-28 03:27:44 +00:00
base64: Moved argument parsing to clap.
Moved argument parsing to clap and added tests to cover using "-" as stdin, passing in too many file arguments, and updated the "wrap" error message in the tests.
This commit is contained in:
parent
5567f32f58
commit
05b20c32a9
6 changed files with 171 additions and 72 deletions
1
Cargo.lock
generated
1
Cargo.lock
generated
|
@ -1688,6 +1688,7 @@ dependencies = [
|
||||||
name = "uu_base64"
|
name = "uu_base64"
|
||||||
version = "0.0.6"
|
version = "0.0.6"
|
||||||
dependencies = [
|
dependencies = [
|
||||||
|
"clap",
|
||||||
"uucore",
|
"uucore",
|
||||||
"uucore_procs",
|
"uucore_procs",
|
||||||
]
|
]
|
||||||
|
|
|
@ -15,6 +15,7 @@ edition = "2018"
|
||||||
path = "src/base64.rs"
|
path = "src/base64.rs"
|
||||||
|
|
||||||
[dependencies]
|
[dependencies]
|
||||||
|
clap = "2.33"
|
||||||
uucore = { version=">=0.0.8", package="uucore", path="../../uucore", features = ["encoding"] }
|
uucore = { version=">=0.0.8", package="uucore", path="../../uucore", features = ["encoding"] }
|
||||||
uucore_procs = { version=">=0.0.5", package="uucore_procs", path="../../uucore_procs" }
|
uucore_procs = { version=">=0.0.5", package="uucore_procs", path="../../uucore_procs" }
|
||||||
|
|
||||||
|
|
|
@ -8,12 +8,19 @@
|
||||||
|
|
||||||
#[macro_use]
|
#[macro_use]
|
||||||
extern crate uucore;
|
extern crate uucore;
|
||||||
|
|
||||||
use uucore::encoding::Format;
|
use uucore::encoding::Format;
|
||||||
use uucore::InvalidEncodingHandling;
|
use uucore::InvalidEncodingHandling;
|
||||||
|
|
||||||
|
use std::fs::File;
|
||||||
|
use std::io::{stdin, BufReader};
|
||||||
|
use std::path::Path;
|
||||||
|
|
||||||
|
use clap::{App, Arg};
|
||||||
|
|
||||||
mod base_common;
|
mod base_common;
|
||||||
|
|
||||||
static SYNTAX: &str = "[OPTION]... [FILE]";
|
static BASE64_ARG_ERROR: i32 = 1;
|
||||||
static SUMMARY: &str = "Base64 encode or decode FILE, or standard input, to standard output.";
|
static SUMMARY: &str = "Base64 encode or decode FILE, or standard input, to standard output.";
|
||||||
static LONG_HELP: &str = "
|
static LONG_HELP: &str = "
|
||||||
With no FILE, or when FILE is -, read standard input.
|
With no FILE, or when FILE is -, read standard input.
|
||||||
|
@ -24,14 +31,128 @@ static LONG_HELP: &str = "
|
||||||
to attempt to recover from any other non-alphabet bytes in the
|
to attempt to recover from any other non-alphabet bytes in the
|
||||||
encoded stream.
|
encoded stream.
|
||||||
";
|
";
|
||||||
|
static VERSION: &str = env!("CARGO_PKG_VERSION");
|
||||||
|
|
||||||
|
fn get_usage() -> String {
|
||||||
|
format!("{0} [OPTION]... [FILE]", executable!())
|
||||||
|
}
|
||||||
|
|
||||||
|
pub mod options {
|
||||||
|
pub static DECODE: &str = "decode";
|
||||||
|
pub static WRAP: &str = "wrap";
|
||||||
|
pub static IGNORE_GARBAGE: &str = "ignore-garbage";
|
||||||
|
pub static FILE: &str = "file";
|
||||||
|
}
|
||||||
|
|
||||||
|
struct Config {
|
||||||
|
decode: bool,
|
||||||
|
ignore_garbage: bool,
|
||||||
|
wrap_cols: Option<usize>,
|
||||||
|
to_read: Option<String>,
|
||||||
|
}
|
||||||
|
|
||||||
|
impl Config {
|
||||||
|
fn from(options: clap::ArgMatches) -> Config {
|
||||||
|
let file: Option<String> = match options.values_of(options::FILE) {
|
||||||
|
Some(mut values) => {
|
||||||
|
let name = values.next().unwrap();
|
||||||
|
if values.len() != 0 {
|
||||||
|
crash!(BASE64_ARG_ERROR, "extra operand ‘{}’", name);
|
||||||
|
}
|
||||||
|
|
||||||
|
if name == "-" {
|
||||||
|
None
|
||||||
|
} else {
|
||||||
|
if !Path::exists(Path::new(name)) {
|
||||||
|
crash!(BASE64_ARG_ERROR, "{}: No such file or directory", name);
|
||||||
|
}
|
||||||
|
Some(name.to_owned())
|
||||||
|
}
|
||||||
|
}
|
||||||
|
None => None,
|
||||||
|
};
|
||||||
|
|
||||||
|
let cols = match options.value_of(options::WRAP) {
|
||||||
|
Some(num) => match num.parse::<usize>() {
|
||||||
|
Ok(n) => Some(n),
|
||||||
|
Err(e) => {
|
||||||
|
crash!(BASE64_ARG_ERROR, "invalid wrap size: ‘{}’: {}", num, e);
|
||||||
|
}
|
||||||
|
},
|
||||||
|
None => None,
|
||||||
|
};
|
||||||
|
|
||||||
|
Config {
|
||||||
|
decode: options.is_present(options::DECODE),
|
||||||
|
ignore_garbage: options.is_present(options::IGNORE_GARBAGE),
|
||||||
|
wrap_cols: cols,
|
||||||
|
to_read: file,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
pub fn uumain(args: impl uucore::Args) -> i32 {
|
pub fn uumain(args: impl uucore::Args) -> i32 {
|
||||||
base_common::execute(
|
let format = Format::Base64;
|
||||||
args.collect_str(InvalidEncodingHandling::Ignore)
|
let usage = get_usage();
|
||||||
.accept_any(),
|
let app = App::new(executable!())
|
||||||
SYNTAX,
|
.version(VERSION)
|
||||||
SUMMARY,
|
.about(SUMMARY)
|
||||||
LONG_HELP,
|
.usage(&usage[..])
|
||||||
Format::Base64,
|
.about(LONG_HELP)
|
||||||
|
// Format arguments.
|
||||||
|
.arg(
|
||||||
|
Arg::with_name(options::DECODE)
|
||||||
|
.short("d")
|
||||||
|
.long(options::DECODE)
|
||||||
|
.help("decode data"),
|
||||||
)
|
)
|
||||||
|
.arg(
|
||||||
|
Arg::with_name(options::IGNORE_GARBAGE)
|
||||||
|
.short("i")
|
||||||
|
.long(options::IGNORE_GARBAGE)
|
||||||
|
.help("when decoding, ignore non-alphabetic characters"),
|
||||||
|
)
|
||||||
|
.arg(
|
||||||
|
Arg::with_name(options::WRAP)
|
||||||
|
.short("w")
|
||||||
|
.long(options::WRAP)
|
||||||
|
.takes_value(true)
|
||||||
|
.help(
|
||||||
|
"wrap encoded lines after COLS character (default 76, 0 to disable wrapping)",
|
||||||
|
),
|
||||||
|
)
|
||||||
|
// "multiple" arguments are used to check whether there is more than one
|
||||||
|
// file passed in.
|
||||||
|
.arg(Arg::with_name(options::FILE).index(1).multiple(true));
|
||||||
|
|
||||||
|
let arg_list = args
|
||||||
|
.collect_str(InvalidEncodingHandling::ConvertLossy)
|
||||||
|
.accept_any();
|
||||||
|
let config: Config = Config::from(app.get_matches_from(arg_list));
|
||||||
|
match config.to_read {
|
||||||
|
// Read from file.
|
||||||
|
Some(name) => {
|
||||||
|
let file_buf = safe_unwrap!(File::open(Path::new(&name)));
|
||||||
|
let mut input = BufReader::new(file_buf);
|
||||||
|
base_common::handle_input(
|
||||||
|
&mut input,
|
||||||
|
format,
|
||||||
|
config.wrap_cols,
|
||||||
|
config.ignore_garbage,
|
||||||
|
config.decode,
|
||||||
|
);
|
||||||
|
}
|
||||||
|
// stdin
|
||||||
|
None => {
|
||||||
|
base_common::handle_input(
|
||||||
|
&mut stdin().lock(),
|
||||||
|
format,
|
||||||
|
config.wrap_cols,
|
||||||
|
config.ignore_garbage,
|
||||||
|
config.decode,
|
||||||
|
);
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
0
|
||||||
}
|
}
|
||||||
|
|
|
@ -7,68 +7,11 @@
|
||||||
// For the full copyright and license information, please view the LICENSE file
|
// For the full copyright and license information, please view the LICENSE file
|
||||||
// that was distributed with this source code.
|
// that was distributed with this source code.
|
||||||
|
|
||||||
use std::fs::File;
|
use std::io::{stdout, Read, Write};
|
||||||
use std::io::{stdin, stdout, BufReader, Read, Write};
|
|
||||||
use std::path::Path;
|
|
||||||
|
|
||||||
use uucore::encoding::{wrap_print, Data, Format};
|
use uucore::encoding::{wrap_print, Data, Format};
|
||||||
|
|
||||||
pub fn execute(
|
pub fn handle_input<R: Read>(
|
||||||
args: Vec<String>,
|
|
||||||
syntax: &str,
|
|
||||||
summary: &str,
|
|
||||||
long_help: &str,
|
|
||||||
format: Format,
|
|
||||||
) -> i32 {
|
|
||||||
let matches = app!(syntax, summary, long_help)
|
|
||||||
.optflag("d", "decode", "decode data")
|
|
||||||
.optflag(
|
|
||||||
"i",
|
|
||||||
"ignore-garbage",
|
|
||||||
"when decoding, ignore non-alphabetic characters",
|
|
||||||
)
|
|
||||||
.optopt(
|
|
||||||
"w",
|
|
||||||
"wrap",
|
|
||||||
"wrap encoded lines after COLS character (default 76, 0 to disable wrapping)",
|
|
||||||
"COLS",
|
|
||||||
)
|
|
||||||
.parse(args);
|
|
||||||
|
|
||||||
let line_wrap = matches.opt_str("wrap").map(|s| match s.parse() {
|
|
||||||
Ok(n) => n,
|
|
||||||
Err(e) => {
|
|
||||||
crash!(1, "invalid wrap size: ‘{}’: {}", s, e);
|
|
||||||
}
|
|
||||||
});
|
|
||||||
let ignore_garbage = matches.opt_present("ignore-garbage");
|
|
||||||
let decode = matches.opt_present("decode");
|
|
||||||
|
|
||||||
if matches.free.len() > 1 {
|
|
||||||
show_usage_error!("extra operand ‘{}’", matches.free[0]);
|
|
||||||
return 1;
|
|
||||||
}
|
|
||||||
|
|
||||||
if matches.free.is_empty() || &matches.free[0][..] == "-" {
|
|
||||||
let stdin_raw = stdin();
|
|
||||||
handle_input(
|
|
||||||
&mut stdin_raw.lock(),
|
|
||||||
format,
|
|
||||||
line_wrap,
|
|
||||||
ignore_garbage,
|
|
||||||
decode,
|
|
||||||
);
|
|
||||||
} else {
|
|
||||||
let path = Path::new(matches.free[0].as_str());
|
|
||||||
let file_buf = safe_unwrap!(File::open(&path));
|
|
||||||
let mut input = BufReader::new(file_buf);
|
|
||||||
handle_input(&mut input, format, line_wrap, ignore_garbage, decode);
|
|
||||||
};
|
|
||||||
|
|
||||||
0
|
|
||||||
}
|
|
||||||
|
|
||||||
fn handle_input<R: Read>(
|
|
||||||
input: &mut R,
|
input: &mut R,
|
||||||
format: Format,
|
format: Format,
|
||||||
line_wrap: Option<usize>,
|
line_wrap: Option<usize>,
|
||||||
|
|
|
@ -7,6 +7,21 @@ fn test_encode() {
|
||||||
.pipe_in(input)
|
.pipe_in(input)
|
||||||
.succeeds()
|
.succeeds()
|
||||||
.stdout_only("aGVsbG8sIHdvcmxkIQ==\n");
|
.stdout_only("aGVsbG8sIHdvcmxkIQ==\n");
|
||||||
|
|
||||||
|
// Using '-' as our file
|
||||||
|
new_ucmd!()
|
||||||
|
.arg("-")
|
||||||
|
.pipe_in(input)
|
||||||
|
.succeeds()
|
||||||
|
.stdout_only("aGVsbG8sIHdvcmxkIQ==\n");
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn test_base64_encode_file() {
|
||||||
|
new_ucmd!()
|
||||||
|
.arg("input-simple.txt")
|
||||||
|
.succeeds()
|
||||||
|
.stdout_only("SGVsbG8sIFdvcmxkIQo=\n");
|
||||||
}
|
}
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
|
@ -60,10 +75,9 @@ fn test_wrap() {
|
||||||
#[test]
|
#[test]
|
||||||
fn test_wrap_no_arg() {
|
fn test_wrap_no_arg() {
|
||||||
for wrap_param in vec!["-w", "--wrap"] {
|
for wrap_param in vec!["-w", "--wrap"] {
|
||||||
new_ucmd!().arg(wrap_param).fails().stderr_only(format!(
|
new_ucmd!().arg(wrap_param).fails().stderr_contains(
|
||||||
"base64: error: Argument to option '{}' missing\n",
|
&"The argument '--wrap <wrap>' requires a value but none was supplied",
|
||||||
if wrap_param == "-w" { "w" } else { "wrap" }
|
);
|
||||||
));
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -77,3 +91,21 @@ fn test_wrap_bad_arg() {
|
||||||
.stderr_only("base64: error: invalid wrap size: ‘b’: invalid digit found in string\n");
|
.stderr_only("base64: error: invalid wrap size: ‘b’: invalid digit found in string\n");
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn test_base64_extra_operand() {
|
||||||
|
// Expect a failure when multiple files are specified.
|
||||||
|
new_ucmd!()
|
||||||
|
.arg("a.txt")
|
||||||
|
.arg("a.txt")
|
||||||
|
.fails()
|
||||||
|
.stderr_only("base64: error: extra operand ‘a.txt’");
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn test_base64_file_not_found() {
|
||||||
|
new_ucmd!()
|
||||||
|
.arg("a.txt")
|
||||||
|
.fails()
|
||||||
|
.stderr_only("base64: error: a.txt: No such file or directory");
|
||||||
|
}
|
||||||
|
|
1
tests/fixtures/base64/input-simple.txt
vendored
Normal file
1
tests/fixtures/base64/input-simple.txt
vendored
Normal file
|
@ -0,0 +1 @@
|
||||||
|
Hello, World!
|
Loading…
Add table
Add a link
Reference in a new issue