BreakingExpress

Parse arguments with Lua | Opensource.com

Most pc instructions include two elements: The command and arguments. The command is this system meant to be executed, whereas the arguments could be command choices or consumer enter. Without this construction, a consumer must edit the command’s code simply to alter the info that the command processes. Imagine rewriting the printf command simply to get your pc to greet you with a “hello world” message. Arguments are very important to interactive computing, and the Lua programming language supplies the {…​} expression to encapsulate varargs given on the time of launching a Lua script.

Use arguments in Lua

Almost each command given to a pc assumes an argument, even when it expects the argument to be an empty checklist. Lua information what’s written after it launches, although chances are you’ll do nothing with these arguments. To use arguments offered by the consumer when Lua begins, iterate over the {…​} desk:

native args = {...}

for i,v in ipairs(args) do
    print(v)
finish

Run the code:

$ lua ./myargs.lua
$ lua ./myargs.lua foo --bar baz
foo
--bar
baz
----

Having no arguments is secure, and Lua prints all arguments precisely as entered.

Parse arguments

For easy instructions, the essential Lua schools are enough to parse and course of arguments. Here’s a easy instance:

-- setup

native args = {...}

-- engine

perform echo(p)
   print(p)
finish

-- go

for i,v in ipairs(args) do
  print(i .. ": " .. v)
finish

for i,v in ipairs(args) do
  if args[i] == "--say" then
    echo("echo: " .. args[i+1])
  finish
finish

Programming and improvement

In the setup part, dump all command arguments right into a variable referred to as args.

In the engine part, create a perform referred to as echo that prints no matter you “feed” into it.

Finally, within the go part, parse the index and values within the args variable (the arguments offered by the consumer at launch). In this pattern code, the primary for loop simply prints every index and worth for readability.

The second for loop makes use of the index to look at the primary argument, which is assumed to be an choice. The solely legitimate choice on this pattern code is --say. If the loop finds the string --say, it calls the echo perform, and the index of the present argument plus 1 (the subsequent argument) is offered because the perform parameter.

The delimiter for command arguments is a number of empty areas. Run the code to see the end result:

$ lua ./echo.lua --say zombie apocalypse
1: --say
2: zombie
3: apocalypse
echo: zombie

Most customers be taught that areas are vital when giving instructions to a pc, so dropping the third argument, on this case, is predicted conduct. Here’s a variation to reveal two legitimate “escape” strategies:

$ lua ./echo.lua --say "zombie apocalypse"
1: --say
2: zombie apocalypse
echo: zombie apocalypse

$ lua ./echo.lua --say zombie apocalypse
1: --say
2: zombie apocalypse
echo: zombie apocalypse

Parse arguments

Parsing arguments manually is easy and dependency-free. However, there are particulars you could think about. Most trendy instructions enable for brief choices (as an example, -f) and lengthy choices (--foo), and most supply a assist menu with -h or --help or when a required argument is not provided.

Using LuaRocks makes it straightforward to put in further libraries. There are some superb ones, reminiscent of alt-getopt, that present further infrastructure for parsing arguments.

Exit mobile version