2 people like it.

Argu .env integration

This example shows how to use .env files together with the awesome command line parsing capabilities of Argu.

 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: 
open dotenv.net
open Argu

type DotenvReader(?trimValues) =
    do DotEnv.Load(DotEnvOptions(?trimValues = trimValues))
    let envVars = DotEnv.Read()
    interface IConfigurationReader with
        member _.Name = "dotenv"
        member _.GetValue key = envVars.[key]

type Args =
    // Other arguments here...
    | [<NoCommandLine; CustomAppSettings "API_KEY">] ApiKey of string
    | [<NoCommandLine; CustomAppSettings "API_SECRET">] ApiSecret of string
    interface IArgParserTemplate with
        member self.Usage =
            // for brevity
            ""

let getToken (apiKey, apiSecret) =
    failwith "function that needs an API key and an API secret and returns a bearer token."

let main argv =
    let parser = ArgumentParser.Create(checkStructure = false, errorHandler = ProcessExiter())
    let result = parser.Parse(argv, DotenvReader(trimValues = true))

    let apiKey = result.GetResult <@ ApiKey @>
    let apiSecret = result.GetResult <@ ApiSecret @>
    let token = getToken (apiKey, apiSecret)

    // ...

    0
namespace dotenv
namespace dotenv.net
namespace Argu
Multiple items
type DotenvReader =
  interface IConfigurationReader
  new : ?trimValues:bool -> DotenvReader

--------------------
new : ?trimValues:bool -> DotenvReader
val trimValues : bool option
type DotEnv =
  static member AutoConfig : ?levelsToSearch:int -> bool
  static member Config : options:DotEnvOptions -> unit
  static member Fluent : unit -> DotEnvOptions
  static member Load : ?options:DotEnvOptions -> unit
  static member Read : ?options:DotEnvOptions -> IDictionary<string, string>
DotEnv.Load(?options: DotEnvOptions) : unit
Multiple items
type DotEnvOptions =
  new : ?ignoreExceptions:bool * ?envFilePaths:IEnumerable<string> * ?encoding:Encoding * ?trimValues:bool * ?overwriteExistingVars:bool * ?probeForEnv:bool * ?probeLevelsToSearch:int -> DotEnvOptions
  member Encoding : Encoding with get, set
  member EnvFilePaths : IEnumerable<string> with get, set
  member IgnoreExceptions : bool with get, set
  member Load : unit -> unit
  member OverwriteExistingVars : bool with get, set
  member ProbeForEnv : bool with get, set
  member ProbeLevelsToSearch : int with get, set
  member Read : unit -> IDictionary<string, string>
  member TrimValues : bool with get, set
  ...

--------------------
DotEnvOptions(?ignoreExceptions: bool,?envFilePaths: System.Collections.Generic.IEnumerable<string>,?encoding: System.Text.Encoding,?trimValues: bool,?overwriteExistingVars: bool,?probeForEnv: bool,?probeLevelsToSearch: int) : DotEnvOptions
val envVars : System.Collections.Generic.IDictionary<string,string>
DotEnv.Read(?options: DotEnvOptions) : System.Collections.Generic.IDictionary<string,string>
type IConfigurationReader =
  interface
    abstract member GetValue : key:string -> string
    abstract member Name : string
  end
val key : string
Multiple items
type NoCommandLineAttribute =
  inherit Attribute
  new : unit -> NoCommandLineAttribute

--------------------
new : unit -> NoCommandLineAttribute
Multiple items
type CustomAppSettingsAttribute =
  inherit Attribute
  new : name:string -> CustomAppSettingsAttribute
  member Name : string

--------------------
new : name:string -> CustomAppSettingsAttribute
union case Args.ApiKey: string -> Args
Multiple items
val string : value:'T -> string

--------------------
type string = System.String
union case Args.ApiSecret: string -> Args
type IArgParserTemplate =
  interface
    abstract member Usage : string
  end
