Releases: JordanMarr/FSharp.SystemCommandLine
Support for command aliases
Changes in v0.17.0-beta4
- New
addAlias
andaddAliases
operations to add command aliases. (thanks to @AngelMunoz! π) - Deprecated
add
overloads and renamed toaddInput
andaddInputs
for clarity and consistency
Support for netstandard, `add` overload
Input helpers
This release adds a few new Input
methods to make it more convenient to modify the underlying System.CommandLine
properties for Option
and Argument
inputs.
- New
Input.Option
overloads that take aname
/alias
and aconfigure
function that allows you to manually set properties on the S.CLOption<'T>
. - New
Input.OptionMaybe
overloads that take aname
/alias
and aconfigure
function that allows you to manually set properties on the S.CLOption<'T option>
. - New
Input.Argument
overloads that take aname
/alias
and aconfigure
function that allows you to manually set properties on the S.CLArgument<'T>
.
Please see updated docs here:
https://github.com/JordanMarr/FSharp.SystemCommandLine#setting-input-properties-manually
Also, two new alias methods (for easier discoverability) for converting existing SCL inputs:
Input.OfOption
- An alias forHandlerInput.OfOption
Input.OfArgument
- An alias forHandlerInput.OfArgument
Handling of greater than 8 inputs
This release provides a convenient way to handle commands that require more than 8 inputs via the new add
custom operation on the rootCommand
and command
builders.
Additional info
Currently, a command handler function is limited to accept a tuple with no more than eight inputs.
If you need more than eight inputs, you can now pass in the InvocationContext
to your handler, which will allow you manually get as many input values as you like (assuming they have been registered via the rootCommand
or command
builder's add
operation:
module Program
open FSharp.SystemCommandLine
module Parameters =
let words = Input.Option<string[]>(["--word"; "-w"], Array.empty, "A list of words to be appended")
let separator = Input.OptionMaybe<string>(["--separator"; "-s"], "A character that will separate the joined words.")
let app (ctx: System.CommandLine.Invocation.InvocationContext) =
// Manually parse as many parameters as you need
let words = Parameters.words.GetValue ctx
let separator = Parameters.separator.GetValue ctx
// Do work
let separator = separator |> Option.defaultValue ", "
System.String.Join(separator, words) |> printfn "Result: %s"
0
[<EntryPoint>]
let main argv =
let ctx = Input.Context()
rootCommand argv {
description "Appends words together"
inputs ctx
setHandler app
add Parameters.words
add Parameters.separator
}
Caveats
This manual binding mechanism is less type safe because the compiler has no way to determine if you have manually added all input parameters to the command; so if you forget to add one that is used, it will result in a runtime error.
(For this reason, you should try to limit your commands to 8 parameters when possible, by factoring your app into separate commands.)
beta4 changes
v0.13.0-beta4 adapts to the System.CommandLine
beta4 changes.
The API stays mostly the same with the following changes:
-
π₯ Removed
Input.InjectedDependency<'T>()
(this allowed you to pass in S.CL injected system dependencies likeInvocationContext
,CancellationToken
,IConsole
, etc. -
β Added
Input.Context()
for passingInvocationContext
to handler function.
This replacesInput.InjectedDependency<'T>()
becauseInvocationContext
can be used to get aCancellationToken
,IConsole
, etc. -
Handler functions now support 8 input bindings instead of 16 (following changes to S.CL).
(If you need more than 8 input bindings, you can pass theInvocationContext
to your handler function using the newInput.Context()
method which will allow you to manually get as many parsed values as you want.)
The readme has been updated with an example of passing the InvocationContext
:
https://github.com/JordanMarr/FSharp.SystemCommandLine#passing-the-invocationcontext
NEW: rootCommandParser CE
Added a rootCommandParser
to provide the opportunity to manually parse and invoke a root command (since the rootCommand
is auto-executing).
open FSharp.SystemCommandLine
open System.CommandLine.Parsing
let app (words: string array, separator: string option) =
let separator = separator |> Option.defaultValue ", "
System.String.Join(separator, words) |> printfn "Result: %s"
0
[<EntryPoint>]
let main argv =
let words = Input.Option(["--word"; "-w"], Array.empty, "A list of words to be appended")
let separator = Input.OptionMaybe(["--separator"; "-s"], "A character that will separate the joined words.")
let parser =
rootCommandParser {
description "Appends words together"
inputs (words, separator)
setHandler app
}
parser.Parse(argv).Invoke()
Fixed OptionMaybe bool with no arguments
- Fixed an issue where
Input.OptionMaybe<bool>("--flag")
would not translate--flag
toSome true
unless it had a boolean argument value (--flag true
).
Fixed ArgumentMaybe
- Fixed a bug with
Input.ArgumentMaybe
. - Added support for
Uri
for the makeshift Maybe parser. (parsing forOptionMaybe
andArgumentMaybe
helpers is currently being done withinFSharp.SystemCommandLine
. This is because the parsing code inSystem.CommandLine
is currently internal and therefore unusable for reuse when creating custom types).
NEW: OptionRequired
Added Input.OptionRequired
overloads.
NEW: `addCommands` and `addCommand`
Deprecated setCommand
in favor of addCommand
or addCommands
.