Argu.MicrosoftExtensions
source link: https://github.com/Tarmil/Argu.MicrosoftExtensions
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.
Argu.MicrosoftExtensions
This library helps integrate Argu, the F# argument and configuration parsing library, into Microsoft.Extensions hosted applications. It provides:
- Injecting
ParseResults
into the dependency injection; - Parsing configuration items (from eg
appSettings.json
) with Argu.
Injecting ParseResults
Add Argu parse results to the dependency injection using services.AddArgu<Args>()
:
type Args = | Username of string | Password of string type Startup() = member this.ConfigureServices(services: IServiceCollection) = services.AddArgu<Args>()
This method takes most of the same optional arguments as ArgumentParser.Parse()
:
services.AddArgu<Args>(ignoreMissing = true, raiseOnUsage = false)
Alternately, it can take a function of type IConfigurationReader -> ParseResults<Args>
:
services.AddArgu<Args>(fun config -> ArgumentParser.Create<Args>().Parse(configurationReader = config))
You can now use the injected parse results:
type MyService(args: ParseResults<Args>) = let username = args.GetResult Username let password = args.GetResult Password
Parsing configuration
The argument parser will also parse values from the injected configuration.
// appSettings.json { "username": "johndoe", "password": "p455w0rd" }
type Args = | Username of string | Password of string
To use nested keys, use CustomAppSettings
with colons as separators.
// appSettings.json { "credentials": { "username": "johndoe", "password": "p455w0rd" } }
type Args = | [<CustomAppSettings "credentials:username">] Username of string | [<CustomAppSettings "credentials:password">] Password of string
To disable reading arguments from the configuration, pass useConfiguration = false
to services.AddArgu()
.
Recommend
About Joyk
Aggregate valuable and interesting links.
Joyk means Joy of geeK