val self : Args
val getToken : apiKey:'a * apiSecret:'b -> 'c
val apiKey : 'a
val apiSecret : 'b
val failwith : message:string -> 'T
val main : argv:string [] -> int
val argv : string []
val parser : ArgumentParser<Args>
Multiple items
type ArgumentParser =
  private new : argInfo:UnionArgInfo * _programName:string * helpTextMessage:string option * _usageStringCharacterWidth:int * errorHandler:IExiter -> ArgumentParser
  abstract member Accept : visitor:IArgumentParserVisitor<'R> -> 'R
  member GetArgumentCases : unit -> ArgumentCaseInfo list
  member GetSubCommandParsers : unit -> ArgumentParser list
  member PrintCommandLineSyntax : ?programName:string * ?usageStringCharacterWidth:int -> string
  member PrintUsage : ?message:string * ?programName:string * ?hideSyntax:bool * ?usageStringCharacterWidth:int -> string
  member ErrorHandler : IExiter
  member HelpDescription : string
  member HelpFlags : string list
  member HelpTextMessage : string option
  ...

--------------------
type ArgumentParser<'Template (requires 'Template :> IArgParserTemplate)> =
  inherit ArgumentParser
  new : ?programName:string * ?helpTextMessage:string * ?usageStringCharacterWidth:int * ?errorHandler:IExiter * ?checkStructure:bool -> ArgumentParser<'Template>
  private new : argInfo:UnionArgInfo * _programName:string * helpTextMessage:string option * _usageStringCharacterWidth:int * errorHandler:IExiter -> ArgumentParser<'Template>
  override Accept : visitor:IArgumentParserVisitor<'a1> -> 'a1
  member GetArgumentCaseInfo : ctorExpr:Expr<('Fields -> 'Template)> -> ArgumentCaseInfo
  member GetArgumentCaseInfo : value:'Template -> ArgumentCaseInfo
  member GetSubCommandParser : expr:Expr<(ParseResults<'SubTemplate> -> 'Template)> -> ArgumentParser<'SubTemplate> (requires 'SubTemplate :> IArgParserTemplate)
  member GetTag : value:'Template -> int
  member Parse : ?inputs:string [] * ?configurationReader:IConfigurationReader * ?ignoreMissing:bool * ?ignoreUnrecognized:bool * ?raiseOnUsage:bool -> ParseResults<'Template>
  member ParseCommandLine : ?inputs:string [] * ?ignoreMissing:bool * ?ignoreUnrecognized:bool * ?raiseOnUsage:bool -> ParseResults<'Template>
  ...

--------------------
new : ?programName:string * ?helpTextMessage:string * ?usageStringCharacterWidth:int * ?errorHandler:IExiter * ?checkStructure:bool -> ArgumentParser<'Template>
static member ArgumentParser.Create : ?programName:string * ?helpTextMessage:string * ?usageStringCharacterWidth:int * ?errorHandler:IExiter * ?checkStructure:bool -> ArgumentParser<#IArgParserTemplate>
Multiple items
type ProcessExiter =
  interface IExiter
  new : unit -> ProcessExiter
  new : colorizer:(ErrorCode -> ConsoleColor option) -> ProcessExiter
  new : colorizerOption:(ErrorCode -> ConsoleColor option) option -> ProcessExiter

--------------------
new : unit -> ProcessExiter
new : colorizerOption:(ErrorCode -> System.ConsoleColor option) option -> ProcessExiter
new : colorizer:(ErrorCode -> System.ConsoleColor option) -> ProcessExiter
val result : ParseResults<Args>
member ArgumentParser.Parse : ?inputs:string [] * ?configurationReader:IConfigurationReader * ?ignoreMissing:bool * ?ignoreUnrecognized:bool * ?raiseOnUsage:bool -> ParseResults<'Template>
val apiKey : string
member ParseResults.GetResult : expr:Quotations.Expr<('Fields -> 'Template)> * ?defaultValue:'Fields * ?source:ParseSource -> 'Fields
member ParseResults.GetResult : expr:Quotations.Expr<'Template> * ?defaultValue:'Template * ?source:ParseSource -> 'Template
val apiSecret : string
val token : obj
Raw view Test code New version

More information

Link:http://fssnip.net/81L
Posted:1 month ago
Author:toburger
Tags: argument parsing , argu , dotenv