Skip to content

Feature idea: Declarative fall-back to environment variables including automatic --help #143

Open
@bartelink

Description

@bartelink

TL;DR add a [<AltEnvVar "envvarname">] or similar attribute, facilitating ability to supply values via environment variables including integrating into usage text

CURRENT

In some layout conventions, it's established practice to have required settings generally be supplied via environment variables, but admitting overrides in a local context via commandline arguments. This yields a clean way to define all configurable settings for a given program

In this mode, Parameters like:

    type Parameters =
        | [<AltCommandLine "-b"; Unique>]   Broker of string
        | [<AltCommandLine "-t"; Unique>]   Topic of string

are processed by a helper that a) accepts the argument if supplied b) otherwise falls back to the environment variable c) provides a message as to the missing argument and related environment variable in the startup failure message if neither are supplied:

    type Arguments(a : ParseResults<Parameters>) =
        member __.Broker = a.TryGetResult Broker |> defaultWithEnvVar "KAFKA_BROKER" "Broker" |> Uri
        member __.Topic =  a.TryGetResult Topic  |> defaultWithEnvVar "KAFKA_TOPIC"  "Topic"

this however brings the burden of keeping the related usage message in sync:

        interface IArgParserTemplate with
            member a.Usage =
                match a with
                | Broker _ -> "specify Kafka Broker, in host:port format. (optional if environment variable KAFKA_BROKER specified)."
                | Topic _ ->  "specify Kafka Topic name. (optional if environment variable KAFKA_TOPIC specified)."

PROPOSAL

the suggestion is to allow the above to be expressed via:

    type Parameters =
        | [<AltEnvVar "KAFKA_BROKER"; AltCommandLine "-b"; Required>] Broker of string
        | [<AltEnvVar "KAFKA_TOPIC";  AltCommandLine "-t"; Required>] Topic of string

(note Required is now possible)

    type Arguments(a : ParseResults<Parameters>) =
        member __.Broker =     a.GetResult Broker |> Uri
        member __.Topic =      a.GetResult Topic

(note no helpers required)

        interface IArgParserTemplate with
            member a.Usage =
                match a with
                | Broker _ ->  "specify Kafka Broker, in host:port format."
                | Topic _ ->   "specify Kafka Topic name."

(note no mention of the env vars)

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions