DEV Community

Wesley de Groot
Wesley de Groot

Posted on • Originally published at wesleydegroot.nl on

Snippet: @EnvironmentVariable

If you are creating commandline apps you sometimes need to acces the operating system environment variables, while you can use getenv(name) it can come in handy to use a property wrapper if you need to access the environment variables often, or if you are updating them.

Full code:

/// A property wrapper to set and get system's environment variables values. /// ///

 ``` /// @EnvironmentVariable(name: "PATH") /// var path: String? /// ```

 @propertyWrapper public struct EnvironmentVariable { var name: String public var wrappedValue: String? { get { guard let pointer = getenv(name) else { return nil } return String(cString: pointer) } set { guard let value = newValue else { unsetenv(name) return } setenv(name, value, 1) } } }

in the future i'll post more about @propertyWrappers.

AWS GenAI LIVE image

Real challenges. Real solutions. Real talk.

From technical discussions to philosophical debates, AWS and AWS Partners examine the impact and evolution of gen AI.

Learn more

Top comments (0)

Postmark Image

Speedy emails, satisfied customers

Are delayed transactional emails costing you user satisfaction? Postmark delivers your emails almost instantly, keeping your customers happy and connected.

Sign up

👋 Kindness is contagious

Please leave a ❤️ or a friendly comment on this post if you found it helpful!

Okay