Open-source News

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.

Give back to open source on Giving Tuesday

opensource.com - Tue, 11/29/2022 - 15:00
Give back to open source on Giving Tuesday Jason Hibbets Tue, 11/29/2022 - 02:00

Giving Tuesday is always a reminder to me to consider how to give back to open source projects, communities, and organizations. In 2012, GivingTuesday was created to encourage people to simply do good. "Since then, it has grown into a year-round global movement that inspires hundreds of millions of people to give, collaborate, and celebrate generosity."

When I think about all the ways I have benefited from "standing on the shoulder of giants," both professionally and personally. I start to think about all the individuals and organizations that have made being part of the open source community such a rewarding experience. Many have used an open source project or joined an open source community and immediately benefited from the contributions of past and current members.

When you dig a little deeper, you’ll find that many of the organizations listed below have volunteer opportunities, ways to get involved with their community, and simple things like sharing information about why you support the organization or joining a newsletter to stay updated. They offer a variety of ways to support them beyond just financial donations.

The list below is not an endorsement from myself or my employer, it is a collection of organizations for you to consider as you look at groups to support and ways to donate time, money, expertise, and other skills you might offer. I would encourage you to do your own research before making a donation, particularly if you are looking for any tax deductions.

18 free and open source organizations to consider for Giving Tuesday Black Girls Code

"We build pathways for young women of color to embrace the current tech marketplace as builders and creators by introducing them to skills in computer programming and technology."

ChickTech

"ChickTech is a national nonprofit organization empowering people of marginalized genders through tech programs and events." (See several options under the "Support us" tab.)

Creative Commons

"Creative Commons is a nonprofit organization that helps overcome legal obstacles to the sharing of knowledge and creativity to address the world’s most pressing challenges." 

Drupal Association

"The Drupal Association is a nonprofit 501©3 organization serving the international Drupal community." 

Electronic Frontier Foundation (EFF)

"The Electronic Frontier Foundation mission is to support and defend civil liberties and human rights online, they are a 501©3 organization."

Free Software Foundation (FSF)

"The Free Software Foundation (FSF) is a nonprofit with a worldwide mission to promote computer user freedom."

Fosstodon and other Mastodon servers

"Fosstodon is part of the decentralized ActivityPub social network. “It’s open to anyone interested in free and open source technology."

Note: There are many instances of Mastodon servers creating a decentralized social network. This is just one example supporting open source and technology enthusiasts.

More Linux resources Linux commands cheat sheet Advanced Linux commands cheat sheet Free online course: RHEL technical overview Linux networking cheat sheet SELinux cheat sheet Linux common commands cheat sheet What are Linux containers? Our latest Linux articles Linux Foundation

"The Linux Foundation provides critical services to promote sustainable open source. Individual Supporter donations go directly into programs and resources that support growing communities."

Note: Donations to The Linux Foundation are not generally deductible by US taxpayers.

Let’s Encrypt

"Let’s Encrypt is a free, automated, and open certificate authority brought to you by the nonprofit Internet Security Research Group (ISRG) supporting a secure and privacy-respecting Web."

LibreOffice (The Document Foundation)

"The Document Foundation is a charitable foundation whose objective is the promotion and development of office software available for use by anyone free of charge. The foundation promotes a sustainable, independent, and meritocratic community for the international development of free and open source software based on open standards."

Open source hardware association

"The Open Source Hardware Association (OSHWA) aims to foster technological knowledge and encourage research that is accessible, collaborative, and respects user freedom."

Open Source Initiative (OSI)

"The Open Source Initiative (OSI) is a non-profit corporation with global scope formed to educate about and advocate for the benefits of open source and to build bridges among different constituencies in the open source community."

Note: Their membership drive begins on November 29.

Raspberry Pi Foundation

"The Raspberry Pi Foundation is a United Kingdom-based charity with the mission to enable young people to realize their full potential through the power of computing and digital technologies."

Software Freedom Conservancy

"Software Freedom Conservancy is a nonprofit organization centered around ethical technology whose mission is to ensure the right to repair, improve and reinstall software."

TransTech

"TransTech is an incubator for LGBTQ Talent with a focus on economically empowering the T, transgender people, in our community. TransTech is fiscally sponsored by Allied Media Projects making all donations tax-deductible."

PHP Foundation (Open Collective)

"We support, promote, and advance the PHP language."

Python Software Foundation

"The mission of the Python Software Foundation is to promote, protect, and advance the Python programming language, and to support and facilitate the growth of a diverse and international community of Python programmers."

The Wikimedia Foundation

"The Wikimedia Foundation is the nonprofit organization that supports Wikipedia, the other Wikimedia free knowledge projects, and our mission of free knowledge for every single person."

I’m almost certain that I did not list a group that you are passionate about. If you feel so inclined, please share an open source community that you think should be on this list in the comments below.

I hope this article inspires you to give something back, no matter how big or small, to an open source community or organization to help keep open source more sustainable.

Here are 18 ways to support and donate to free and open source organizations for Giving Tuesday.

Image by:

Opensource.com

Diversity and inclusion Tools Programming Linux Raspberry Pi Python Hardware LibreOffice 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.

Red Hat Summit 2023: Save the date!

Red Hat News - Tue, 11/29/2022 - 08:00
<p><span><span><span><span><span><span>Red Hat Summit is the place for our customers, partners, community members and IT professionals to come together to innovate, learn and help shape the future of the enterprise. This year, we are returning to Boston, Massachusetts, and encourage you and your team to join us on </span></span></span></span></span></span><span><span><span>

Red Hat Adds New Collaboration and MLOps Capabilities in Red Hat OpenShift Data Science

Red Hat News - Tue, 11/29/2022 - 08:00
<p><span><span><span><span><span><span>Today, we are announcing several new capabilities to Red Hat OpenShift Data Science, our managed cloud service for data scientists and developers of intelligent applications. Red Hat OpenShift Data Science now includes new features for deeper data analysis and better collaboration between ITOps, data scientists, and intelligent application developers. Furthermore, customers can now use committed AWS spend to purchase and run Red Hat OpenShift Data Science dir

Coreboot Joins The Open-Source Firmware Foundation

Phoronix - Tue, 11/29/2022 - 08:00
Earlier this year the Open-Source Firmware Foundation was created to help advance open firmware development. Today the Coreboot project has officially joined the Open-Source Firmware Foundation...

AMD Software: PRO Edition 22.Q4 for Linux Released

Phoronix - Tue, 11/29/2022 - 07:01
A little more than a week ago AMD quietly released their Radeon Pro Software for Enterprise Linux 22.Q4 driver, also advertised more recently as the AMD Software: PRO Edition 22.Q4 for Linux driver package. With most Linux enthusiasts and gamers happily using the upstream open-source components in the mainline Linux kernel and Mesa, the new quarterly release slipped under the radar until now...

KDE Improving File Access For Apple iOS Devices With New KIO AFC

Phoronix - Tue, 11/29/2022 - 03:45
With KDE Gear next year there will be improved support for accessing files from Apple iOS devices like iPhones and iPads. There already exist several KDE implementations for accessing files off iOS devices while a new one written against the latest KIO code has been merged...

Pages