Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Expose an API for parsing Strings #15

Open
swfsql opened this issue May 30, 2019 · 6 comments
Open

Expose an API for parsing Strings #15

swfsql opened this issue May 30, 2019 · 6 comments
Labels
enhancement New feature or request

Comments

@swfsql
Copy link

swfsql commented May 30, 2019

I once had an use-case where a String (instead of strictly a file/args) would need to be parsed.
Is it possible to expose an API for String parsing?

@ZoeyR
Copy link
Contributor

ZoeyR commented May 30, 2019

I'm curious, what was the use-case you had where you had a String instead of a file?

@ZoeyR ZoeyR added the enhancement New feature or request label May 30, 2019
@swfsql
Copy link
Author

swfsql commented May 31, 2019

I was using structopt as well, which can get values from env vars, and I was setting the default values of such "env" vars with dotenv lol by including such "defaults" files and parsing their contents

So I'd parse from &str and I'd not necessarily push into the local env vars, id first get the parsed Hashmap of vars=value as well

@ZoeyR
Copy link
Contributor

ZoeyR commented May 31, 2019

I didn't realize that structopt can fill in values from environment variables, interesting. If it can do that is there a reason that you did not run dotenv() to fill out your environment and then run structopt? That should have filled everything in right?

@swfsql
Copy link
Author

swfsql commented May 31, 2019

Oh in some cases I preferred to read strings, I think it was for compile-time assertion.
The wrapping setup could also remove all env-vars at start (we also didn't enjoy using env vars directly from "within the code", everything env and config related would be setup early as a Config<> structure).

So we could use config files, command line args, env vars and .env files (multiple, recursive, etc), disable the env vars from the shell and also link to some default .env files as env vars at compile-time, as a default fallback (structopt read values as static strings as seen in TeXitoi/structopt#150 ).

We probably should just have stuck to config files and env vars, but oh well.. ^^

@albel727
Copy link

I'm curious, what was the use-case you had where you had a String instead of a file?
As for me, I need this, when I want to bake environment configuration into the program at compile time.

I'd use include_str!("../../.env") to get a &'static str with the compile time .env file contents, which I'd then parse at runtime with dotenv to set environment variables. I don't want to use dotenv! macro, because that robs me of the flexibility of being able to make my whole program depend on runtime environment again by changing just a couple of lines at the beginning of main().

In fact I may want to use both dotenv::dotenv() and parsing the baked-in .env string, to create environment variables at runtime -> .env file at runtime -> .env file at compile time chain of fallback.

@0xForerunner
Copy link

+1 I think this would be useful for unit tests!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

No branches or pull requests

4 participants