Skip to content

Commit

Permalink
Merge pull request #64 from RockfordWei/master
Browse files Browse the repository at this point in the history
Implementing Environmental Variables.
  • Loading branch information
kjessup authored Nov 20, 2018
2 parents 2b1201b + 9f678c3 commit e2bff6f
Show file tree
Hide file tree
Showing 6 changed files with 137 additions and 0 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
1 change: 1 addition & 0 deletions README.zh_CN.md
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
64 changes: 64 additions & 0 deletions guide.zh_CN/env.md
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")
```
4 changes: 4 additions & 0 deletions guide.zh_CN/toc.json
Original file line number Diff line number Diff line change
Expand Up @@ -136,6 +136,10 @@
"name": "目录与路径",
"doc": "dir"
},
{
"name": "环境变量",
"doc": "env"
},
{
"name": "文件操作",
"doc": "file"
Expand Down
63 changes: 63 additions & 0 deletions guide/env.md
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")
```
4 changes: 4 additions & 0 deletions guide/toc.json
Original file line number Diff line number Diff line change
Expand Up @@ -136,6 +136,10 @@
"name": "Dir",
"doc": "dir"
},
{
"name": "Environmental Variables",
"doc": "env"
},
{
"name": "File",
"doc": "file"
Expand Down

0 comments on commit e2bff6f

Please sign in to comment.