diff --git a/README.md b/README.md index 73fbe62..872e5ef 100644 --- a/README.md +++ b/README.md @@ -74,6 +74,7 @@ This library contains all the reference documentation and API reference-related * [Bytes](https://github.com/PerfectlySoft/PerfectDocs/blob/master/guide/bytes.md) * [File](https://github.com/PerfectlySoft/PerfectDocs/blob/master/guide/file.md) * [Dir](https://github.com/PerfectlySoft/PerfectDocs/blob/master/guide/dir.md) + * [Environmental Variables](https://github.com/PerfectlySoft/PerfectDocs/blob/master/guide/env.md) * [Threading](https://github.com/PerfectlySoft/PerfectDocs/blob/master/guide/thread.md) * [Networking](https://github.com/PerfectlySoft/PerfectDocs/blob/master/guide/net.md) @kjessup * [OAuth2 and Providers](https://github.com/PerfectlySoft/PerfectDocs/blob/master/guide/OAuth2.md) diff --git a/README.zh_CN.md b/README.zh_CN.md index c8f3124..7fb6f96 100644 --- a/README.zh_CN.md +++ b/README.zh_CN.md @@ -90,6 +90,7 @@ * [字节流转换](https://github.com/PerfectlySoft/PerfectDocs/blob/master/guide.zh_CN/bytes.md) * [文件操作](https://github.com/PerfectlySoft/PerfectDocs/blob/master/guide.zh_CN/file.md) * [目录与路径](https://github.com/PerfectlySoft/PerfectDocs/blob/master/guide.zh_CN/dir.md) + * [环境变量](https://github.com/PerfectlySoft/PerfectDocs/blob/master/guide.zh_CN/env.md) * [线程](https://github.com/PerfectlySoft/PerfectDocs/blob/master/guide.zh_CN/thread.md) * [网络](https://github.com/PerfectlySoft/PerfectDocs/blob/master/guide.zh_CN/net.md) @kjessup * [OAuth2 身份授权](https://github.com/PerfectlySoft/PerfectDocs/blob/master/guide.zh_CN/OAuth2.md) diff --git a/guide.zh_CN/env.md b/guide.zh_CN/env.md new file mode 100644 index 0000000..b6b8b38 --- /dev/null +++ b/guide.zh_CN/env.md @@ -0,0 +1,64 @@ +# 环境变量 + +## 使用方法 + +首先导入PerfectLib: + +``` swift +import PerfectLib +``` + +现在可以在程序中直接调用`Env`类对象操作环境变量: + +### 设置 + +- 设置一个环境变量 + +以下操作等同于 bash 命令 "export foo=bar" + +``` swift +Env.set("foo", value: "bar") +``` + +- 设置一组环境变量 + +同样可以使用字典方式设置一组环境变量 + +``` swift +Env.set(["foo":"bar", "koo":"kar"]) +// 结果等同于 bash 命令 "export foo=bar && export koo=kar" +``` + +### 读取 + +- 查询单个变量: + +``` swift +guard let foo = Env.get("foo") else { + // 查询失败 +} +``` + +- 查询单个变量,并附加默认值(如果不存在这个变量就用默认值代替) + +``` swift +guard let foo = Env.get("foo", defaultValue: "bar") else { + // 既然有默认值,则查询应该不会失败 +} +``` + +- 查询所有系统变量 + +``` swift +let all = Env.get() +// 结果是一个字典 [String: String] +``` + +### 删除 + +- 删除一个环境变量: + + +``` swift +Env.del("foo") +``` \ No newline at end of file diff --git a/guide.zh_CN/toc.json b/guide.zh_CN/toc.json index 444ce27..e758493 100644 --- a/guide.zh_CN/toc.json +++ b/guide.zh_CN/toc.json @@ -136,6 +136,10 @@ "name": "目录与路径", "doc": "dir" }, + { + "name": "环境变量", + "doc": "env" + }, { "name": "文件操作", "doc": "file" diff --git a/guide/env.md b/guide/env.md new file mode 100644 index 0000000..75a374d --- /dev/null +++ b/guide/env.md @@ -0,0 +1,63 @@ +# Environmental Operations + +## Usage + +First, ensure the `PerfectLib` is imported in your Swift file: + +``` swift +import PerfectLib +``` +You are now able to use the `Env` class to operate the environmental variables + +### Set + +- Single Variable Setting: + +This statement is equal to bash command "export foo=bar" + +``` swift +Env.set("foo", value: "bar") +``` + +- Group Setting: + +It is also possible to set a group of variables in a dictionary style: + +``` swift +Env.set(["foo":"bar", "koo":"kar"]) +// the result is identically the same as "export foo=bar && export koo=kar" +``` + +### Get + +- Single variable query: + +``` swift +guard let foo = Env.get("foo") else { + // there is no such a variable +} +``` + +- Single variable query with a default value: + +``` swift +guard let foo = Env.get("foo", defaultValue: "bar") else { + // there is no such a variable even with a default value?? +} +``` + +- Query all system variables: + +``` swift +let all = Env.get() +// the result of all is a dictionary [String: String] +``` + +### Delete + +- Delete an environmental variable: + + +``` swift +Env.del("foo") +``` \ No newline at end of file diff --git a/guide/toc.json b/guide/toc.json index 40228a6..39d0e03 100644 --- a/guide/toc.json +++ b/guide/toc.json @@ -136,6 +136,10 @@ "name": "Dir", "doc": "dir" }, + { + "name": "Environmental Variables", + "doc": "env" + }, { "name": "File", "doc": "file"