Skip to content

Commit cd7904a

Browse files
committed
Adds more examples and sections
1 parent 18fc817 commit cd7904a

1 file changed

Lines changed: 91 additions & 2 deletions

File tree

README.md

Lines changed: 91 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
# cpp-dotenv
22

3-
C++ implementation of nodejs [dotenv](https://github.com/motdotla/dotenv) project. Loads environment variables from .env for C++ projects.
3+
![version 0.1.0](https://img.shields.io/badge/version-0.1.0-blue)
4+
5+
C++ implementation of NodeJS [dotenv](https://github.com/motdotla/dotenv) project. Loads environment variables from `.env` for C++ projects.
46

57
**Please take into account this is still a developing project.**
68

@@ -12,6 +14,9 @@ C++ implementation of nodejs [dotenv](https://github.com/motdotla/dotenv) projec
1214
2. [Usage](#usage)
1315
1. [CMake](#cmake)
1416
3. [Examples](#examples)
17+
1. [Basic usage](#basic-usage)
18+
2. [Reference renaming](#reference-renaming)
19+
3. [Several dotenv files](#several-dotenv-files)
1520
4. [Grammar](#grammar)
1621

1722
## Dependencies
@@ -34,6 +39,8 @@ using namespace dotenv;
3439

3540
For convenience, **cpp-dotenv** auto-configures a class object (which is instance of the singleton class `dotenv`) by calling the `load_dotenv()` method at the very beginning of your file (just right before the end of `dotenv.h`) and trying to load a `.env` file, although if you need to add-in your own files (like `.myenv`), simply re-run the loading step passing the file name as parameter; everything new will show up on the `dotenv` instances.
3641

42+
By default, already-defined environment variables are not overwritten even if redefined in some of the loaded files. This behavior can be changed, however, by calling the `load_config()` function with the `overwrite` parameter set to `true`. For an example, take a look at [this one](#several-dotenv-files).
43+
3744
Also for convenience, there is a namespace-global pre-loaded reference variable to the `dotenv` singleton class instance named `env`. Simply use it as you would use a dotenv object on NodeJS, or you can define your own references:
3845

3946
```cpp
@@ -56,6 +63,8 @@ After this, you might use the library as described in [usage](#usage); no extra
5663

5764
## Examples
5865

66+
### Basic usage
67+
5968
Assume the following `.env` file:
6069

6170
```env
@@ -80,8 +89,34 @@ using namespace std;
8089

8190
int main()
8291
{
83-
auto& dotenv = env; // Reference re-naming
92+
cout << "DB_NAME: " << env["DB_NAME"] << endl;
93+
cout << "eval \"" << env["COMMAND"] << " " << env["HOST"] << "\"" << endl;
94+
}
95+
```
8496

97+
would produce the following output:
98+
99+
```shell
100+
$ ./main
101+
DB_NAME: DontDoThisAtHome
102+
eval "ping 8.8.8.8"
103+
```
104+
105+
### Reference renaming
106+
107+
Assuming the same `.env` file as in the [previous case](#basic-usage), the predefined `env` reference can be easily renamed and used just exactly as the original one.
108+
109+
The following code:
110+
111+
```cpp
112+
#include "dotenv.h"
113+
#include <iostream>
114+
115+
using namespace std;
116+
117+
int main()
118+
{
119+
auto& dotenv = dotenv::env;
85120
cout << "DB_NAME: " << dotenv["DB_NAME"] << endl;
86121
cout << "eval \"" << dotenv["COMMAND"] << " " << dotenv["HOST"] << "\"" << endl;
87122
}
@@ -95,6 +130,60 @@ $ ./main
95130
eval "ping 8.8.8.8"
96131
```
97132

133+
### Several dotenv files
134+
135+
The situation of having several different dotenv files is no stranger one (`.env` for private configuration variables, `.pubenv` for public variables, etc.). Loading several files in addition to the default one and overwritting any variables that are redefined on the files can be done as follows:
136+
137+
Assume the following `.env` file:
138+
139+
```env
140+
# DB THINGS
141+
DB_NAME=DontDoThisAtHome
142+
DB_PASS=such_security
143+
```
144+
145+
And the following `.pubenv` file:
146+
147+
```env
148+
# CONNECTIONS THINGS
149+
COMMAND=ping
150+
HOST=8.8.8.8
151+
MESSAGE="Hey buddy!"
152+
```
153+
154+
The following source file:
155+
156+
```cpp
157+
#include "dotenv.h"
158+
#include <iostream>
159+
160+
using namespace dotenv;
161+
using namespace std;
162+
163+
int main()
164+
{
165+
env.load_dotenv(".env", true);
166+
env.load_dotenv(".pubenv", true);
167+
cout << "DB_NAME: " << env["DB_NAME"] << endl;
168+
cout << "eval \"" << env["COMMAND"] << " " << env["HOST"] << "\"" << endl;
169+
}
170+
```
171+
172+
would produce the following output:
173+
174+
```shell
175+
$ ./main
176+
DB_NAME: DontDoThisAtHome
177+
eval "ping 8.8.8.8"
178+
```
179+
98180
## Grammar
99181

100182
For the geeks, you can check the grammar I've implemented on the `grammar/env.g4` file. Despite being written in an ANTLR4 fashion, I've implemented a simple recursive parser myself given the basic nature of the language. The parser and its methods are publicly available under the `dotenv::parser` class.
183+
184+
## Known issues
185+
186+
The complete list of issues con be consulted at the [issues page](https://github.com/adeharo9/cpp-dotenv/issues).
187+
188+
1. [Variable resolution on values not yet vailable](https://github.com/adeharo9/cpp-dotenv/issues/3)
189+
2. Scaped sequences not being scaped inside strings

0 commit comments

Comments
 (0)