open Microsoft.Extensions.Configuration open Microsoft.Extensions.DependencyInjection open Argu type HostedConfigurationReader(config: IConfiguration) = interface IConfigurationReader with member this.GetValue(k) = config.[k] member this.Name = ".NET Core hosted configuration" type IServiceCollection with member this.AddArgu<'T when 'T : not struct and 'T :> IArgParserTemplate>(configure: IConfigurationReader -> ParseResults<'T>) = this.AddSingleton() this.AddSingleton>(fun services -> services.GetRequiredService() |> HostedConfigurationReader |> configure) member this.AddArgu<'T when 'T : not struct and 'T :> IArgParserTemplate>() = this.AddArgu<'T>(fun config -> ArgumentParser.Create<'T>().Parse(configurationReader = config)) //// Example use: type Args = // Passed as --file XXX on the CLI // or { "file": XXX } in appSettings.json // or FILE environment variable | File of string // Passed as --url on the CLI // or { "myService": { "url": XXX } } in appSettings.json // or MYSERVICE__URL envirenment variable | [] Url of string interface IArgParserTemplate with member this.Usage = match this with | File _ -> "a file." | Url _ -> "the url of my service." // Service using it: type MyService(args: ParseResults) = let file = args.GetResult Args.File let url = args.GetResult Args.Url // App startup config: type Startup() = member this.ConfigureServices(services: IServiceCollection) = services.AddArgu() |> ignore