-
Notifications
You must be signed in to change notification settings - Fork 128
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #64 from RockfordWei/master
Implementing Environmental Variables.
- Loading branch information
Showing
6 changed files
with
137 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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") | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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") | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters