Created
November 24, 2017 20:12
-
-
Save ctaggart/3a890c7d6c3d2f3a46f106b61fdea053 to your computer and use it in GitHub Desktop.
Yargs from Fable
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
module App | |
// a port of the last example: Yargs is here to help you... | |
// https://github.com/yargs/yargs/blob/master/docs/examples.md#yargs-is-here-to-help-you | |
// It could the number of lines in a file. | |
open Yargs | |
open Fable.Core | |
open System.Text.RegularExpressions | |
let argv = | |
yargs | |
.usage("Usage: $0 <command> [options]") | |
.command(U2.Case1 "count", "Count the lines in a file") | |
.example("$0 count -f foo.js", "count the lines in the given file") | |
.alias("f", U2.Case1 "file") | |
.nargs("f", 1.) | |
.describe(U2.Case1 "f", "Load a file") | |
.demandOption(U2.Case1 "f", "Please specify a file.") | |
.help("h") | |
.alias("h", U2.Case1 "help") | |
.epilog("copyright 2015") | |
.argv | |
let s = Node.fs.createReadStream(PathLike.ofString(argv.["file"].Value :?> string)) | |
let mutable lines = 1 | |
s.on_data(fun buf -> | |
let s = | |
match buf with | |
| U2.Case1 b -> b.toString() | |
| U2.Case2 s -> s | |
lines <- lines + Regex.Matches(s, "(\n)").Count | |
) |> ignore | |
s.on_end(fun _ -> | |
printfn "%d" lines | |
) |> ignore |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
open Yargs | |
open Fable.Import.JS | |
let argv = yargs.argv | |
let retreat() = printfn "Retreat from the xupptumblers!" | |
let plunder() = printfn "Plunder more riffiwobbles!" | |
match argv.Item "ships", argv.Item "distance" with | |
| Some ships, Some distance -> | |
let ships = parseFloat (ships :?> string) | |
let distance = parseFloat (distance :?> string) | |
if isNaN ships || isNaN distance then | |
retreat() | |
else | |
if ships > 3. && distance < 53.5 then | |
plunder() | |
else | |
retreat() | |
| _ -> retreat() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
dotnet new -i ctaggart.templates | |
dotnet new --list -lang f# | |
dotnet new yargs -n myconsoleapp | |
cd myconsoleapp | |
.\build.ps1 | |
.\start.ps1 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
let argv = | |
yargs | |
.usage("Usage: ts2fable some.d.ts src/Some.fs") | |
.command(U2.Case1 "$0 [files..]", "") | |
.demandOption(U2.Case1 "files", "") | |
.help() | |
.argv | |
let files = argv.["files"].Value :?> string array |> List.ofArray | |
let tsfiles = files |> List.filter (fun s -> s.EndsWith ".ts") | |
let fsfile = files |> List.tryFind (fun s -> s.EndsWith ".fs") | |
match tsfiles.Length, fsfile with | |
| 0, _ -> failwithf "Please provide the path to a TypeScript file" | |
| _, None -> failwithf "Please provide the path to the F# file to be written " | |
| _, Some fsf -> | |
printfn "ts2fable %s" Version.version | |
// validate ts files exist | |
for ts in tsfiles do | |
if fs.existsSync(PathLike.ofString ts) = false then | |
failwithf "TypeScript file not found: %s" ts | |
writeFile tsfiles fsf |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment