4 people like it.

Use Argu to access .NET Core hosted configuration (appSettings.json)

Configure Argu to read from hosted Microsoft.Extensions.Configuration and pass a ParseResult<'T> through dependency injection. This allows you to combine Argu's full-featured command-line handling and strong typing with M.E.C's multiple sources (appSettings..json, environment variables, etc).

 1: 
 2: 
 3: 
 4: 
 5: 
 6: 
 7: 
 8: 
 9: 
10: 
11: 
12: 
13: 
14: 
15: 
16: 
17: 
18: 
19: 
20: 
21: 
22: 
23: 
24: 
25: 
26: 
27: 
28: 
29: 
30: 
31: 
32: 
33: 
34: 
35: 
36: 
37: 
38: 
39: 
40: 
41: 
42: 
43: 
44: 
45: 
46: 
47: 
48: 
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<IConfigurationReader, HostedConfigurationReader>()
        this.AddSingleton<ParseResults<'T>>(fun services ->
            services.GetRequiredService<IConfiguration>()
            |> 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
    | [<CustomAppSettings "myService:url">] 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<Args>) =
    let file = args.GetResult Args.File
    let url = args.GetResult Args.Url

// App startup config:
type Startup() =
    member this.ConfigureServices(services: IServiceCollection) =
        services.AddArgu<Args>()
        |> ignore
namespace Microsoft
Multiple items
type HostedConfigurationReader =
  interface obj
  new : config:obj -> HostedConfigurationReader
  override GetValue : k:'a -> 'b
  override Name : string

Full name: Script.HostedConfigurationReader

--------------------
new : config:obj -> HostedConfigurationReader
val config : obj
val this : HostedConfigurationReader
override HostedConfigurationReader.GetValue : k:'a -> 'b

Full name: Script.HostedConfigurationReader.GetValue
val k : 'a
override HostedConfigurationReader.Name : string

Full name: Script.HostedConfigurationReader.Name
val not : value:bool -> bool

Full name: Microsoft.FSharp.Core.Operators.not
type Args =
  | File of string
  | Url of string
  interface obj
  override Usage : string

Full name: Script.Args
union case Args.File: string -> Args
Multiple items
val string : value:'T -> string

Full name: Microsoft.FSharp.Core.Operators.string

--------------------
type string = System.String

Full name: Microsoft.FSharp.Core.string
union case Args.Url: string -> Args
val this : Args
override Args.Usage : string

Full name: Script.Args.Usage
Multiple items
type MyService =
  new : args:obj -> MyService

Full name: Script.MyService

--------------------
new : args:obj -> MyService
val args : obj
val file : obj
val url : obj
Multiple items
type Startup =
  new : unit -> Startup
  member ConfigureServices : services:'a -> unit

Full name: Script.Startup

--------------------
new : unit -> Startup
val this : Startup
member Startup.ConfigureServices : services:'a -> unit

Full name: Script.Startup.ConfigureServices
val services : 'a
val ignore : value:'T -> unit

Full name: Microsoft.FSharp.Core.Operators.ignore
Raw view New version

More information

Link:http://fssnip.net/7XP
Posted:3 months ago
Author:Loïc Denuzière
Tags: argument parsing , asp.net , configuration