-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathCliArguments.fs
57 lines (51 loc) · 2.08 KB
/
CliArguments.fs
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
module CliArguments
open Argu
open Model
open System.Text.RegularExpressions
[<Unique>]
type CliArguments =
| [<AltCommandLine("-s")>]Sheet of document_id:string*sheet_name:string
| [<AltCommandLine("-c")>]Csv of url_or_path:string
| [<AltCommandLine("-f")>]Format of OutputFormat
| [<AltCommandLine("-o")>]OutputDir of directory_path:string
| [<AltCommandLine("-n")>]Name of string
| [<AltCommandLine("-p")>]Convert_Placeholders of regex_pattern:string
| [<Hidden>]Output of folder_path:string
with
interface IArgParserTemplate with
member s.Usage =
match s with
| Sheet _ -> "specify a Google Sheet as input."
| Csv _ -> "specify a online or local cvs file as input."
| Format _ -> "specify the output format."
| OutputDir _ -> "specify the output directory."
| Name _ -> "specify an optional name for the output."
| Convert_Placeholders _ -> "convert placeholders to match the output format."
| Output _ -> ""
let getSheetUrl (d,s) = sprintf "https://docs.google.com/spreadsheets/d/%s/gviz/tq?tqx=out:csv&sheet=%s&headers=0" d s
let validate = function
| { InputUrl = "" } -> "ERROR: missing parameter '--sheet' or '--csv'." |> Error
| o -> o |> Ok
let Parse progName args =
let exiter = ProcessExiter () :> IExiter
let parser = ArgumentParser.Create<CliArguments>
(progName, errorHandler = exiter)
let rec loop o = function
| Sheet (d,s)::t -> (d,s) |> getSheetUrl |> Csv |> (fun h -> h::t) |> loop o
| Csv u::t -> t |> loop {o with InputUrl = u }
| Format f::t -> t |> loop {o with Format = f }
| Output p::t -> t |> loop {o with OutputDir = p }
| OutputDir p::t -> t |> loop {o with OutputDir = p }
| Name f::t -> t |> loop {o with Name = Some f }
| Convert_Placeholders p::t -> t |> loop {o with Placeholders = Regex p|> Some}
| [] -> o
parser.Parse args
|> (fun a -> a.GetAllResults())
|> loop Options.empty
|> validate
|> function
| Ok o -> o
| Error e ->
exiter.Exit
(String.concat "\n" [e;parser.PrintUsage()],
ErrorCode.CommandLine)