FsCli 1.0.1
See the version list below for details.
dotnet add package FsCli --version 1.0.1
NuGet\Install-Package FsCli -Version 1.0.1
<PackageReference Include="FsCli" Version="1.0.1" />
paket add FsCli --version 1.0.1
#r "nuget: FsCli, 1.0.1"
// Install FsCli as a Cake Addin #addin nuget:?package=FsCli&version=1.0.1 // Install FsCli as a Cake Tool #tool nuget:?package=FsCli&version=1.0.1
FsCli
A simple way of dealing with command line arguments for console apps in .NET, designed around F# DUs and Records.
Quickstart for using FsCli
Define your commands
- Create a DU that lists your allowed set of commands:
type Verbs =
| Run of RunCmd
| List of ListCmd
// e.g > myprog list --user "Peter"
// or > myprog run --name "Sophie" --data 69
- Create a record type for each command specifying the options for the command.
You can optionally use description attributes to help generate useful help messages.
open System.ComponentModel
type ListCmd = {
user: string
}
[<Description("Run the thing")>]
type RunCmd = {
[<Description("The name of the thing")>] name: string
[<Description("The value of the thing")>] data: int
}
Parse the commandline
- Pass the commandline args into the
parse
function
The parser returns either a populated variable of a DU value, or an helpful error message for your users.
open FsCli
[<EntryPoint>]
let Main argv =
match Commandline.parse<Verbs> argv with
| Error msg->
printfn "%s" msg
| Ok cmd ->
match cmd with
| Run runcmd -> DoSomething* runcmd
| List listcmd -> ListSomething* listcmd
* DoSomething and ListSomething are your functions for acting on the user's commands
A help message for the above run command would look something like:
USAGE: tkt run --name --data [--help]
A description for the Run verb
OPTIONS:
--name The name of the thing (String)
--data The value of the thing (Int32)
--help display this list of options
Run the thing
Some extra options
There are a few extras and pointers that have helped me:
Optional Arguments
Use an option type to make arguments optional. If you do this then the help with show the argument in square brackets
type ListCmd = {
user: string option
}
// USAGE: tkt list [--user] [--help]
Argument options' types
Option values can be any simple type (e.g. string, int, double) and the parsing with accept anything that works for the types ...Parse(x)
method.
type ListCmd = {
user: string option
max: int option
}
// USAGE: myprog list [--user] [--max] [--help]
// ...
// --name (String)
// --max (Int)
Commands with a subject
Sometime the commands I make need a subject. For instance:
myprog create admin --name Peter --title programmer
I can nominate a command subject with the Key
attribute:
open System.ComponentModel
type CreateCmd ={
[<DataAnnotations.Key>]
usertype: string
name: string
title: string option
}
// USAGE: myprog create usertype --name [--title] [--help]
Product | Versions Compatible and additional computed target framework versions. |
---|---|
.NET | net6.0 is compatible. net6.0-android was computed. net6.0-ios was computed. net6.0-maccatalyst was computed. net6.0-macos was computed. net6.0-tvos was computed. net6.0-windows was computed. net7.0 was computed. net7.0-android was computed. net7.0-ios was computed. net7.0-maccatalyst was computed. net7.0-macos was computed. net7.0-tvos was computed. net7.0-windows was computed. net8.0 was computed. net8.0-android was computed. net8.0-browser was computed. net8.0-ios was computed. net8.0-maccatalyst was computed. net8.0-macos was computed. net8.0-tvos was computed. net8.0-windows was computed. |
-
net6.0
- FSharp.Core (>= 6.0.2)
NuGet packages
This package is not used by any NuGet packages.
GitHub repositories
This package is not used by any popular GitHub repositories.