Skip to content

Instantly share code, notes, and snippets.

@TeaDrivenDev
Last active September 25, 2016 16:57
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save TeaDrivenDev/155b79682b196a73ec662cd641be26c3 to your computer and use it in GitHub Desktop.
Save TeaDrivenDev/155b79682b196a73ec662cd641be26c3 to your computer and use it in GitHub Desktop.
Simple example how to hide Argu command line parsing behind an interface for use from .NET languages other than F#
open Argu
type CliArguments =
| Yes
| Name of string
| Value of int
with interface IArgParserTemplate with
member s.Usage =
match s with
| Yes -> "specify"
| Name _ -> "specify a name"
| Value _ -> "specify a value"
type Parameters =
{
Yes : bool
Name : string
Value : int
}
type ICliArgumentParser =
abstract member Parse : string [] -> Parameters
abstract member Help : string
type CliArgumentParser() as this =
let parser = ArgumentParser.Create<CliArguments>()
interface ICliArgumentParser with
member __.Parse(args : string[]) =
let results = parser.Parse args
{
Yes = results.Contains <@ Yes @>
Name =
match results.TryGetResult <@ Name @> with
| Some name -> name
| None -> "defaultName"
Value =
match results.TryGetResult <@ Value @> with
| Some value -> value
| None -> 5
}
member __.Help = parser.PrintUsage()
@baronfel
Copy link

baronfel commented Sep 25, 2016

There's a helper in Argu for doing the 'get with this default' pattern:

Name = results.GetResult(<@ Name @>, "defaultName") from

https://github.com/fsprojects/Argu/blob/master/src/Argu/ParseResults.fs#L112-112

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment