mirror of
https://github.com/nushell/nushell
synced 2024-11-15 01:17:07 +00:00
Add -c
flag to select
command (#4062)
See cc3653cfd9
for more on the `-c` flag.
Co-authored-by: Andrés N. Robalino <andres@androbtech.com>
Co-authored-by: Andrés N. Robalino <andres@androbtech.com>
This commit is contained in:
parent
69708f7244
commit
574c5961c8
1 changed files with 55 additions and 7 deletions
|
@ -15,7 +15,14 @@ impl WholeStreamCommand for Command {
|
|||
}
|
||||
|
||||
fn signature(&self) -> Signature {
|
||||
Signature::build("select").rest(
|
||||
Signature::build("select")
|
||||
.named(
|
||||
"columns",
|
||||
SyntaxShape::Table,
|
||||
"Optionally operate by column path",
|
||||
Some('c'),
|
||||
)
|
||||
.rest(
|
||||
"rest",
|
||||
SyntaxShape::ColumnPath,
|
||||
"the columns to select from the table",
|
||||
|
@ -27,10 +34,10 @@ impl WholeStreamCommand for Command {
|
|||
}
|
||||
|
||||
fn run(&self, args: CommandArgs) -> Result<OutputStream, ShellError> {
|
||||
let columns: Vec<ColumnPath> = args.rest(0)?;
|
||||
let mut columns = args.rest(0)?;
|
||||
columns.extend(column_paths_from_args(&args)?);
|
||||
let input = args.input;
|
||||
let name = args.call_info.name_tag;
|
||||
|
||||
select(name, columns, input)
|
||||
}
|
||||
|
||||
|
@ -46,10 +53,51 @@ impl WholeStreamCommand for Command {
|
|||
example: "ls | select name size",
|
||||
result: None,
|
||||
},
|
||||
Example {
|
||||
description: "Select columns dynamically",
|
||||
example: "[[a b]; [1 2]] | select -c [a]",
|
||||
result: Some(vec![UntaggedValue::row(indexmap! {
|
||||
"a".to_string() => UntaggedValue::int(1).into(),
|
||||
})
|
||||
.into()]),
|
||||
},
|
||||
]
|
||||
}
|
||||
}
|
||||
|
||||
fn column_paths_from_args(args: &CommandArgs) -> Result<Vec<ColumnPath>, ShellError> {
|
||||
let column_paths: Option<Vec<Value>> = args.get_flag("columns")?;
|
||||
let has_columns = column_paths.is_some();
|
||||
let column_paths = match column_paths {
|
||||
Some(cols) => {
|
||||
let mut c = Vec::new();
|
||||
for col in cols {
|
||||
let colpath = ColumnPath::build(&col.convert_to_string().spanned_unknown());
|
||||
if !colpath.is_empty() {
|
||||
c.push(colpath)
|
||||
}
|
||||
}
|
||||
c
|
||||
}
|
||||
None => Vec::new(),
|
||||
};
|
||||
|
||||
if has_columns && column_paths.is_empty() {
|
||||
let colval: Option<Value> = args.get_flag("columns")?;
|
||||
let colspan = match colval {
|
||||
Some(v) => v.tag.span,
|
||||
None => Span::unknown(),
|
||||
};
|
||||
return Err(ShellError::labeled_error(
|
||||
"Requires a list of columns",
|
||||
"must be a list of columns",
|
||||
colspan,
|
||||
));
|
||||
}
|
||||
|
||||
Ok(column_paths)
|
||||
}
|
||||
|
||||
fn select(
|
||||
name: Tag,
|
||||
columns: Vec<ColumnPath>,
|
||||
|
|
Loading…
Reference in a new issue