Skip to content

Commit

Permalink
Implement argument parsing
Browse files Browse the repository at this point in the history
  • Loading branch information
milandjurdjevic committed Jul 3, 2024
1 parent 130a339 commit 94da520
Show file tree
Hide file tree
Showing 4 changed files with 37 additions and 6 deletions.
1 change: 1 addition & 0 deletions src/Analog/Analog.fsproj
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
<ItemGroup>
<PackageReference Include="Argu" Version="6.2.4"/>
<PackageReference Include="Spectre.Console" Version="0.49.1"/>
<PackageReference Include="Spectre.Console.Json" Version="0.49.1"/>
<PackageReference Include="YamlDotNet" Version="15.3.0"/>
</ItemGroup>

Expand Down
3 changes: 1 addition & 2 deletions src/Analog/Parser.fs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
open System
open System.Linq
open System.IO
open System.Text
open System.Text.RegularExpressions

/// <summary>
Expand All @@ -18,7 +17,7 @@ open System.Text.RegularExpressions
/// <param name="pattern">The regular expression used to find matches in the stream.</param>
/// <param name="stream">The stream to be parsed.</param>
let parse (tap: Map<string, string> -> unit) (pattern: string) (stream: Stream) =
use reader = new StreamReader(stream, Encoding.UTF8)
use reader = new StreamReader(stream)

let regex =
Regex(pattern, RegexOptions.Multiline ||| RegexOptions.Compiled, TimeSpan.FromSeconds(5))
Expand Down
37 changes: 34 additions & 3 deletions src/Analog/Program.fs
Original file line number Diff line number Diff line change
@@ -1,4 +1,35 @@

open System.IO
open System.Text.Json
open Analog
open Argu
open Spectre.Console
open Spectre.Console.Json

type Argument =
| [<AltCommandLine("-f"); Mandatory>] File of string
| [<AltCommandLine("-t"); Unique>] Template of string

interface IArgParserTemplate with
member this.Usage =
match this with
| File _ -> "log file path."
| Template _ -> "log file template."

[<EntryPoint>]
let main argv =
0
let main argv =
try
let command = ArgumentParser.Create<Argument>().ParseCommandLine(argv)

let template =
command.TryGetResult Template
|> Option.map (fun template -> Template.configuration |> Map.find template)
|> Option.defaultValue (Template.configuration |> Seq.head |> _.Value)

let render = JsonSerializer.Serialize >> JsonText >> AnsiConsole.Write
let parse = Parser.parse render template.Pattern
command.GetResults File |> Seq.iter (fun file -> File.OpenRead file |> parse)
command |> ignore
0
with :? ArguParseException as e ->
eprintfn $"%s{e.Message}"
1
2 changes: 1 addition & 1 deletion src/Analog/template.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
"type": "string"
}
},
"required": ["regex"]
"required": ["pattern"]
}
}
}

0 comments on commit 94da520

Please sign in to comment.