Skip to content
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

input: add --skip-lines option #266

Merged
merged 2 commits into from
Apr 20, 2022
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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
7 changes: 7 additions & 0 deletions src/cmd/input.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ input options:
--escape <arg> The escape character to use. When not specified,
quotes are escaped by doubling them.
--no-quoting Disable quoting completely.
--skip-lines <arg> The number of lines to skip.

Common options:
-h, --help Display this message
Expand All @@ -35,6 +36,7 @@ struct Args {
flag_quote: Delimiter,
flag_escape: Option<Delimiter>,
flag_no_quoting: bool,
flag_skip_lines: Option<usize>,
}

pub fn run(argv: &[&str]) -> CliResult<()> {
Expand All @@ -55,6 +57,11 @@ pub fn run(argv: &[&str]) -> CliResult<()> {
let mut rdr = rconfig.reader()?;
let mut wtr = wconfig.writer()?;
let mut row = csv::ByteRecord::new();
if let Some(skip_lines) = args.flag_skip_lines {
for _i in 1..=skip_lines {
rdr.read_byte_record(&mut row)?;
}
}
while rdr.read_byte_record(&mut row)? {
wtr.write_record(&row)?;
}
Expand Down
81 changes: 81 additions & 0 deletions tests/test_comments.rs
Original file line number Diff line number Diff line change
Expand Up @@ -184,3 +184,84 @@ fn envlist() {
cmd.env("QSV_ENVVAR", "");
cmd.env("MIMALLOC_ENVVAR", "");
}

#[test]
fn test_input_skiplines() {
let wrk = Workdir::new("input_skiplines");
wrk.create(
"preamble.csv",
vec![
svec!["# test file to see how skiplines work", ""],
svec!["! this is another comment before the header", ""],
svec!["# DATA DICTIONARY", ""],
svec!["! column1 - alphabetic; id of the column", ""],
svec!["% column2 - numeric; just a number", ""],
svec!["column1", "column2"],
svec!["a", "1"],
svec!["c", "3"],
svec!["e", "5"],
],
);
let mut cmd = wrk.command("input");
cmd.arg("--skip-lines").arg("5").arg("preamble.csv");

let got: Vec<Vec<String>> = wrk.read_stdout(&mut cmd);
let expected = vec![
svec!["column1", "column2"],
svec!["a", "1"],
svec!["c", "3"],
svec!["e", "5"],
];
assert_eq!(got, expected);
}

#[test]
fn test_input_skip_one_line() {
let wrk = Workdir::new("input_skip_one_line");
wrk.create(
"preamble.csv",
vec![
svec!["# test file to see how skiplines work", ""],
svec!["column1", "column2"],
svec!["a", "1"],
svec!["c", "3"],
svec!["e", "5"],
],
);
let mut cmd = wrk.command("input");
cmd.arg("--skip-lines").arg("1").arg("preamble.csv");

let got: Vec<Vec<String>> = wrk.read_stdout(&mut cmd);
let expected = vec![
svec!["column1", "column2"],
svec!["a", "1"],
svec!["c", "3"],
svec!["e", "5"],
];
assert_eq!(got, expected);
}

#[test]
fn test_input_skip_no_line() {
let wrk = Workdir::new("input_skip_no_line");
wrk.create(
"preamble.csv",
vec![
svec!["column1", "column2"],
svec!["a", "1"],
svec!["c", "3"],
svec!["e", "5"],
],
);
let mut cmd = wrk.command("input");
cmd.arg("--skip-lines").arg("0").arg("preamble.csv");

let got: Vec<Vec<String>> = wrk.read_stdout(&mut cmd);
let expected = vec![
svec!["column1", "column2"],
svec!["a", "1"],
svec!["c", "3"],
svec!["e", "5"],
];
assert_eq!(got, expected);
}