Adds flags and optional arguments to view-source (#5446)

* added flags and optional arguments to view-source

* removed redundant code

* removed redundant code

* fmt
This commit is contained in:
pwygab 2022-05-05 19:37:56 +08:00 committed by GitHub
parent 02a3430ef0
commit 0b9c0fea9d
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23

View file

@ -52,6 +52,8 @@ impl Command for ViewSource {
let decl = engine_state.get_decl(decl_id);
let sig = decl.signature();
let vec_of_required = &sig.required_positional;
let vec_of_optional = &sig.optional_positional;
let vec_of_flags = &sig.named;
// gets vector of positionals.
if let Some(block_id) = decl.get_block_id() {
let block = engine_state.get_block(block_id);
@ -68,6 +70,30 @@ impl Command for ViewSource {
final_contents.push_str(&n.shape.to_string());
final_contents.push(' ');
}
for n in vec_of_optional {
final_contents.push_str(&n.name);
// name of positional arg
final_contents.push_str("?:");
final_contents.push_str(&n.shape.to_string());
final_contents.push(' ');
}
for n in vec_of_flags {
final_contents.push_str("--");
final_contents.push_str(&n.long);
final_contents.push(' ');
if n.short.is_some() {
final_contents.push_str("(-");
final_contents.push(n.short.expect("this cannot trigger."));
final_contents.push(')');
}
if n.arg.is_some() {
final_contents.push_str(": ");
final_contents.push_str(
&n.arg.as_ref().expect("this cannot trigger.").to_string(),
);
}
final_contents.push(' ');
}
final_contents.push_str("] ");
final_contents.push_str(&String::from_utf8_lossy(contents));
Ok(Value::string(final_contents, call.head).into_pipeline_data())