Last active
October 10, 2021 00:31
-
-
Save callmekohei/d5f62bd530f4c039081cd736a5d5e8de to your computer and use it in GitHub Desktop.
fsharp console app sample
This file contains hidden or 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 Program | |
(* | |
fsharp console app sample with System.CommandLine | |
require: dotnet add package system.commandline | |
e.g. | |
dotnet run -- --opdir ./hello --opfile ./hello/world.csv --opint 2021 | |
=> hello world 2022 | |
see also | |
Parse the Command Line with System.CommandLine | |
us https://docs.microsoft.com/en-us/archive/msdn-magazine/2019/march/net-parse-the-command-line-with-system-commandline | |
jp https://docs.microsoft.com/ja-jp/archive/msdn-magazine/2019/march/net-parse-the-command-line-with-system-commandline | |
*) | |
open System.IO | |
open System.CommandLine | |
open System.CommandLine.Invocation | |
[<EntryPoint>] | |
let main args = | |
let rootCommand = RootCommand() | |
rootCommand.Description <- "fsharp console app : happy new year!" | |
// read as DirectoryInfo, show error hint if not exists folder | |
let opFileDir = (new Option<DirectoryInfo>("--opdir")).ExistingOnly() | |
rootCommand.AddOption opFileDir | |
// read as FileInfo, show error hint if not exists file | |
let opFileInfo = (new Option<FileInfo>("--opfile")).ExistingOnly() | |
rootCommand.AddOption opFileInfo | |
// read as Int | |
let opInt = new Option<int>("--opint") | |
rootCommand.AddOption opInt | |
rootCommand.Handler <- CommandHandler.Create( | |
fun (opint:int) (opdir:DirectoryInfo) (opfile:FileInfo) -> | |
printfn "%s %s %i" (opdir.Name) (opfile.OpenText().ReadLine()) (opint + 1) | |
) | |
rootCommand.Invoke args |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment