Perfect icon indicating copy to clipboard operation
Perfect copied to clipboard

Implementing Environmental Utility

Open RockfordWei opened this issue 6 years ago • 2 comments

Environmental Operations

Usage

First, ensure the PerfectLib is imported in your Swift file:

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"

Env.set("foo", value: "bar")
  • Group Setting:

It is also possible to set a group of variables in a dictionary style:

Env.set(["foo":"bar", "koo":"kar"])
// the result is identically the same as "export foo=bar && export koo=kar"

Get

  • Single variable query:
guard let foo = Env.get("foo") else {
	// there is no such a variable
}
  • Single variable query with a default value:
guard let foo = Env.get("foo", defaultValue: "bar") else {
	// there is no such a variable even with a default value??
}
  • Query all system variables:
let all = Env.get()
// the result of all is a dictionary [String: String]

Delete

  • Delete an environmental variable:
Env.del("foo")

RockfordWei avatar Nov 07 '18 16:11 RockfordWei

This should be added👍🏼

ThomasDeLange avatar Feb 01 '19 12:02 ThomasDeLange

CLA assistant check
Thank you for your submission! We really appreciate it. Like many open source projects, we ask that you all sign our Contributor License Agreement before we can accept your contribution.
1 out of 2 committers have signed the CLA.

:white_check_mark: RockfordWei
:x: RockyCognizant
You have signed the CLA already but the status is still pending? Let us recheck it.

CLAassistant avatar Apr 25 '23 18:04 CLAassistant