mirror of
https://github.com/nushell/nushell.git
synced 2024-11-23 08:53:29 +01:00
add --default
flag to input command (#14374)
# Description Closes: #14248 # User-Facing Changes Added a `--default` flag to input command, and it also added an extra output to prompt: ``` > let x = input -d 18 "input your age" input your age (default: 18) > $x 18 > let x = input -d 18 > $x 18 ``` # Tests + Formatting I don't think it's easy to add a test for it :-(
This commit is contained in:
parent
13ce9e4f64
commit
6773dfce8d
@ -43,6 +43,12 @@ impl Command for Input {
|
||||
"number of characters to read; suppresses output",
|
||||
Some('n'),
|
||||
)
|
||||
.named(
|
||||
"default",
|
||||
SyntaxShape::String,
|
||||
"default value if no input is provided",
|
||||
Some('d'),
|
||||
)
|
||||
.switch("suppress-output", "don't print keystroke values", Some('s'))
|
||||
.category(Category::Platform)
|
||||
}
|
||||
@ -72,8 +78,12 @@ impl Command for Input {
|
||||
});
|
||||
}
|
||||
|
||||
let default_val: Option<String> = call.get_flag(engine_state, stack, "default")?;
|
||||
if let Some(prompt) = &prompt {
|
||||
print!("{prompt}");
|
||||
match &default_val {
|
||||
None => print!("{prompt}"),
|
||||
Some(val) => print!("{prompt} (default: {val})"),
|
||||
}
|
||||
let _ = std::io::stdout().flush();
|
||||
}
|
||||
|
||||
@ -149,7 +159,10 @@ impl Command for Input {
|
||||
if !suppress_output {
|
||||
std::io::stdout().write_all(b"\n")?;
|
||||
}
|
||||
Ok(Value::string(buf, call.head).into_pipeline_data())
|
||||
match default_val {
|
||||
Some(val) if buf.is_empty() => Ok(Value::string(val, call.head).into_pipeline_data()),
|
||||
_ => Ok(Value::string(buf, call.head).into_pipeline_data()),
|
||||
}
|
||||
}
|
||||
|
||||
fn examples(&self) -> Vec<Example> {
|
||||
@ -164,6 +177,11 @@ impl Command for Input {
|
||||
example: "let user_input = (input --numchar 2)",
|
||||
result: None,
|
||||
},
|
||||
Example {
|
||||
description: "Get input from the user with default value, and assign to a variable",
|
||||
example: "let user_input = (input --default 10)",
|
||||
result: None,
|
||||
},
|
||||
]
|
||||
}
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user