Skip to content

Commit 1448f30

Browse files
committed
2 parents edd866b + c7892cf commit 1448f30

File tree

3 files changed

+41
-5
lines changed

3 files changed

+41
-5
lines changed

README.md

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -64,23 +64,32 @@ _Notice that mismatches between the `setAction` and the `inputs` are caught as a
6464

6565
## Input API
6666
The new `Input` module contains functions for the underlying System.CommandLine `Option` and `Argument` properties.
67+
### Inputs
6768
* `context` passes an `ActionContext` containing a `ParseResult` and `CancellationToken` to the action
6869
* `argument` creates a named `Argument<'T>`
6970
* `argumentMaybe` creates a named `Argument<'T option>` that defaults to `None`.
7071
* `option` creates a named `Option<'T>`
7172
* `optionMaybe` creates a named `Option<'T option>` that defaults to `None`.
73+
74+
### Input Properties
75+
* `acceptLegalFilePathsOnly` sets the option or argument to accept only values representing legal file paths.
7276
* `alias` adds an `Alias` to an `Option`
7377
* `aliases` adds one or more aliases to an `Option`
7478
* `desc` adds a description to an `Option` or `Argument`
7579
* `defaultValue` or `def` provides a default value to an `Option` or `Argument`
7680
* `defFactory` assigns a default value factor to an `Option` or `Argument`
81+
* `helpName` adds the name used in help output to describe the option or argument.
7782
* `required` marks an `Option` as required
7883
* `validate` allows you to return a `Result<unit, string>` for the parsed value
7984
* `validateFileExists` ensures that the `FileInfo` exists
8085
* `validateDirectoryExists` ensures that the `DirectoryInfo` exists
8186
* `addValidator` allows you to add a validator to the underlying `Option` or `Argument`
87+
* `acceptOnlyFromAmong` validates the allowed values for an `Option` or `Argument`
8288
* `customParser` allows you to parse the input tokens using a custom parser function.
8389
* `tryParse` allows you to parse the input tokens using a custom parser `Result<'T, string>` function.
90+
* `arity` sets the arity of an `Option` or `Argument`
91+
* `allowMultipleArgumentsPerToken` allows multiple values for an `Option` or `Argument`. (Defaults to 'false' if not set.)
92+
* `hidden` hides an option or argument from the help output
8493
* `editOption` allows you to pass a function to edit the underlying `Option`
8594
* `editArgument` allows you to pass a function to edit the underlying `Argument`
8695
* `ofOption` allows you to pass a manually created `Option`
@@ -402,14 +411,14 @@ module Global =
402411
403412
type Options = { EnableLogging: bool; LogFile: FileInfo }
404413
405-
let options: HandlerInput seq = [ enableLogging; logFile ]
414+
let options: ActionInput seq = [ enableLogging; logFile ]
406415
407416
let bind (ctx: ActionContext) =
408417
{ EnableLogging = enableLogging.GetValue ctx.ParseResult
409418
LogFile = logFile.GetValue ctx.ParseResult }
410419
411420
let listCmd =
412-
let action (ctx: InvocationContext, dir: DirectoryInfo) =
421+
let action (ctx: ActionContext, dir: DirectoryInfo) =
413422
let options = Global.bind ctx
414423
if options.EnableLogging then
415424
printfn $"Logging enabled to {options.LogFile.FullName}"
@@ -431,7 +440,7 @@ let listCmd =
431440
}
432441
433442
let deleteCmd =
434-
let action (ctx: InvocationContext, dir: DirectoryInfo, recursive: bool) =
443+
let action (ctx: ActionContext, dir: DirectoryInfo, recursive: bool) =
435444
let options = Global.bind ctx
436445
if options.EnableLogging then
437446
printfn $"Logging enabled to {options.LogFile.FullName}"
@@ -462,7 +471,7 @@ let ioCmd =
462471
}
463472
464473
[<EntryPoint>]
465-
let main argv =
474+
let main (argv: string array) =
466475
let cfg =
467476
commandLineConfiguration {
468477
description "Sample app for System.CommandLine"

src/FSharp.SystemCommandLine/CommandBuilders.fs

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@ type CommandSpec<'Inputs, 'Output> =
3737
SubCommands: System.CommandLine.Command list
3838
/// Registers extra inputs that can be parsed via the InvocationContext if more than 8 are required.
3939
ExtraInputs: ActionInput list
40+
Hidden: bool
4041
}
4142
static member Default =
4243
{
@@ -47,6 +48,7 @@ type CommandSpec<'Inputs, 'Output> =
4748
ExtraInputs = []
4849
Handler = def<unit -> 'Output> // Support unit -> 'Output handler by default
4950
SubCommands = []
51+
Hidden = false
5052
}
5153

