diff --git a/src/cmd/split.rs b/src/cmd/split.rs index 11df30ea7..d945aa644 100644 --- a/src/cmd/split.rs +++ b/src/cmd/split.rs @@ -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] [] qsv split --help +split arguments: + The directory where the output files will be written. + If it does not exist, it will be created. + The CSV file to read. If not given, input is read from + STDIN. + split options: -s, --size The number of records to write into each chunk. [default: 500] @@ -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!(" is not specified or is a file."); + } + fs::create_dir_all(&args.arg_outdir)?; match args.rconfig().indexed()? { diff --git a/tests/test_split.rs b/tests/test_split.rs index d8340279d..deaf73e46 100644 --- a/tests/test_split.rs +++ b/tests/test_split.rs @@ -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: is not specified or is a file.\n"; + assert_eq!(got, expected); +}