swift-argument-parser icon indicating copy to clipboard operation
swift-argument-parser copied to clipboard

Option parsing from environment variables

Open KyleLeneau opened this issue 4 years ago • 9 comments

It would be super nice/helpful to parse an options value from an Environment variable value as well taking what is passed in the command line call as the priority over the ENV variable. For CLI's that run in CI or automated environments this is super helpful as it would allow not having to print the value in the ENV variable and could keep it hidden. Not only that but it would be very convenient for repeated uses on commands!

import ArgumentParser

struct MyCommand: ParsableCommand {
    @Option(name: .shortAndLong, help: "The API Secret.", env: "SERVICE_API_SECRET")
    var secret: String
}

Usage: mycommand --secret QWERTY12345 or mycommand (where SERVICE_API_SECRET='QWERTY12345'

KyleLeneau avatar Feb 28 '20 01:02 KyleLeneau

Yes. We have been working on this already, and we’ll open a PR within the next week or two. There are still a lot of open questions about how to do this right, and we’d love to get feedback from everyone.

danieleggert avatar Feb 28 '20 21:02 danieleggert

Awesome, I am happy to provide some feedback on a PR!

KyleLeneau avatar Feb 28 '20 21:02 KyleLeneau

@danieleggert Any update on the progress of this?

KyleLeneau avatar Apr 03 '20 21:04 KyleLeneau

@danieleggert Any update on the progress of this?

Please check out #109 — it’s almost done.

danieleggert avatar Apr 05 '20 15:04 danieleggert

any updates?

griffin-stewie avatar Jan 31 '21 02:01 griffin-stewie

still waiting😭

kojirou1994 avatar Apr 19 '21 18:04 kojirou1994

I this still on the roadmap?

philprime avatar Jul 29 '21 12:07 philprime

@rauhul, do you have a preference on @KyleLeneau's proposal in this issue vs @danieleggert implementation in https://github.com/apple/swift-argument-parser/pull/109 vs something new like auto-magically populate values based on command names and argument names. For example, if we have a root command tart and sub-command push with a --populate-cache flag then set it automatically to a value of TART_PUSH_POPULATE_CACHE if defined?

fkorotkov avatar Feb 20 '24 14:02 fkorotkov

I was thinking about a different approach where the parsing pipeline is composed of ArgumentValueProviders, the command line would be one such provider. I haven't fleshed out the idea but the benefit is that users could add their own argument providers depending on their use cases.

e.g. we could provide support out of the box for json config files, environment, .env files, NSUserDefaults, etc...

rauhul avatar Feb 20 '24 17:02 rauhul