5254
/// Contains shared operations for building a `rootCommand`, `command` or `commandLineConfiguration` CE.
@@ -61,6 +63,7 @@ type BaseCommandBuilder<'A, 'B, 'C, 'D, 'E, 'F, 'G, 'H, 'Output>() =
6163
ExtraInputs = spec.ExtraInputs
6264
Handler = handler
6365
SubCommands = spec.SubCommands
66+
Hidden = spec.Hidden
6467
}
6568

6669
/// Converts up to 8 handler inputs into a tuple of the specified action input type.
@@ -275,6 +278,7 @@ type BaseCommandBuilder<'A, 'B, 'C, 'D, 'E, 'F, 'G, 'H, 'Output>() =
275278
)
276279
spec.SubCommands |> List.iter cmd.Add
277280
spec.Aliases |> List.iter cmd.Aliases.Add
281+
cmd.Hidden <- spec.Hidden
278282
cmd
279283

280284
/// Sets a command handler that returns `unit`.
@@ -433,6 +437,11 @@ type RootCommandBuilder<'A, 'B, 'C, 'D, 'E, 'F, 'G, 'H, 'Output>(args: string ar
433437
type CommandBuilder<'A, 'B, 'C, 'D, 'E, 'F, 'G, 'H, 'Output>(name: string) =
434438
inherit BaseCommandBuilder<'A, 'B, 'C, 'D, 'E, 'F, 'G, 'H, 'Output>()
435439

440+
/// Hides the command from the help output.
441+
[<CustomOperation("hidden")>]
442+
member this.Hidden (spec: CommandSpec<'Inputs, 'Output>) =
443+
{ spec with Hidden = true }
444+
436445
/// Returns a Command with a handler that returns unit.
437446
member this.Run (spec: CommandSpec<'Inputs, unit>) =
438447
Command(name)

src/FSharp.SystemCommandLine/Inputs.fs

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,12 @@ module Input =
6868
| _ -> ()
6969
input
7070

71+
/// Configures the option or argument to accept only values representing legal file paths.
72+
let acceptLegalFilePathsOnly (input: ActionInput<'T>) =
73+
input
74+
|> editOption (fun o -> o.AcceptLegalFilePathsOnly() |> ignore)
75+
|> editArgument (fun a -> a.AcceptLegalFilePathsOnly() |> ignore)
76+
7177
/// Adds one or more aliases to an option.
7278
let aliases (aliases: string seq) (input: ActionInput<'T>) =
7379
input |> editOption (fun o -> aliases |> Seq.iter o.Aliases.Add)
@@ -100,6 +106,12 @@ module Input =
100106
|> editOption (fun o -> o.DefaultValueFactory <- defaultValueFactory)
101107
|> editArgument (fun a -> a.DefaultValueFactory <- defaultValueFactory)
102108

109+
/// The name used in help output to describe the option or argument.
110+
let helpName (helpName: string) (input: ActionInput<'T>) =
111+
input
112+
|> editOption (fun o -> o.HelpName <- helpName)
113+
|> editArgument (fun a -> a.HelpName <- helpName)
114+
103115
/// Marks an option as required.
104116
let required (input: ActionInput<'T>) =
105117
input
@@ -229,7 +241,13 @@ module Input =
229241
/// Sets a value that indicates whether multiple arguments are allowed for each option identifier token. (Defaults to 'false'.)
230242
let allowMultipleArgumentsPerToken (input: ActionInput<'T>) =
231243
input
232-
|> editOption (fun a -> a.AllowMultipleArgumentsPerToken <- true)
244+
|> editOption (fun o -> o.AllowMultipleArgumentsPerToken <- true)
245+
246+
/// Hides an option or argument from the help output.
247+
let hidden (input: ActionInput<'T>) =
248+
input
249+
|> editOption (fun o -> o.Hidden <- true)
250+
|> editArgument (fun a -> a.Hidden <- true)
233251

234252
/// Converts an `Option<'T>` to an `ActionInput<'T>` for usage with the command builders.
235253
let ofOption (o: Option<'T>) =

0 commit comments

Comments
 (0)