Open-source News

Intel Advances Linux "IPC Classes" Design To Improve Load Balancing For Hybrid CPUs

Phoronix - Tue, 11/29/2022 - 20:13
Back in September was a big patch set working out classes of tasks for hybrid CPUs and more properly implementing Intel Thread Director for Linux. This work to better the performance/efficiency of modern Intel Core CPUs with a mix of P and E cores has now advanced past the "request for comments" stage with a new patch series sent out on Monday...

Apple SoC CPUFreq Linux Driver Moves Closer To Mainline

Phoronix - Tue, 11/29/2022 - 19:50
The Apple SoC CPUFreq driver worked on by the Asahi Linux crew for CPU frequency scaling with the M1 and M2 chips under Linux looks like it could soon be reaching the mainline kernel...

Cryptsetup 2.6 Released With Support For Apple FileVault2

Phoronix - Tue, 11/29/2022 - 19:15
Cryptsetup 2.6 is now available as the newest version of this utility for managing disk encryption on Linux systems in conjunction with the DMcrypt kernel module...

ClamAV Anti-Virus Reaches Version 1.0 With New LTS Release

Phoronix - Tue, 11/29/2022 - 19:00
ClamAV as one of the leading open-source anti-virus / anti-malware toolkits for Linux / Windows / BSDs has finally reached the version 1.0 milestone...

Parse arguments with Lua

opensource.com - Tue, 11/29/2022 - 16:00
Parse arguments with Lua Seth Kenlon Tue, 11/29/2022 - 03:00

Most computer commands consist of two parts: The command and arguments. The command is the program meant to be executed, while the arguments might be command options or user input. Without this structure, a user would have to edit the command's code just to change the data that the command processes. 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.

Use 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 launches, even though you may do nothing with those arguments. To use arguments provided by the user when Lua starts, iterate over the {…​} table:

local args = {...}

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

Run the code:

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

Having no arguments is safe, and Lua prints all arguments exactly as entered.

Parse arguments

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

-- setup

local args = {...}

-- engine

function echo(p)
   print(p)
end

-- go

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

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

Programming and development Red Hat Developers Blog Programming cheat sheets Try for free: Red Hat Learning Subscription eBook: An introduction to programming with Bash Bash shell scripting cheat sheet eBook: Modernizing Enterprise Java An open source developer's guide to building applications

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

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

Finally, in the go section, 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, it calls the echo function, and the index of the current argument plus 1 (the next argument) is provided as the function parameter.

The delimiter for command arguments is one or more empty spaces. Run the code to see the result:

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

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:

$ 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 apocalypseParse arguments

Parsing arguments manually is simple and dependency-free. However, there are details you must consider. Most modern commands allow for 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. There are some very good ones, such as alt-getopt, that provide additional infrastructure for parsing arguments.

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.

Image by:

WOCinTech Chat. Modified by Opensource.com. CC BY-SA 4.0

Programming What to read next This work is licensed under a Creative Commons Attribution-Share Alike 4.0 International License. Register or Login to post a comment.

How to Install Viber on Linux Desktop

Tecmint - Tue, 11/29/2022 - 15:00
The post How to Install Viber on Linux Desktop first appeared on Tecmint: Linux Howtos, Tutorials & Guides .

Brief: This article shows various ways to install and use Viber, a free voice and video call, and messaging app in Linux. Viber is a well-known, free, and secure voice/video calls and messaging app,

The post How to Install Viber on Linux Desktop first appeared on Tecmint: Linux Howtos, Tutorials & Guides.

Pages