-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdotenv_demo.cpp
31 lines (26 loc) · 907 Bytes
/
dotenv_demo.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
#include "dotenv.h"
#include <iostream>
using namespace std;
using namespace dotenv;
int main() {
if (!env.load_dotenv()) {
cerr << "Failed to load .env file" << endl;
return 1;
}
if (env.has("DB_NAME")) {
cout<<"DB_NAME exists"<<endl;
}
cout<<"================================================================"<<endl;
// Get all variables
for (const auto& [key, value] : env.all()) {
cout << key << "=" << value << endl;
}
cout<<"================================================================"<<endl;
cout << "DB_NAME: " << env["DB_NAME"] << endl;
cout << "eval \"" << env["COMMAND"] << " " << env["HOST"] << "\"" << endl;
// You can also use the safer get() method that returns an optional
if (auto db_name = env.get("DB_NAME")) {
cout << "Database name: " << *db_name << endl;
}
return 0;
}