Start support for commandline args to nu itself (#851)

* cmdline args wip

* WIP

* redirect working

* Add help and examples

* Only show flags in signature of more than help
This commit is contained in:
JT
2022-01-26 09:42:39 -05:00
committed by GitHub
parent 285f65ba34
commit 8ee619954d
9 changed files with 307 additions and 97 deletions

View File

@ -14,7 +14,7 @@ pub use cp::Cp;
pub use ls::Ls;
pub use mkdir::Mkdir;
pub use mv::Mv;
pub use open::Open;
pub use open::{BufferedReader, Open};
pub use rm::Rm;
pub use save::Save;
pub use touch::Touch;

View File

@ -184,6 +184,12 @@ pub struct BufferedReader<R: Read> {
input: BufReader<R>,
}
impl<R: Read> BufferedReader<R> {
pub fn new(input: BufReader<R>) -> Self {
Self { input }
}
}
impl<R: Read> Iterator for BufferedReader<R> {
type Item = Result<Vec<u8>, ShellError>;

View File

@ -186,6 +186,10 @@ pub fn get_documentation(
long_desc.push('\n');
}
if !sig.named.is_empty() {
long_desc.push_str(&get_flags_section(sig))
}
if !sig.required_positional.is_empty()
|| !sig.optional_positional.is_empty()
|| sig.rest_positional.is_some()
@ -205,13 +209,11 @@ pub fn get_documentation(
long_desc.push_str(&format!(" ...args: {}\n", rest_positional.desc));
}
}
if !sig.named.is_empty() {
long_desc.push_str(&get_flags_section(sig))
}
if !examples.is_empty() {
long_desc.push_str("\nExamples:");
}
for example in examples {
long_desc.push('\n');
long_desc.push_str(" ");
@ -222,7 +224,7 @@ pub fn get_documentation(
} else if let Some(highlighter) = engine_state.find_decl(b"nu-highlight") {
let decl = engine_state.get_decl(highlighter);
if let Ok(output) = decl.run(
match decl.run(
engine_state,
stack,
&Call::new(),
@ -232,16 +234,23 @@ pub fn get_documentation(
}
.into_pipeline_data(),
) {
let result = output.into_value(Span { start: 0, end: 0 });
match result.as_string() {
Ok(s) => {
long_desc.push_str(&format!("\n > {}\n", s));
}
_ => {
long_desc.push_str(&format!("\n > {}\n", example.example));
Ok(output) => {
let result = output.into_value(Span { start: 0, end: 0 });
match result.as_string() {
Ok(s) => {
long_desc.push_str(&format!("\n > {}\n", s));
}
_ => {
long_desc.push_str(&format!("\n > {}\n", example.example));
}
}
}
Err(_) => {
long_desc.push_str(&format!("\n > {}\n", example.example));
}
}
} else {
long_desc.push_str(&format!("\n > {}\n", example.example));
}
}

View File

@ -12,9 +12,7 @@ pub use flatten::{
};
pub use lex::{lex, Token, TokenContents};
pub use lite_parse::{lite_parse, LiteBlock};
pub use parse_keywords::{
parse_alias, parse_def, parse_def_predecl, parse_let, parse_module, parse_use,
};
pub use parser::{find_captures_in_expr, parse, trim_quotes, Import};
#[cfg(feature = "plugin")]

View File

@ -1,5 +1,6 @@
use crate::Value;
#[derive(Debug)]
pub struct Example {
pub example: &'static str,
pub description: &'static str,

View File

@ -279,6 +279,14 @@ impl Signature {
one_liner.push_str(&self.name);
one_liner.push(' ');
// Note: the call signature needs flags first because on the nu commandline,
// flags will precede the script file name. Flags for internal commands can come
// either before or after (or around) positional parameters, so there isn't a strong
// preference, so we default to the more constrained example.
if self.named.len() > 1 {
one_liner.push_str("{flags} ");
}
for positional in &self.required_positional {
one_liner.push_str(&get_positional_short_name(positional, true));
}
@ -286,18 +294,14 @@ impl Signature {
one_liner.push_str(&get_positional_short_name(positional, false));
}
if self.rest_positional.is_some() {
one_liner.push_str("...args ");
if let Some(rest) = &self.rest_positional {
one_liner.push_str(&format!("...{}", get_positional_short_name(rest, false)));
}
// if !self.subcommands.is_empty() {
// one_liner.push_str("<subcommand> ");
// }
if !self.named.is_empty() {
one_liner.push_str("{flags} ");
}
one_liner
}