SkillAgentSearch skills...

Argu.MicrosoftExtensions

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

Install / Use

/learn @Tarmil/Argu.MicrosoftExtensions
About this skill

Quality Score

0/100

Supported Platforms

Universal

README

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

Related Skills

View on GitHub
GitHub Stars12
CategoryDevelopment
Updated2y ago
Forks0

Languages

F#

Security Score

60/100

Audited on Jan 24, 2024

No findings