Most computer commands consist of two parts: the command and arguments. The command is the program that’s meant to be executed, while the arguments might be command options or user input. Without this ability, a user would have to edit the code of a command just to change the data that the command processed. Imagine rewriting the

printf

command just to get your computer to greet you with a "hello world" message. Arguments are vital to interactive computing, and the Lua programming language provides the `{...}` expression to encapsulate *varargs* given at the time of launching a Lua script.

Using arguments in Lua

Almost every command given to a computer assumes an *argument*, even if it expects the argument to be an empty list. Lua records what’s written after it has been launched, even though you may do nothing with those arguments. To use arguments provided by the user when Lua is started, you iterate over the `{...}` table:

Run the code:

It’s safe to have no arguments, and all arguments are printed exactly as entered.

Parsing arguments

For simple commands, the basic Lua faculties are sufficient to parse and process arguments. Here’s a simple example:

In the `setup` section, you dump all command arguments into a variable called `args`.

In the `engine` section, you create a function called `echo` that prints whatever you "feed" into it.

Finally, in the `go` section, you parse the index and values in the `args` variable (the arguments provided by the user at launch). In this sample code, the first `for` loop just prints each index and value for clarity.

The second `for` loop uses the index to examine the first argument, which is assumed to be an option. The only valid option in this sample code is `--say`. If the loop finds the string `--say`, then the `echo` function is called, and the index of the current argument *plus 1* (the *next* argument) is provided as the function parameter.

Empty space (one or more spaces) is the delimiter for command arguments. Run the code to see the result:

Most users learn that spaces are significant when giving commands to a computer, so dropping the third argument in this case is expected behavior. Here’s a variation to demonstrate two valid "escape" methods:

Parsing arguments

Parsing arguments manually is simple and dependency-free. However, there are details you must consider. Most modern commands allow for both short options (for instance, `-f`) and long options (`--foo`), and most offer a help menu with `-h` or `--help` or when a required argument isn’t supplied.

Using

LuaRocks

makes it easy to install additional libraries, and there are some very good ones, such as

alt-getopt

, that provide additional infrastructure for parsing arguments.

Proxied content from gemini://sdf.org/klaatu/geminifiles/lua-parameters.gmi (external content)

Gemini request details:

Original URL
gemini://sdf.org/klaatu/geminifiles/lua-parameters.gmi
Status code
Success
Meta
text/gemini
Proxied by
kineto

Be advised that no attempt was made to verify the remote SSL certificate.