docs/pages/getting-started/2-introduction/4-script-arguments.md
2024-01-14 19:07:03 +01:00

1.1 KiB

Script Arguments

Arguments can be passed to Lune scripts directly from the command line when running them:

lune run script-name arg1 arg2 "argument three"

These arguments will then be available in your script using the process built-in library, more specifically in process.args:

local process = require("@lune/process")

print(process.args)
--> { "arg1", "arg2", "argument three" }

Arguments in process.args will always be a table that is a contiguous array, and are guaranteed to not change during runtime. A useful pattern here could be to check for arguments given, and if there are none, prompt the user for input:

local process = require("@lune/process")
local stdio = require("@lune/stdio")

if #process.args > 3 then
	error("Too many arguments!")
elseif #process.args > 0 then
	print("Got arguments:")
	print(process.args)
else
	print("Got no arguments ☹️")
	local prompted = stdio.prompt("Please enter some text:")
	print("Got prompted text:", prompted)
end