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

split: add <outdir> error handling and add usage text examples #1585

Merged
merged 1 commit into from
Feb 3, 2024
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
23 changes: 22 additions & 1 deletion src/cmd/split.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,27 @@ Splits the given CSV data into chunks.
The files are written to the directory given with the name '{start}.csv',
where {start} is the index of the first record of the chunk (starting at 0).
For examples, see https://github.com/jqnatividad/qsv/blob/master/tests/test_split.rs.
Examples:
qsv split outdir --size 100 --filename chunk_{}.csv input.csv
qsv split outdir -s 100 --filename chunk_{}.csv --pad 5 input.csv
qsv split . -s 100 input.csv
cat in.csv | qsv split outdir -s 1000
For more examples, see https://github.com/jqnatividad/qsv/blob/master/tests/test_split.rs.
Usage:
qsv split [options] <outdir> [<input>]
qsv split --help
split arguments:
<outdir> The directory where the output files will be written.
If it does not exist, it will be created.
<input> The CSV file to read. If not given, input is read from
STDIN.
split options:
-s, --size <arg> The number of records to write into each chunk.
[default: 500]
Expand Down Expand Up @@ -66,6 +81,12 @@ pub fn run(argv: &[&str]) -> CliResult<()> {
if args.flag_size == 0 {
return fail_incorrectusage_clierror!("--size must be greater than 0.");
}

// check if outdir is set correctly
if Path::new(&args.arg_outdir).is_file() && args.arg_input.is_none() {
return fail_incorrectusage_clierror!("<outdir> is not specified or is a file.");
}

fs::create_dir_all(&args.arg_outdir)?;

match args.rconfig().indexed()? {
Expand Down
15 changes: 15 additions & 0 deletions tests/test_split.rs
Original file line number Diff line number Diff line change
Expand Up @@ -502,3 +502,18 @@ fn split_custom_filename_padded() {
assert!(wrk.path("prefix-002.csv").exists());
assert!(wrk.path("prefix-004.csv").exists());
}

#[test]
fn split_nooutdir() {
let wrk = Workdir::new("split_nooutdir");
wrk.create("in.csv", data(true));

let mut cmd = wrk.command("split");
cmd.args(["--size", "2"]).arg("in.csv");
wrk.run(&mut cmd);

wrk.assert_err(&mut cmd);
let got = wrk.output_stderr(&mut cmd);
let expected = "usage error: <outdir> is not specified or is a file.\n";
assert_eq!(got, expected);
}