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

Add environment variable support #698

Open
wants to merge 4 commits into
base: master
Choose a base branch
from

Conversation

jannickj
Copy link

@jannickj jannickj commented Oct 5, 2020

Fixes: #677

Allows for enviroment variables to be part of a cli setup the order is Args > Env > Default
NB. Currently does not support sequences (Future work - will instead crash with a helpful error msg)

I also changed 0/1 to be parsable booleans as that's the language agnotisc / most common way to set booleans via environment variables.

@jannickj jannickj force-pushed the add-environment-variable-support branch from 992a756 to b954fbd Compare October 5, 2020 18:36
@jannickj
Copy link
Author

Hello?

@jannickj
Copy link
Author

👋

@jannickj
Copy link
Author

🌧️
🚶‍♂️

@SommerEngineering
Copy link

@ericnewton76 Could this be part of the 2.9.0 release, please? 🙂

@jannickj
Copy link
Author

jannickj commented Feb 9, 2021

🛌

@jannickj
Copy link
Author

🌤️

@jannickj
Copy link
Author

🌞

@BoBiene
Copy link

BoBiene commented Apr 15, 2021

@ericnewton76 & @moh-hassan would be great to get this feature :)

@johnjaylward
Copy link
Contributor

It looks like @moh-hassan has left this project (he's not listed in the org anymore), and @ericnewton76 has been inactive on GitHub since November. It effectively looks like this project has been abandoned, which is a shame :(

@BoBiene
Copy link

BoBiene commented Apr 16, 2021

So there is no one able to merge pullrequests to this repo?
@gsscoder you are listed as contact, are you able to maintain this great project?

@jannickj
Copy link
Author

🌇

@jannickj
Copy link
Author

jannickj commented Aug 9, 2021

🏙️

@jannickj
Copy link
Author

🦗

@jannickj
Copy link
Author

💀

@devedse
Copy link

devedse commented Apr 18, 2022

I created a NuGet package based on your branch:
https://www.nuget.org/packages/CommandLineParserWithEnvironmentVariables/2.8.0

@devedse
Copy link

devedse commented Apr 18, 2022

@jannickj , I did find one issue though. If Required = true and you did provide the value for the variable through an EnvironmentVariable it's still showing the error that a required-option is missing.

Not sure if that is desirable.

@jannickj
Copy link
Author

@devedse thx :D, also would say that is in my view expected behavior :)

@devedse
Copy link

devedse commented Apr 19, 2022

@jannickj , the thing is that this results in me having to check all variables in the code to see if they are not null. Even though it is required, thus, should be provided.

But I'm not sure what the best solution would be.

@jannickj
Copy link
Author

jannickj commented Apr 19, 2022

yeah it's a bit annoying the way to get around it is to just set it equal to null!
ex.

[Option("host",
	HelpText = "The host for the client",
	Default = "localhost:80")]
public string Host { get; set; } = null!;

@devedse
Copy link

devedse commented Apr 19, 2022

That's not really what I mean. What I mean is that if you set Required = true, you need to provide the variable by passing in the flag.

For example, let's assume we have:

[Option("data",
        Required = true,
	HelpText = "The data",
        Env = "DATA"
)]
public string Data { get; set; } = null!;
#This would work:
testapp --data 123
#This would not work:
SET DATA=123
testapp

#Now we're getting the message that --data is missing.

@jannickj
Copy link
Author

jannickj commented Apr 19, 2022

right... I wasn't clear in my first message then, I tried to argue that it could make sense since required sort of strongly requests that you set the field. That said I understand what you mean and the more I think about the more I'm convinced that it's the behavior you expect.

@devedse
Copy link

devedse commented Apr 19, 2022

That's what I thought first as well. But I'm doubting it a bit now since if you look at it from the consumer end it just requires the data. It doesn't matter where it comes from. I can't think of a scenario where actually passing it as a flag is required if there is also an environment variable.

But either way is fine. I'll just work with the way is implemented now.

@jannickj
Copy link
Author

jannickj commented Apr 19, 2022

That said I understand what you mean and the more I think about the more I'm convinced that it's the behavior you expect.

just read what I wrote, what I meant to say is that the behavior you (@devedse ) expected is the correct behavior, i.e. required accepts environment variables 😅

@devedse
Copy link

devedse commented Apr 21, 2022

@jannickj , are you planning on implementing this?

@jannickj
Copy link
Author

@devedse probably not, I made this PR 1.5 years ago, so at this point I'm okay with it, if someone revived this git-repo then maybe but not on a half-dead project.

@devedse
Copy link

devedse commented Apr 21, 2022

I'll see if I can make a quick fix then. I'll just release a new version of my NuGet Package

@jannickj
Copy link
Author

💪

@devedse
Copy link

devedse commented Apr 21, 2022

I think I fixed it. I actually moved all the code for setting the Value from the BuildMutable Method to the OptionMapper.MapValues as this is probably the place where we'd want to add this stuff.
devedse@9ef1591

I also wrote some test scenario's that test Required = true in combination with providing the value through Environment values or arguments or neither.

I've released this version to NuGet:
https://www.nuget.org/packages/CommandLineParserWithEnvironmentVariables/2.8.1

@devedse
Copy link

devedse commented Apr 22, 2022

Feel free to do a code review, Incase I missed something.

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

Successfully merging this pull request may close these issues.

Can I read options from Environment variables?
5 participants