Navigation Menu

Skip to content

Instantly share code, notes, and snippets.

@leandrosilva
Created November 24, 2010 00:46
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save leandrosilva/712878 to your computer and use it in GitHub Desktop.
Save leandrosilva/712878 to your computer and use it in GitHub Desktop.
Using F# to parse command line options with Mono.Options
Sample code on how to parse command line options with Mono.Options.
Mono.Options can be downloaded and compiled from:
https://github.com/mono/mono/tree/master/mcs/class/Mono.Options/Mono.Options
#light
open System
open Mono.Options
type Options() =
let mutable m_help = false
let mutable m_volume = null
let mutable m_fix = false
let mutable m_logSize = 0
member this.Help with get() = m_help
and set newHelp = m_fix <- newHelp
member this.Volume with get() = m_volume
and set newVolume = m_volume <- newVolume
member this.Fix with get() = m_fix
and set newFix = m_fix <- newFix
member this.LogSize with get() = m_logSize
and set newLogSize = m_logSize <- newLogSize
[<EntryPoint>]
let main (args: string[]) =
Console.WriteLine("Checks a disk and displays a status report.\n")
let options = new Options()
let optionSet = new OptionSet()
optionSet.Add("help", "Show help message for usage",
fun v -> options.Help <- true) |> ignore
optionSet.Add("volume=", "REQUIRED Specifies the drive letter.",
fun v -> options.Volume <- v) |> ignore
optionSet.Add("f|F", "Fixes error on the disk",
fun v -> options.Fix <- true) |> ignore
optionSet.Add("l=", "Changes the log file size to the specified number of kilobytes",
fun v -> options.LogSize <- int v) |> ignore
optionSet.Parse(args) |> ignore
if options.Help || options.Volume = null then
Console.WriteLine("Usage message:\n")
optionSet.WriteOptionDescriptions(Console.Error)
Environment.Exit(-1)
else
Console.WriteLine("Received options:\n")
Console.WriteLine("- volume = {0}", options.Volume)
Console.WriteLine("- fix = {0}", options.Fix)
Console.WriteLine("- logSize = {0}", options.LogSize)
0
#light
open System
open Mono.Options
type Arguments() =
let optionSet = new OptionSet()
let mutable m_help = false
let mutable m_volume = null
let mutable m_fix = false
let mutable m_logSize = 0
member this.Help with get() = m_help
and set newHelp = m_fix <- newHelp
member this.Volume with get() = m_volume
and set newVolume = m_volume <- newVolume
member this.Fix with get() = m_fix
and set newFix = m_fix <- newFix
member this.LogSize with get() = m_logSize
and set newLogSize = m_logSize <- newLogSize
static member New =
new Arguments()
member this.With (option, description, action) =
optionSet.Add(option, description, fun value -> action this value) |> ignore
this
member this.From (args) =
optionSet.Parse(args) |> ignore
this
member this.WriteOptionDescriptions(output) =
optionSet.WriteOptionDescriptions(output)
[<EntryPointAttribute>]
let main (args) =
Console.WriteLine("Checks a disk and displays a status report.\n")
let arguments = Arguments.New
.With("help", "Show help message for usage",
fun this value -> this.Help <- true)
.With("volume=", "REQUIRED Specifies the drive letter.",
fun this value -> this.Volume <- value)
.With("f|F", "Fixes error on the disk",
fun this value -> this.Fix <- true)
.With("l=", "Changes the log file size to the specified number of kilobytes",
fun this value -> this.LogSize <- int value)
.From(args)
if arguments.Help || arguments.Volume = null then
Console.WriteLine("Usage message:\n")
arguments.WriteOptionDescriptions(Console.Error)
Environment.Exit(-1)
else
Console.WriteLine("Received options:\n")
Console.WriteLine("- volume = {0}", arguments.Volume)
Console.WriteLine("- fix = {0}", arguments.Fix)
Console.WriteLine("- logSize = {0}", arguments.LogSize)
0
@samal-rasmussen
Copy link

Thanks for this. I was tearing my hair out trying to figure out the syntax for F#.

I simplified it a bit further I think, by using local mutable variables. No need for defining a type to hold the variables. Short syntax example:

let help = ref false
let optionSet = new OptionSet()
optionSet.Add("h|help", "Show help message for usage", fun v -> help := true) |> ignore
optionSet.Parse(argv)
if !help then 
    optionSet.WriteOptionDescriptions (System.Console.Out)
else ...

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