Skip to content

Integrate Argu (http://fsprojects.github.io/Argu/), the F# argument and configuration parsing library, into Microsoft.Extensions hosted applications.

Notifications You must be signed in to change notification settings

Tarmil/Argu.MicrosoftExtensions

Repository files navigation

Argu.MicrosoftExtensions

Build Nuget

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().

About

Integrate Argu (http://fsprojects.github.io/Argu/), the F# argument and configuration parsing library, into Microsoft.Extensions hosted applications.

Resources

Stars

Watchers

Forks

Packages

No packages published

Languages