FsCli 1.0.1

There is a newer version of this package available.
See the version list below for details.
dotnet add package FsCli --version 1.0.1                
NuGet\Install-Package FsCli -Version 1.0.1                
This command is intended to be used within the Package Manager Console in Visual Studio, as it uses the NuGet module's version of Install-Package.
<PackageReference Include="FsCli" Version="1.0.1" />                
For projects that support PackageReference, copy this XML node into the project file to reference the package.
paket add FsCli --version 1.0.1                
#r "nuget: FsCli, 1.0.1"                
#r directive can be used in F# Interactive and Polyglot Notebooks. Copy this into the interactive tool or source code of the script to reference the package.
// 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

  1. 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

  1. 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

  1. 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 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. 
Compatible target framework(s)
Included target framework(s) (in package)
Learn more about Target Frameworks and .NET Standard.

NuGet packages

This package is not used by any NuGet packages.

GitHub repositories

This package is not used by any popular GitHub repositories.

Version Downloads Last updated
1.0.3 460 4/8/2022
1.0.2 410 4/4/2022
1.0.1 414 4/3/2022
1.0.0 412 4/2/2022