On Linux, this is defined by the Freedesktop specification. For clarity during this exercise, just save the file in your home directory so that it's easy to find and use. First, you must tell Lua where you've placed the additional library you want it to use. The package. You can view Lua's default package path in a terminal:. With the package location established, the next thing to do is to require the inifile library and then handle some OS logistics.
Even though this is a simple example application, the code needs to get the user's home directory location from the OS and establish how to communicate filesystem paths back to the OS when necessary:. Now you can use inifile to parse data from the config file into a Lua table. Once the data has been placed into a table, you can query the table as you would any other Lua table:. Not all parser libraries read and write data often called encoding and decoding , but the inifile library does. That means you can use it to make changes to a configuration file.
To change a value in a configuration file, you set the variable representing the value in the parsed table, and then you write the table back to the configuration file:.
The ability to save data about how a user wants to use an application is an important part of programming. Fortunately, it's a common task for programmers, so much of the work has probably already been done. Find a good library for encoding and decoding into an open format, and you can provide a persistent and consistent user experience.
Parsing config files with Lua Opensource. Configure persistent application settings with the Lua programming language. Image credits :.
Get the highlights in your inbox every week. Choose a format Programming and development. What is a config file? There are several popular formats for configuration files, each with its own strengths. Find what works best for you. Stack Overflow for Teams — Collaborate and share knowledge with a private group. Create a free Team What is Teams?
Learn more. Should I use Lua for writing config files? Ask Question. Asked 4 years, 11 months ago. Active 4 years, 9 months ago. Viewed 5k times. However, I also heard that using programming to configure a program is an anti-pattern. Since Lua does not get compiled as part of the program, would this prove as an exception?
Improve this question. Lawful Lazy. Lawful Lazy Lawful Lazy 1, 2 2 gold badges 9 9 silver badges 11 11 bronze badges. I can't speak for Lua, but the idea to have a program being a configuration makes my stomach grumble. Still, having a programming language for complex configuration beats something like sendmail.
The OP is asking about using Lua in the same way. It wasn't; it's just JavaScript. It was created because sending JavaScript was something that web developers could already do, so they encoded their data as JavaScript files that were then executed.
Eventually, people streamlined this process by creating dedicated JSON parsers. But the basic idea of using a scripting language as a data language is at the very heart of JSON. If it was valid for JavaScript, why wouldn't it be valid for Lua? I use Lua for this purpose, but that said: 1 the software is happily married to Lua as its primary embedded scripting language.
If a user wants to write an infinite loop in their configs and make it so the software can't start up, so be it. In my case I don't use an explicit return to return a table. I just define global variables the config script is given a clean environment, and the environment is tossed away after reading the configs. Show 3 more comments. Sign up using Email and Password. Post as a guest Name. Email Required, but never shown.
The Overflow Blog. Does ES6 make JavaScript frameworks obsolete? Podcast Do polyglots have an edge when it comes to mastering programming Featured on Meta. Now live: A fully responsive profile. Related 1. Hot Network Questions. Question feed. Stack Overflow works best with JavaScript enabled.
0コメント