Skip to content

Commit

Permalink
Merge pull request #5 from Kerollmops/rename-cmd
Browse files Browse the repository at this point in the history
Add a new rename command to change the headers
  • Loading branch information
jqnatividad authored Dec 27, 2020
2 parents 31a13ac + 0df7b7d commit bd6d3a0
Show file tree
Hide file tree
Showing 3 changed files with 69 additions and 1 deletion.
1 change: 1 addition & 0 deletions src/cmd/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ pub mod index;
pub mod input;
pub mod join;
pub mod partition;
pub mod rename;
pub mod reverse;
pub mod sample;
pub mod search;
Expand Down
65 changes: 65 additions & 0 deletions src/cmd/rename.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
use csv;

use CliResult;
use config::{Config, Delimiter};
use util;

static USAGE: &'static str = "
Rename the columns of CSV data efficiently.
This command lets you rename the columns in CSV data. You must specify
all of the headers, and separate them by a comma.
Change the name of the columns:
$ xsv rename id,name,title
Use column names that contains commas and conflict with the separator:
$ xsv rename '\"Date - Opening\",\"Date - Actual Closing\"'
Usage:
xsv rename [options] [--] <headers> [<input>]
xsv rename --help
Common options:
-h, --help Display this message
-o, --output <file> Write output to <file> instead of stdout.
-d, --delimiter <arg> The field delimiter for reading CSV data.
Must be a single character. (default: ,)
";

#[derive(Deserialize)]
struct Args {
arg_input: Option<String>,
arg_headers: String,
flag_output: Option<String>,
flag_delimiter: Option<Delimiter>,
}

pub fn run(argv: &[&str]) -> CliResult<()> {
let args: Args = util::get_args(USAGE, argv)?;

let rconfig = Config::new(&args.arg_input)
.delimiter(args.flag_delimiter);

let mut rdr = rconfig.reader()?;
let mut wtr = Config::new(&args.flag_output).writer()?;
let headers = rdr.byte_headers()?;

let mut new_rdr = csv::Reader::from_reader(args.arg_headers.as_bytes());
let new_headers = new_rdr.byte_headers()?;

if headers.len() != new_headers.len() {
return fail!("The length of the CSV headers is different from the provided one.");
}

if !rconfig.no_headers {
wtr.write_record(new_headers)?;
}

let mut record = csv::ByteRecord::new();
while rdr.read_byte_record(&mut record)? {
wtr.write_record(&record)?;
}
wtr.flush()?;
Ok(())
}
4 changes: 3 additions & 1 deletion src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -155,6 +155,7 @@ enum Command {
Input,
Join,
Partition,
Rename,
Reverse,
Sample,
Search,
Expand All @@ -175,7 +176,7 @@ impl Command {

if !argv[1].chars().all(char::is_lowercase) {
return Err(CliError::Other(format!(
"xsv expects commands in lowercase. Did you mean '{}'?",
"xsv expects commands in lowercase. Did you mean '{}'?",
argv[1].to_lowercase()).to_string()));
}
match self {
Expand All @@ -192,6 +193,7 @@ impl Command {
Command::Input => cmd::input::run(argv),
Command::Join => cmd::join::run(argv),
Command::Partition => cmd::partition::run(argv),
Command::Rename => cmd::rename::run(argv),
Command::Reverse => cmd::reverse::run(argv),
Command::Sample => cmd::sample::run(argv),
Command::Search => cmd::search::run(argv),
Expand Down

0 comments on commit bd6d3a0

Please sign in to comment.