Add missing flags to existing commands (#565)

* Add missing flags to existing commands

* fmt
This commit is contained in:
JT
2021-12-24 08:41:29 +11:00
committed by GitHub
parent 29c8b826d4
commit b719f8d4eb
6 changed files with 462 additions and 88 deletions

View File

@ -23,6 +23,11 @@ impl Command for Do {
SyntaxShape::Block(Some(vec![])),
"the block to run",
)
.switch(
"ignore-errors",
"ignore errors as the block runs",
Some('i'),
)
.rest("rest", SyntaxShape::Any, "the parameter(s) for the block")
.category(Category::Core)
}
@ -37,6 +42,8 @@ impl Command for Do {
let block: Value = call.req(engine_state, stack, 0)?;
let block_id = block.as_block()?;
let ignore_errors = call.has_flag("ignore-errors");
let rest: Vec<Value> = call.rest(engine_state, stack, 1)?;
let block = engine_state.get_block(block_id);
@ -81,6 +88,15 @@ impl Command for Do {
)
}
}
eval_block(engine_state, &mut stack, block, input)
let result = eval_block(engine_state, &mut stack, block, input);
if ignore_errors {
match result {
Ok(x) => Ok(x),
Err(_) => Ok(PipelineData::new(call.head)),
}
} else {
result
}
}
}

View File

@ -35,6 +35,11 @@ impl Command for For {
SyntaxShape::Block(Some(vec![])),
"the block to run",
)
.switch(
"numbered",
"returned a numbered item ($it.index and $it.item)",
Some('n'),
)
.creates_scope()
.category(Category::Core)
}
@ -60,6 +65,8 @@ impl Command for For {
.as_block()
.expect("internal error: expected block");
let numbered = call.has_flag("numbered");
let ctrlc = engine_state.ctrlc.clone();
let engine_state = engine_state.clone();
let block = engine_state.get_block(block_id).clone();
@ -68,8 +75,26 @@ impl Command for For {
match values {
Value::List { vals, .. } => Ok(vals
.into_iter()
.map(move |x| {
stack.add_var(var_id, x);
.enumerate()
.map(move |(idx, x)| {
stack.add_var(
var_id,
if numbered {
Value::Record {
cols: vec!["index".into(), "item".into()],
vals: vec![
Value::Int {
val: idx as i64,
span: head,
},
x,
],
span: head,
}
} else {
x
},
);
//let block = engine_state.get_block(block_id);
match eval_block(&engine_state, &mut stack, &block, PipelineData::new(head)) {
@ -80,8 +105,26 @@ impl Command for For {
.into_pipeline_data(ctrlc)),
Value::Range { val, .. } => Ok(val
.into_range_iter()?
.map(move |x| {
stack.add_var(var_id, x);
.enumerate()
.map(move |(idx, x)| {
stack.add_var(
var_id,
if numbered {
Value::Record {
cols: vec!["index".into(), "item".into()],
vals: vec![
Value::Int {
val: idx as i64,
span: head,
},
x,
],
span: head,
}
} else {
x
},
);
//let block = engine_state.get_block(block_id);
match eval_block(&engine_state, &mut stack, &block, PipelineData::new(head)) {