Created
April 17, 2015 20:11
Using F# 2.0 Powerpack ArgParser from C#
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
using System; | |
using System.Collections.Generic; | |
using System.Linq; | |
using System.Text; | |
using Microsoft.FSharp.Core; | |
using Microsoft.FSharp.Text; | |
namespace fsgetopt | |
{ | |
class Program | |
{ | |
static void Main(string[] args) | |
{ | |
Action<string> compile = (s => Console.WriteLine("Compiling {0}...", s)); | |
var outputName = "a.out"; | |
var verbose = new FSharpRef<bool>(false); | |
var warningLevel = 0; | |
ArgInfo[] specs = | |
{ | |
new ArgInfo("-o", | |
ArgType.String (FuncConvert.ToFSharpFunc<string> (x => outputName=x)), | |
"Name of the output"), | |
new ArgInfo("-v", | |
ArgType.Set (verbose), | |
"Display additional information"), | |
new ArgInfo("--warn", | |
ArgType.Int (FuncConvert.ToFSharpFunc<int> (x => warningLevel=x)), | |
"Set warning level"), | |
new ArgInfo("--", | |
ArgType.Rest (FuncConvert.ToFSharpFunc<string> (x => Console.WriteLine("rest has {0}", x))), | |
"Stop parsing command line"), | |
}; | |
var after = FuncConvert.ToFSharpFunc<string>(compile); | |
var opt1 = new FSharpOption<FSharpFunc<string, Unit>>(after); | |
var opt2 = new FSharpOption<string>("Usage options are:"); | |
ArgParser.Parse(specs, opt1, opt2); | |
Console.WriteLine("outputName = {0}", outputName); | |
Console.WriteLine("Verbose = {0}", verbose.Value); | |
Console.WriteLine("Warning level = {0}", warningLevel); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment