mirror of
https://github.com/LCTT/TranslateProject.git
synced 2025-02-03 23:40:14 +08:00
translated
This commit is contained in:
parent
b1ed72f4f6
commit
c905cdac41
@ -1,122 +0,0 @@
|
||||
[#]: subject: "Parse arguments with Lua"
|
||||
[#]: via: "https://opensource.com/article/22/11/lua-command-arguments"
|
||||
[#]: author: "Seth Kenlon https://opensource.com/users/seth"
|
||||
[#]: collector: "lkxed"
|
||||
[#]: translator: "geekpi"
|
||||
[#]: reviewer: " "
|
||||
[#]: publisher: " "
|
||||
[#]: url: " "
|
||||
|
||||
Parse arguments with Lua
|
||||
======
|
||||
|
||||
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][1] 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][2] 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
|
||||
```
|
||||
|
||||
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 apocalypse
|
||||
```
|
||||
|
||||
### Parse 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][3] makes it easy to install additional libraries. There are some very good ones, such as [alt-getopt][4], that provide additional infrastructure for parsing arguments.
|
||||
|
||||
--------------------------------------------------------------------------------
|
||||
|
||||
via: https://opensource.com/article/22/11/lua-command-arguments
|
||||
|
||||
作者:[Seth Kenlon][a]
|
||||
选题:[lkxed][b]
|
||||
译者:[译者ID](https://github.com/译者ID)
|
||||
校对:[校对者ID](https://github.com/校对者ID)
|
||||
|
||||
本文由 [LCTT](https://github.com/LCTT/TranslateProject) 原创编译,[Linux中国](https://linux.cn/) 荣誉推出
|
||||
|
||||
[a]: https://opensource.com/users/seth
|
||||
[b]: https://github.com/lkxed
|
||||
[1]: https://opensource.com/article/20/8/printf
|
||||
[2]: https://opensource.com/article/22/11/lua-worth-learning
|
||||
[3]: https://opensource.com/article/19/11/getting-started-luarocks
|
||||
[4]: https://opensource.com/article/21/8/parsing-commands-lua
|
122
translated/tech/20221129.3 ⭐️⭐️ Parse arguments with Lua.md
Normal file
122
translated/tech/20221129.3 ⭐️⭐️ Parse arguments with Lua.md
Normal file
@ -0,0 +1,122 @@
|
||||
[#]: subject: "Parse arguments with Lua"
|
||||
[#]: via: "https://opensource.com/article/22/11/lua-command-arguments"
|
||||
[#]: author: "Seth Kenlon https://opensource.com/users/seth"
|
||||
[#]: collector: "lkxed"
|
||||
[#]: translator: "geekpi"
|
||||
[#]: reviewer: " "
|
||||
[#]: publisher: " "
|
||||
[#]: url: " "
|
||||
|
||||
用 Lua 解析参数
|
||||
======
|
||||
|
||||
大多数计算机命令由两部分组成:命令和参数。命令是要执行的程序,而参数可能是命令选项或用户输入。如果没有这种结构,用户将不得不编辑命令的代码,以改变命令所处理的数据。想象一下重写 [printf][1] 命令只是为了让你的计算机用 “hello world” 消息问候您。参数对于交互式计算至关重要,[Lua 编程语言][2] 提供了 `{...}` 表达式来封装在启动 Lua 脚本时给定的可变参数。
|
||||
|
||||
### 在 Lua 中使用参数
|
||||
|
||||
几乎每一个给计算机的命令都假定一个参数,即使它期望参数是一个空列表。 Lua 会记录启动后写入的内容,即使你可能对这些参数不做任何操作。要在 Lua 启动时使用用户提供的参数,请迭代 `{...}` 表:
|
||||
|
||||
```
|
||||
local args = {...}
|
||||
|
||||
for i,v in ipairs(args) do
|
||||
print(v)
|
||||
end
|
||||
```
|
||||
|
||||
运行代码:
|
||||
|
||||
```
|
||||
$ lua ./myargs.lua
|
||||
$ lua ./myargs.lua foo --bar baz
|
||||
foo
|
||||
--bar
|
||||
baz
|
||||
----
|
||||
```
|
||||
|
||||
没有参数是安全的,Lua 会完全按照输入的方式打印所有参数。
|
||||
|
||||
### 解析参数
|
||||
|
||||
对于简单的命令,Lua 的基本功能足以解析和处理参数。这是一个简单的例子:
|
||||
|
||||
```
|
||||
-- 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
|
||||
```
|
||||
|
||||
在 `setup` 部分,将所有命令参数转储到名为 `args` 的变量中。
|
||||
|
||||
在 `engine` 部分,创建一个名为 `echo` 的函数,用于打印你“输入”其中的任何内容。
|
||||
|
||||
最后,在 `go` 部分,解析 `args` 变量(用户在启动时提供的参数)中的索引和值。在此示例代码中,为清楚起见,第一个 `for` 循环仅打印每个索引和值。
|
||||
|
||||
第二个 `for` 循环使用索引来检查第一个参数,它被假定是一个选项。此示例代码中唯一有效的选项是 `--say`。如果循环找到字符串 `--say`,它会调用 `echo` 函数,并将当前参数的索引_加 1_(_next_ 参数)作为函数参数提供。
|
||||
|
||||
命令参数的分隔符是一个或多个空格。运行代码查看结果:
|
||||
|
||||
```
|
||||
$ lua ./echo.lua --say zombie apocalypse
|
||||
1: --say
|
||||
2: zombie
|
||||
3: apocalypse
|
||||
echo: zombie
|
||||
```
|
||||
|
||||
大多数用户都知道在向计算机发出命令时空格很重要,因此在这种情况下删除第三个参数是预期的行为。下面是演示两种有效“转义”方法的变体:
|
||||
|
||||
```
|
||||
$ 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
|
||||
```
|
||||
|
||||
### 解析参数
|
||||
|
||||
手动解析参数简单且无依赖性。但是,你必须考虑一些细节。大多数现代命令都允许使用短选项(例如,`-f`)和长选项(`--foo`),并且大多数命令都提供 `-h` 或 `--help` 或者在没有所需参数时显示帮助菜单。
|
||||
|
||||
使用 [LuaRocks][3] 可以轻松安装其他库。有一些非常好的工具,例如 [alt-getopt][4],它们为解析参数提供了额外的基础设施。
|
||||
|
||||
--------------------------------------------------------------------------------
|
||||
|
||||
via: https://opensource.com/article/22/11/lua-command-arguments
|
||||
|
||||
作者:[Seth Kenlon][a]
|
||||
选题:[lkxed][b]
|
||||
译者:[geekpi](https://github.com/geekpi)
|
||||
校对:[校对者ID](https://github.com/校对者ID)
|
||||
|
||||
本文由 [LCTT](https://github.com/LCTT/TranslateProject) 原创编译,[Linux中国](https://linux.cn/) 荣誉推出
|
||||
|
||||
[a]: https://opensource.com/users/seth
|
||||
[b]: https://github.com/lkxed
|
||||
[1]: https://opensource.com/article/20/8/printf
|
||||
[2]: https://opensource.com/article/22/11/lua-worth-learning
|
||||
[3]: https://opensource.com/article/19/11/getting-started-luarocks
|
||||
[4]: https://opensource.com/article/21/8/parsing-commands-lua
|
Loading…
Reference in New Issue
Block a user