New read command #33

This commit is contained in:
sagie gur ari 2020-01-05 17:46:17 +00:00
parent 8a8a30ed5c
commit a6f8a0b3a9
10 changed files with 174 additions and 31 deletions

View file

@ -2,6 +2,7 @@
### v0.1.5
* New read command #33
* New hostname command #18
* New trim_start command #29
* New trim_end command #30

View file

@ -274,11 +274,34 @@ run_flow
```
This example demonstrates how functions as a concept do not need to be part of the language and can be implemented by anyone as a command.<br>
This also means that other developers can replace the function command with their implementation to provide additional/different functionality.
This also means that other developers can replace the function command with their implementation to provide additional/different functionality.<br>
Below an example of loops using the [for/in command](https://github.com/sagiegurari/duckscript/blob/master/docs/sdk.md#sdk__ForIn):
```sh
values = range 1 10
for i in values
for j in values
echo i: ${i} j: ${j}
end_for
end_for
release values
```
Below an example of [if/else command](https://github.com/sagiegurari/duckscript/blob/master/docs/sdk.md#sdk__If):
```sh
echo Enter Full Name:
name = read
if is_empty ${name}
echo You didn't enter any value
else
echo Your name is: ${name}
end_if
value = set false
if ${value}
echo should not be here
@ -303,20 +326,6 @@ else
end_if
```
Below an example of loops using the [for/in command](https://github.com/sagiegurari/duckscript/blob/master/docs/sdk.md#sdk__ForIn):
```sh
values = range 1 10
for i in values
for j in values
echo i: ${i} j: ${j}
end_for
end_for
release values
```
<a name="tutorial-standard-api-full-sdk-docs"></a>
#### Full SDK Docs
The full SDK docs can be found [here](https://github.com/sagiegurari/duckscript/blob/master/docs/sdk.md)

View file

@ -231,11 +231,34 @@ run_flow
```
This example demonstrates how functions as a concept do not need to be part of the language and can be implemented by anyone as a command.<br>
This also means that other developers can replace the function command with their implementation to provide additional/different functionality.
This also means that other developers can replace the function command with their implementation to provide additional/different functionality.<br>
Below an example of loops using the [for/in command](https://github.com/sagiegurari/duckscript/blob/master/docs/sdk.md#sdk__ForIn):
```sh
values = range 1 10
for i in values
for j in values
echo i: ${i} j: ${j}
end_for
end_for
release values
```
Below an example of [if/else command](https://github.com/sagiegurari/duckscript/blob/master/docs/sdk.md#sdk__If):
```sh
echo Enter Full Name:
name = read
if is_empty ${name}
echo You didn't enter any value
else
echo Your name is: ${name}
end_if
value = set false
if ${value}
echo should not be here
@ -260,20 +283,6 @@ else
end_if
```
Below an example of loops using the [for/in command](https://github.com/sagiegurari/duckscript/blob/master/docs/sdk.md#sdk__ForIn):
```sh
values = range 1 10
for i in values
for j in values
echo i: ${i} j: ${j}
end_for
end_for
release values
```
<a name="tutorial-standard-api-full-sdk-docs"></a>
#### Full SDK Docs
The full SDK docs can be found [here](https://github.com/sagiegurari/duckscript/blob/master/docs/sdk.md)

View file

@ -8,6 +8,7 @@
* [sdk::If (if)](#sdk__If)
* [sdk::IsDefined (is_defined)](#sdk__IsDefined)
* [sdk::Not (not)](#sdk__Not)
* [sdk::ReadUserInput (read)](#sdk__ReadUserInput)
* [sdk::Release (release)](#sdk__Release)
* [sdk::Set (set)](#sdk__Set)
* [sdk::ShowCommandDocumentation (man)](#sdk__ShowCommandDocumentation)
@ -491,6 +492,40 @@ echo is true: ${is_true}
#### Aliases:
not
<a name="sdk__ReadUserInput"></a>
## sdk::ReadUserInput
```sh
var = read
```
Reads the user input into the output variable.<br>
If the user didn't insert any input, none will be returned.
#### Parameters
None
#### Return Value
The user input or none if no input was entered.
#### Examples
```sh
echo Enter Full Name:
name = read
if is_empty ${name}
echo You didn't enter any value
else
echo Your name is: ${name}
end_if
```
#### Aliases:
read
<a name="sdk__Release"></a>
## sdk::Release
```sh

View file

@ -14,6 +14,7 @@ mod math;
mod net;
mod not;
mod process;
mod read;
mod release;
mod set;
mod string;
@ -34,6 +35,7 @@ pub(crate) fn load(commands: &mut Commands) -> Result<(), ScriptError> {
commands.set(is_defined::create(PACKAGE))?;
commands.set(man::create(PACKAGE))?;
commands.set(not::create(PACKAGE))?;
commands.set(read::create(PACKAGE))?;
commands.set(release::create(PACKAGE))?;
commands.set(set::create(PACKAGE))?;
commands.set(unalias::create(PACKAGE))?;

View file

@ -0,0 +1,27 @@
```sh
var = read
```
Reads the user input into the output variable.<br>
If the user didn't insert any input, none will be returned.
#### Parameters
None
#### Return Value
The user input or none if no input was entered.
#### Examples
```sh
echo Enter Full Name:
name = read
if is_empty ${name}
echo You didn't enter any value
else
echo Your name is: ${name}
end_if
```

View file

@ -0,0 +1,42 @@
use crate::utils::pckg;
use duckscript::types::command::{Command, CommandResult};
use std::io::stdin;
#[cfg(test)]
#[path = "./mod_test.rs"]
mod mod_test;
struct CommandImpl {
package: String,
}
impl Command for CommandImpl {
fn name(&self) -> String {
pckg::concat(&self.package, "ReadUserInput")
}
fn aliases(&self) -> Vec<String> {
vec!["read".to_string()]
}
fn help(&self) -> String {
include_str!("help.md").to_string()
}
fn run(&self, _arguments: Vec<String>) -> CommandResult {
let mut text = String::new();
let value = match stdin().read_line(&mut text) {
Ok(_) => Some(text.trim().to_string()),
Err(_) => None,
};
CommandResult::Continue(value)
}
}
pub(crate) fn create(package: &str) -> Box<dyn Command> {
Box::new(CommandImpl {
package: package.to_string(),
})
}

View file

@ -0,0 +1,7 @@
use super::*;
use crate::test;
#[test]
fn common_functions() {
test::test_common_command_functions(create(""));
}

View file

@ -29,7 +29,9 @@ pub(crate) fn eval(
line_buffer.push(' ');
}
match parser::parse_text(&line_buffer) {
let line_str = line_buffer.replace("\r", "").replace("\n", "");
match parser::parse_text(&line_str) {
Ok(instructions) => {
let (command_result, _) = runner::run_instruction(
commands,

9
examples/user_input.ds Normal file
View file

@ -0,0 +1,9 @@
echo Enter Full Name:
name = read
if is_empty ${name}
echo You didn't enter any value
else
echo Your name is: ${name}
end_if