mirror of
https://github.com/sagiegurari/duckscript
synced 2024-09-29 20:54:34 +00:00
new man command #16
This commit is contained in:
parent
6bb1207b6a
commit
897da405dc
|
@ -2,6 +2,7 @@
|
|||
|
||||
### v0.1.2
|
||||
|
||||
* New **man** command #16
|
||||
* New **calc** command #10
|
||||
* New **unset_env** command #23
|
||||
* New **mv** command #8
|
||||
|
|
27
docs/sdk.md
27
docs/sdk.md
|
@ -10,6 +10,7 @@
|
|||
* [sdk::Not](#sdk__Not)
|
||||
* [sdk::Release](#sdk__Release)
|
||||
* [sdk::Set](#sdk__Set)
|
||||
* [sdk::ShowCommandDocumentation](#sdk__ShowCommandDocumentation)
|
||||
* [sdk::Unalias](#sdk__Unalias)
|
||||
* [sdk::env::GetVar](#sdk__env__GetVar)
|
||||
* [sdk::env::PrintCurrentDirectory](#sdk__env__PrintCurrentDirectory)
|
||||
|
@ -541,6 +542,32 @@ var = set "home: ${HOME}"
|
|||
#### Aliases:
|
||||
set
|
||||
|
||||
<a name="sdk__ShowCommandDocumentation"></a>
|
||||
## sdk::ShowCommandDocumentation
|
||||
```sh
|
||||
var = man command
|
||||
```
|
||||
|
||||
Prints and returns the help documentation of the provided command.
|
||||
|
||||
#### Parameters
|
||||
|
||||
The command name.
|
||||
|
||||
#### Return Value
|
||||
|
||||
The help documentation or if not found, none.
|
||||
|
||||
#### Examples
|
||||
|
||||
```sh
|
||||
man set
|
||||
```
|
||||
|
||||
|
||||
#### Aliases:
|
||||
man
|
||||
|
||||
<a name="sdk__Unalias"></a>
|
||||
## sdk::Unalias
|
||||
```sh
|
||||
|
|
19
duckscript_sdk/src/sdk/std/man/help.md
Normal file
19
duckscript_sdk/src/sdk/std/man/help.md
Normal file
|
@ -0,0 +1,19 @@
|
|||
```sh
|
||||
var = man command
|
||||
```
|
||||
|
||||
Prints and returns the help documentation of the provided command.
|
||||
|
||||
#### Parameters
|
||||
|
||||
The command name.
|
||||
|
||||
#### Return Value
|
||||
|
||||
The help documentation or if not found, none.
|
||||
|
||||
#### Examples
|
||||
|
||||
```sh
|
||||
man set
|
||||
```
|
78
duckscript_sdk/src/sdk/std/man/mod.rs
Executable file
78
duckscript_sdk/src/sdk/std/man/mod.rs
Executable file
|
@ -0,0 +1,78 @@
|
|||
use crate::utils::pckg;
|
||||
use duckscript::types::command::{Command, CommandResult, Commands};
|
||||
use duckscript::types::instruction::Instruction;
|
||||
use duckscript::types::runtime::StateValue;
|
||||
use std::collections::HashMap;
|
||||
|
||||
#[cfg(test)]
|
||||
#[path = "./mod_test.rs"]
|
||||
mod mod_test;
|
||||
|
||||
fn print_help(help_doc: String, name: &str) -> CommandResult {
|
||||
if help_doc.is_empty() {
|
||||
println!("No documentation found for command: {}", name);
|
||||
CommandResult::Continue(None)
|
||||
} else {
|
||||
println!("{}", &help_doc);
|
||||
CommandResult::Continue(Some(help_doc))
|
||||
}
|
||||
}
|
||||
|
||||
struct CommandImpl {
|
||||
package: String,
|
||||
}
|
||||
|
||||
impl Command for CommandImpl {
|
||||
fn name(&self) -> String {
|
||||
pckg::concat(&self.package, "ShowCommandDocumentation")
|
||||
}
|
||||
|
||||
fn aliases(&self) -> Vec<String> {
|
||||
vec!["man".to_string()]
|
||||
}
|
||||
|
||||
fn help(&self) -> String {
|
||||
include_str!("help.md").to_string()
|
||||
}
|
||||
|
||||
fn requires_context(&self) -> bool {
|
||||
true
|
||||
}
|
||||
|
||||
fn run_with_context(
|
||||
&self,
|
||||
arguments: Vec<String>,
|
||||
_state: &mut HashMap<String, StateValue>,
|
||||
_variables: &mut HashMap<String, String>,
|
||||
_output_variable: Option<String>,
|
||||
_instructions: &Vec<Instruction>,
|
||||
commands: &mut Commands,
|
||||
_test_setline: usize,
|
||||
) -> CommandResult {
|
||||
if arguments.is_empty() {
|
||||
print_help(self.help(), &self.name())
|
||||
} else {
|
||||
let name = &arguments[0];
|
||||
|
||||
match commands.get(name) {
|
||||
Some(command) => {
|
||||
let help_doc = command.help();
|
||||
print_help(help_doc, name)
|
||||
}
|
||||
None => {
|
||||
if name == &self.name() || self.aliases().contains(name) {
|
||||
print_help(self.help(), &self.name())
|
||||
} else {
|
||||
CommandResult::Error(format!("Command: {} not found.", name).to_string())
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
pub(crate) fn create(package: &str) -> Box<dyn Command> {
|
||||
Box::new(CommandImpl {
|
||||
package: package.to_string(),
|
||||
})
|
||||
}
|
50
duckscript_sdk/src/sdk/std/man/mod_test.rs
Normal file
50
duckscript_sdk/src/sdk/std/man/mod_test.rs
Normal file
|
@ -0,0 +1,50 @@
|
|||
use super::*;
|
||||
use crate::sdk::std::alias;
|
||||
use crate::test;
|
||||
use crate::test::{CommandValidation, SetCommand};
|
||||
|
||||
#[test]
|
||||
fn common_functions() {
|
||||
test::test_common_command_functions(create(""));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn run_no_args() {
|
||||
test::run_script_and_validate(
|
||||
vec![create("")],
|
||||
"out = man",
|
||||
CommandValidation::Contains("out".to_string(), " = man".to_string()),
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn run_self_command_found() {
|
||||
test::run_script_and_validate(
|
||||
vec![create("")],
|
||||
"out = man man",
|
||||
CommandValidation::Contains("out".to_string(), " = man".to_string()),
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn run_command_not_found() {
|
||||
test::run_script_and_fail(vec![create("")], "out = man badcommand");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn run_command_found_with_docs() {
|
||||
test::run_script_and_validate(
|
||||
vec![create(""), alias::create("")],
|
||||
"out = man alias",
|
||||
CommandValidation::Contains("out".to_string(), "alias ".to_string()),
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn run_command_found_no_docs() {
|
||||
test::run_script_and_validate(
|
||||
vec![create(""), Box::new(SetCommand {})],
|
||||
"out = man test_set",
|
||||
CommandValidation::None,
|
||||
);
|
||||
}
|
|
@ -8,6 +8,7 @@ mod fs;
|
|||
mod function;
|
||||
mod goto;
|
||||
mod ifelse;
|
||||
mod man;
|
||||
mod math;
|
||||
mod not;
|
||||
mod process;
|
||||
|
@ -28,6 +29,7 @@ pub(crate) fn load(commands: &mut Commands) -> Result<(), ScriptError> {
|
|||
commands.set(echo::create(PACKAGE))?;
|
||||
commands.set(eval::create(PACKAGE))?;
|
||||
commands.set(goto::create(PACKAGE))?;
|
||||
commands.set(man::create(PACKAGE))?;
|
||||
commands.set(not::create(PACKAGE))?;
|
||||
commands.set(release::create(PACKAGE))?;
|
||||
commands.set(set::create(PACKAGE))?;
|
||||
|
|
|
@ -25,6 +25,10 @@ impl Command for SetCommand {
|
|||
"test_set".to_string()
|
||||
}
|
||||
|
||||
fn help(&self) -> String {
|
||||
"".to_string()
|
||||
}
|
||||
|
||||
fn run(&self, arguments: Vec<String>) -> CommandResult {
|
||||
if arguments.is_empty() {
|
||||
CommandResult::Continue(None)
|
||||
|
|
2
examples/man.ds
Normal file
2
examples/man.ds
Normal file
|
@ -0,0 +1,2 @@
|
|||
|
||||
man set
|
Loading…
Reference in a new issue