-
Notifications
You must be signed in to change notification settings - Fork 0
toml
weilycoder edited this page Oct 30, 2024
·
1 revision
输入 <filename> -h,你大概可以得到这样的结果:
usage: <filename> [-h] [--mode {client,server}] [-i HOST] [-p POST] [-b BUF] [--timeout TIMEOUT] [--superpasswd SUPERPASSWD]
Launch the File Transfer.
options:
-h, --help show this help message and exit
--mode {client,server}
Specify the launch mode, which defaults to starting in client mode.
-i HOST, --host HOST set the server name
-p POST, --post POST set the communication port
-b BUF, --buf BUF set buffer size, which must be greater than or equal to 1024
--timeout TIMEOUT set the timeout in second
--superpasswd SUPERPASSWD
set a super password, only effective when starting in server mode我们逐项进行解释:
-
-h, --help:显示这份帮助文件; -
--mode:指定启动方式,如<filename> --mode server指定以服务端模式启动; -
-i, --host:指定机器名称,一般是localhost或0.0.0.0;前者只监听本地通信,后者监听所有来源的通信; -
-p, --post:指定通信端口,必须是$0\sim 65535$ 之间的数字,不建议在没有端口冲突的情况下更改默认值($8080$); -
-b, --buf:指定通信缓冲区大小,一般使用默认值,注意客户端与服务端的本项设置必须相同; -
--timeout:指定连接超时,一般使用默认值; -
--superpasswd:指定超级密码,仅在启动服务器时指定有效,可以使用设定的超级密码下载/删除任意文件。
为了避免每次输入冗长的命令,程序提供 TOML 配置文件的解析。
以下是一个配置文件示例:
# filetransfer.toml
mode = "server"
[client]
host = "localhost"
post = 65432
buf = 1048576
timeout = 12.0
[server]
host = "0.0.0.0"
post = 65432
buf = 1048576
timeout = 10.0
superpasswd = "123456"逻辑上,程序首先读取 mode 项,然后根据 mode 项的内容读取 [client] 表或 [server] 表。
如果配置文件无法被解析,程序将终止,若原因是 TOML 的语法错误,将返回语法错误的位置(这一项由第三方库 tomlkit 完成);若语法正确,但某一项的类型错误,将返回 Unable to parse configuration file (key: {key}),这里 {key} 是错误的项的名称。
实际上,程序内还存有一份默认配置;运行时,以命令行参数、配置文件、默认配置的优先级顺序决定具体设置,并将设置保存到配置文件。
# 开头的是注释行,解析时将被忽略,重新写入时将丢失。