<commit_before>﻿namespace CommandLine.FSharp

open System
open System.IO
open System.Globalization
open CommandLine.Core
open CommandLine
open CommandLine.Text

type public ParserConfig = {
    CaseSensitive : bool;
    HelpWriter : TextWriter;
    IgnoreUnknownArguments : bool;
    ParsingCulture : CultureInfo;
}

module ArgParser =
    let private handleUnknownArguments<'a> (result : ParserResult<'a>, ignoreUnknownArguments) =
        if ignoreUnknownArguments then
            let errs = query { for err in result.Errors do
                               where(err.Tag <> ErrorType.UnknownOptionError)
                               select err }
            ParserResult(result.Tag, result.Value, errs, result.VerbTypes)
        else result

    let private displayHelp<'a> (result : ParserResult<'a>, helpWriter: TextWriter option) =
        let write (res : ParserResult<'a>, hw : TextWriter) =
            hw.Write(HelpText.AutoBuild(res)) |> ignore
            res
        match helpWriter with
            | Some(helpWriter) -> write(result, helpWriter)
            | _ -> result

    let private makeParserResult<'a> (parseFunc : unit -> ParserResult<'a>, config : ParserConfig) =
        let writerToOption(hw: TextWriter) =
            match hw with
                | null -> None
                | _ -> Some(hw)
        displayHelp(handleUnknownArguments(parseFunc(), config.IgnoreUnknownArguments), writerToOption(config.HelpWriter))

    let private getComparer config =
        if config.CaseSensitive then StringComparer.Ordinal
        else StringComparer.OrdinalIgnoreCase
  
    let ParseOptions<'a when 'a : (new : unit -> 'a)> (config, args) =
        let parseFunc = fun() -> InstanceBuilder.Build(Func<'a>(fun () -> new 'a()), args, getComparer(config), config.ParsingCulture)
        makeParserResult(parseFunc, config)

//    let ParseVerbs args =
//        args<commit_msg>Refactored with pipe operator.<commit_after>﻿namespace CommandLine.FSharp

open System
open System.IO
open System.Globalization
open CommandLine.Core
open CommandLine
open CommandLine.Text

type public ParserConfig = {
    CaseSensitive : bool;
    HelpWriter : TextWriter;
    IgnoreUnknownArguments : bool;
    ParsingCulture : CultureInfo;
}

module ArgParser =
    let ParseOptions<'a when 'a : (new : unit -> 'a)> (config, args) =
        let getComparer config =
            if config.CaseSensitive then StringComparer.Ordinal
            else StringComparer.OrdinalIgnoreCase
        let filterUnknown (result: ParserResult<'a>) =
            match config.IgnoreUnknownArguments with
                | true -> ParserResult(result.Tag, result.Value, query { for err in result.Errors do
                                                                         where(err.Tag <> ErrorType.UnknownOptionError)
                                                                         select err }, result.VerbTypes)
                | _ -> result
        let displayHelp (result: ParserResult<'a>) =
            match config.HelpWriter with
                | null -> result
                | _ -> config.HelpWriter.Write(HelpText.AutoBuild(result)) |> ignore; result
        InstanceBuilder.Build(Func<'a>(fun () -> new 'a()), args, getComparer(config), config.ParsingCulture)
            |> filterUnknown
            |> displayHelp

//    let ParseVerbs args =
//        args
{'fraction_non_alphanumeric': 0.09085528262799644, 'fraction_numerical': 0.0, 'mean_word_length': 2.2688588007736943, 'pattern_counts': {'":': 0, '<': 18, '<?xml version=': 0, '>': 36, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 6, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5377714', 'n_tokens_mistral': 867, 'n_tokens_neox': 862, 'n_words': 225}