Split split command to sub commands.

This commit is contained in:
Andrés N. Robalino
2020-05-24 01:41:30 -05:00
parent 74c2daf665
commit edbecda14d
22 changed files with 154 additions and 57 deletions

View File

@ -33,7 +33,7 @@ impl WholeStreamCommand for Headers {
fn examples(&self) -> Vec<Example> {
vec![Example {
description: "Create headers for a raw string",
example: r#"echo "a b c|1 2 3" | split-row "|" | split-column " " | headers"#,
example: r#"echo "a b c|1 2 3" | split row "|" | split column " " | headers"#,
result: None,
}]
}

View File

@ -15,15 +15,15 @@ struct SplitColumnArgs {
collapse_empty: bool,
}
pub struct SplitColumn;
pub struct SubCommand;
impl WholeStreamCommand for SplitColumn {
impl WholeStreamCommand for SubCommand {
fn name(&self) -> &str {
"split-column"
"split column"
}
fn signature(&self) -> Signature {
Signature::build("split-column")
Signature::build("split column")
.required(
"separator",
SyntaxShape::Any,
@ -34,7 +34,7 @@ impl WholeStreamCommand for SplitColumn {
}
fn usage(&self) -> &str {
"Split row contents across multiple columns via the separator."
"splits contents across multiple columns via the separator."
}
fn run(
@ -106,12 +106,12 @@ fn split_column(args: CommandArgs, registry: &CommandRegistry) -> Result<OutputS
#[cfg(test)]
mod tests {
use super::SplitColumn;
use super::SubCommand;
#[test]
fn examples_work_as_expected() {
use crate::examples::test as test_examples;
test_examples(SplitColumn {})
test_examples(SubCommand {})
}
}

View File

@ -0,0 +1,49 @@
use crate::commands::WholeStreamCommand;
use crate::prelude::*;
use nu_errors::ShellError;
use nu_protocol::{ReturnSuccess, Signature, UntaggedValue};
#[derive(Clone)]
pub struct Command;
impl WholeStreamCommand for Command {
fn name(&self) -> &str {
"split"
}
fn signature(&self) -> Signature {
Signature::build("split")
}
fn usage(&self) -> &str {
"split contents across desired subcommand (like row, column) via the separator."
}
fn run(
&self,
_args: CommandArgs,
registry: &CommandRegistry,
) -> Result<OutputStream, ShellError> {
let registry = registry.clone();
let stream = async_stream! {
yield Ok(ReturnSuccess::Value(
UntaggedValue::string(crate::commands::help::get_help(&Command, &registry))
.into_value(Tag::unknown()),
));
};
Ok(stream.to_output_stream())
}
}
#[cfg(test)]
mod tests {
use super::Command;
#[test]
fn examples_work_as_expected() {
use crate::examples::test as test_examples;
test_examples(Command {})
}
}

View File

@ -0,0 +1,7 @@
pub mod column;
pub mod command;
pub mod row;
pub use column::SubCommand as SplitColumn;
pub use command::Command as Split;
pub use row::SubCommand as SplitRow;

View File

@ -10,15 +10,15 @@ struct SplitRowArgs {
separator: Tagged<String>,
}
pub struct SplitRow;
pub struct SubCommand;
impl WholeStreamCommand for SplitRow {
impl WholeStreamCommand for SubCommand {
fn name(&self) -> &str {
"split-row"
"split row"
}
fn signature(&self) -> Signature {
Signature::build("split-row").required(
Signature::build("split row").required(
"separator",
SyntaxShape::Any,
"the character that denotes what separates rows",
@ -26,7 +26,7 @@ impl WholeStreamCommand for SplitRow {
}
fn usage(&self) -> &str {
"Split row contents over multiple rows via the separator."
"splits contents over multiple rows via the separator."
}
fn run(
@ -73,12 +73,12 @@ fn split_row(args: CommandArgs, registry: &CommandRegistry) -> Result<OutputStre
#[cfg(test)]
mod tests {
use super::SplitRow;
use super::SubCommand;
#[test]
fn examples_work_as_expected() {
use crate::examples::test as test_examples;
test_examples(SplitRow {})
test_examples(SubCommand {})
}
}