Last active
September 25, 2016 16:57
-
-
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#
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
There's a helper in Argu for doing the 'get with this default' pattern:
Name = results.GetResult(<@ Name @>, "defaultName")
fromhttps://github.com/fsprojects/Argu/blob/master/src/Argu/ParseResults.fs#L112-112