-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathexample.fsx
More file actions
58 lines (48 loc) · 1.71 KB
/
Copy pathexample.fsx
File metadata and controls
58 lines (48 loc) · 1.71 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
#r "nuget: System.Text.Json, 10.0.0-preview.2.25163.2"
open System
open System.Text.Json
open System.IO
type Car =
{ Make: string
Model: string
Year: int
Colour: string }
// Function to parse command-line arguments like --colour, --make, --model
let parseArguments (args: string[]) =
let tryFindArg flag =
args
|> Array.tryFindIndex (fun arg -> arg = flag)
|> Option.bind (fun index ->
if index + 1 < args.Length then
Some args.[index + 1]
else
None)
let colour = tryFindArg "--colour" |> Option.defaultValue ""
let make = tryFindArg "--make" |> Option.defaultValue ""
let model = tryFindArg "--model" |> Option.defaultValue ""
colour, make, model
let Json: Stream = File.OpenRead("./cars.json")
let options = JsonSerializerOptions(PropertyNameCaseInsensitive = true)
try
// Deserialize cars from the JSON file
let cars: List<Car> =
Json
|> fun stream -> JsonSerializer.DeserializeAsync<List<Car>>(stream, options)
|> fun valueTask -> valueTask.AsTask()
|> Async.AwaitTask
|> Async.RunSynchronously
// Parse the arguments from the script
let args = fsi.CommandLineArgs
let colour, make, model = parseArguments args
// Filter the list of cars
let filteredCars =
cars
|> Seq.filter (fun car ->
(car.Colour = colour || colour = "")
&& (car.Make = make || make = "")
&& (car.Model = model || model = ""))
// Print the filtered results
filteredCars
|> Seq.iter (fun car -> printfn "%s %s %d %s" car.Make car.Model car.Year car.Colour)
with ex ->
printfn "An error occurred: %s" ex.Message