Add support for script args

This commit is contained in:
Filip Tibell 2023-01-19 00:23:06 -05:00
parent ce4a2d1620
commit 67235f436a
No known key found for this signature in database
3 changed files with 47 additions and 9 deletions

View file

@ -12,6 +12,18 @@ module.hello()
--[==[ --[==[
EXAMPLE #2 EXAMPLE #2
Using arguments given to the program
]==]
local arg: string? = ...
if arg then
print("\nGot an argument while running hello_lune:")
print(arg)
end
--[==[
EXAMPLE #3
Get & set environment variables Get & set environment variables
Checks if environment variables are empty or not, Checks if environment variables are empty or not,
@ -32,7 +44,7 @@ for _, key in vars do
end end
--[==[ --[==[
EXAMPLE #3 EXAMPLE #4
Read files in the current directory Read files in the current directory
@ -79,7 +91,7 @@ if process.getEnvVar("GITHUB_ACTIONS") then
end end
--[==[ --[==[
EXAMPLE #4 EXAMPLE #5
Call out to another program / executable Call out to another program / executable
@ -94,7 +106,7 @@ local result = process.spawn("ping", {
}) })
--[==[ --[==[
EXAMPLE #5 EXAMPLE #6
Using the result of a spawned process, exiting the process Using the result of a spawned process, exiting the process

View file

@ -4,7 +4,7 @@ use std::{
}; };
use clap::Parser; use clap::Parser;
use mlua::{Lua, Result}; use mlua::{Lua, MultiValue, Result, ToLua};
use crate::lune::{fs::LuneFs, json::LuneJson, process::LuneProcess}; use crate::lune::{fs::LuneFs, json::LuneJson, process::LuneProcess};
@ -14,13 +14,31 @@ use crate::lune::{fs::LuneFs, json::LuneJson, process::LuneProcess};
pub struct Cli { pub struct Cli {
/// Path to the file to run /// Path to the file to run
path: String, path: String,
/// Arguments to pass to the file
args: Vec<String>,
} }
impl Cli { impl Cli {
#[allow(dead_code)] #[allow(dead_code)]
pub fn from_path<S: AsRef<str>>(path: S) -> Self { pub fn from_path<S>(path: S) -> Self
where
S: Into<String>,
{
Self { Self {
path: path.as_ref().to_owned(), path: path.into(),
args: vec![],
}
}
#[allow(dead_code)]
pub fn from_path_with_args<S, A>(path: S, args: A) -> Self
where
S: Into<String>,
A: Into<Vec<String>>,
{
Self {
path: path.into(),
args: args.into(),
} }
} }
@ -35,8 +53,15 @@ impl Cli {
globals.set("process", LuneProcess::new())?; globals.set("process", LuneProcess::new())?;
globals.set("json", LuneJson::new())?; globals.set("json", LuneJson::new())?;
lua.sandbox(true)?; lua.sandbox(true)?;
// Run the file // Load & call the file with the given args
lua.load(&file_contents).exec_async().await?; let lua_args = self
.args
.iter()
.map(|value| value.to_owned().to_lua(&lua))
.collect::<Result<Vec<_>>>()?;
lua.load(&file_contents)
.call_async(MultiValue::from_vec(lua_args))
.await?;
Ok(()) Ok(())
} }
} }

View file

@ -26,7 +26,8 @@ async fn main() -> Result<()> {
#[tokio::test] #[tokio::test]
async fn hello_lune() { async fn hello_lune() {
let cli = Cli::from_path("hello_lune"); let args = vec!["Hello, test! ✅".to_owned()];
let cli = Cli::from_path_with_args("hello_lune", args);
let result = cli.run().await; let result = cli.run().await;
assert!(result.is_ok()); assert!(result.is_ok());
} }