Parsing command line arguments Usage:



// Usage: dndgenerate /e [emhd] [0-9+]
//          dndgenerate /e d 4 4 5 4
// /e   Generate Encounter. Parameter can be one of
//          e - Easy
//          m - Medium
//          h - Hard
//          d - Deadly
//      As well as a list of heroes by their levels
//
// Example:  dndgenerate /e m 4 4 5 4
//      Generate a medium encounter for three level 4 and one level 5 adventurers.


The code to make it happen:



type DifficultyOption = Easy | Medium | Hard | Deadly

type EncounterOptions = {
    difficulty: DifficultyOption option
    heroes: int list
}

let (|Level|_|) (str: string) =
    let mutable intValue = 0

    if System.Int32.TryParse(str, &amp;intValue)
        then match intValue with
             | x when x &lt; 0 -&gt; None
             | x when x &gt; 20 -&gt; None
             | x -&gt; Some x
        else None



let (|Difficulty|_|) (str: string) =
    match str with
    | "e" | "E" -&gt; Some(Easy)
    | "m" | "M" -&gt; Some(Medium)
    | "h" | "H" -&gt; Some(Hard)
    | "d" | "D" -&gt; Some(Deadly)
    | x -&gt; None

let isComplete (options:EncounterOptions) =
    not &lt;| options.heroes.IsEmpty &amp;&amp; options.difficulty.IsSome

type VerboseOption = VerboseOutput | TerseOutput

type MiscOptions = {
    verbose: VerboseOption
}

type GeneratorType =
    | E of EncounterOptions
    | Unknown

type ParseMode = TopLevel | Encounter | Plot | Dungeon | Settlement | Error

type CommandLineOptions = {
    misc: MiscOptions
    parseMode: ParseMode
    generatorType: GeneratorType
}

let parseTopLevel arg miscSoFar =
    match arg with
    | "/v" -&gt;
        let newMiscSoFar = { miscSoFar with verbose = VerboseOutput }
        { misc = newMiscSoFar; parseMode = TopLevel; generatorType = Unknown }

    | "/e" -&gt; 
        { misc = miscSoFar; parseMode = Encounter; generatorType = E { difficulty = None; heroes = [] } }

    | x -&gt;
        { misc = miscSoFar; parseMode = Error; generatorType = Unknown }

let parseEncounter arg miscSoFar encounterGenerator =
    match arg with
    | Level x -&gt;
        let builder' = { encounterGenerator with heroes = x :: encounterGenerator.heroes }
        { misc = miscSoFar; parseMode = Encounter; generatorType = E builder' }

    | Difficulty x -&gt;
        let builder' = { encounterGenerator with difficulty = Some x }
        { misc = miscSoFar; parseMode = Encounter; generatorType = E builder' }

    | _ -&gt; 
        { misc = miscSoFar; parseMode = Error; generatorType = E encounterGenerator }

let foldFunction state element =
    match state with
    | { misc = m; parseMode = TopLevel } -&gt;
        parseTopLevel element m

    | { misc = m; parseMode = Encounter; generatorType = E(g) } -&gt;
        parseEncounter element m g

    | { parseMode = Error } -&gt;
        state

    | { misc = m; parseMode = p; generatorType = g } -&gt; 
        printfn "Unexpected constellation of %A %A %A" m p g
        state

let disableIfIncomplete commandLineOptions =
    match commandLineOptions.parseMode with
    | Error -&gt;
        commandLineOptions

    | _ -&gt;
        match commandLineOptions.generatorType with
        | E(encounter) when isComplete encounter -&gt;
            commandLineOptions

        | Unknown -&gt; commandLineOptions

        | _ -&gt; { commandLineOptions with parseMode = Error }

let parseCommandLine args =
    let defaultOptions = { 
        verbose = TerseOutput
    }

    let initialFoldState =
        { misc = defaultOptions; parseMode = TopLevel; generatorType = Unknown }

    let finalFoldState = args |&gt; List.fold foldFunction initialFoldState

    disableIfIncomplete finalFoldState


Based on code from Scott Wlaschin's Parsing command line arguments but extended to include argument groups and error handling. Purposefully avoided FParsec because I'm new to F# and wanted to solve it vanilla-style first.
I am also new to F#, so I have just 2 small annotations:

The function that parses the string to Int32 can be simplified:

match System.Int32.TryParse(str) with
    | (true, x) when x &gt;= 0 &amp;&amp; x &lt;= 20 -&gt; Some x
    | _ -&gt; None


The last case of the function parseTopLevel should be _ -&gt; instead of x -&gt; because the value is not used ('_' expresses exactly that)