Skip to content

Commit 89b8cb5

Browse files
authored
Merge pull request #15 from AngelMunoz/add-alias
Add Support for Command Aliases
2 parents 7a424f1 + 158918f commit 89b8cb5

File tree

2 files changed

+34
-19
lines changed

2 files changed

+34
-19
lines changed

src/FSharp.SystemCommandLine/CommandBuilders.fs

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ type CommandSpec<'Inputs, 'Output> =
2222
Description: string
2323
Inputs: HandlerInput list
2424
Handler: 'Inputs -> 'Output
25+
Alias: string list
2526
SubCommands: System.CommandLine.Command list
2627
/// Registers extra inputs that can be parsed via the InvocationContext if more than 8 are required.
2728
ExtraInputs: HandlerInput list
@@ -30,6 +31,7 @@ type CommandSpec<'Inputs, 'Output> =
3031
{
3132
Description = "My Command"
3233
Inputs = []
34+
Alias = []
3335
ExtraInputs = []
3436
Handler = def<unit -> 'Output> // Support unit -> 'Output handler by default
3537
SubCommands = []
@@ -42,6 +44,7 @@ type BaseCommandBuilder<'A, 'B, 'C, 'D, 'E, 'F, 'G, 'H, 'Output>() =
4244
{
4345
Description = spec.Description
4446
Inputs = spec.Inputs
47+
Alias = spec.Alias
4548
ExtraInputs = spec.ExtraInputs
4649
Handler = handler
4750
SubCommands = spec.SubCommands
@@ -120,6 +123,14 @@ type BaseCommandBuilder<'A, 'B, 'C, 'D, 'E, 'F, 'G, 'H, 'Output>() =
120123
member this.AddCommands (spec: CommandSpec<'Inputs, 'Output>, subCommands: System.CommandLine.Command seq) =
121124
{ spec with SubCommands = spec.SubCommands @ (subCommands |> Seq.toList) }
122125

126+
[<CustomOperation("addAlias")>]
127+
member this.AddAlias (spec: CommandSpec<'Inputs, 'Output>, alias: string seq) =
128+
{ spec with Alias = spec.Alias @ (alias |> Seq.toList) }
129+
130+
[<CustomOperation("addAlias")>]
131+
member this.AddAlias (spec: CommandSpec<'Inputs, 'Output>, alias: string) =
132+
{ spec with Alias = alias :: spec.Alias }
133+
123134
/// Registers an additional input that can be manually parsed via the InvocationContext. (Use when more than 8 inputs are required.)
124135
[<CustomOperation("add")>]
125136
member this.Add(spec: CommandSpec<'Inputs, 'Output>, extraInput: HandlerInput<'Value>) =
@@ -152,6 +163,7 @@ type BaseCommandBuilder<'A, 'B, 'C, 'D, 'E, 'F, 'G, 'H, 'Output>() =
152163
)
153164

154165
spec.SubCommands |> List.iter cmd.AddCommand
166+
spec.Alias |> List.iter cmd.AddAlias
155167
cmd
156168

157169
/// Sets a command handler that returns `unit`.

src/TestConsole/ProgramSubCommand.fs

Lines changed: 22 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -3,45 +3,48 @@
33
open System.IO
44
open FSharp.SystemCommandLine
55

6-
let listCmd =
7-
let handler (dir: DirectoryInfo) =
8-
if dir.Exists
9-
then dir.EnumerateFiles() |> Seq.iter (fun f -> printfn "%s" f.FullName)
10-
else printfn $"{dir.FullName} does not exist."
11-
6+
let listCmd =
7+
let handler (dir: DirectoryInfo) =
8+
if dir.Exists then
9+
dir.EnumerateFiles()
10+
|> Seq.iter (fun f -> printfn "%s" f.FullName)
11+
else
12+
printfn $"{dir.FullName} does not exist."
13+
1214
let dir = Input.Argument("directory", DirectoryInfo(@"c:\default"))
1315

1416
command "list" {
1517
description "lists contents of a directory"
1618
inputs dir
1719
setHandler handler
20+
addAlias "ls"
1821
}
1922

20-
let deleteCmd =
21-
let handler (dir: DirectoryInfo, recursive: bool) =
22-
if dir.Exists then
23-
if recursive
24-
then printfn $"Recursively deleting {dir.FullName}"
25-
else printfn $"Deleting {dir.FullName}"
26-
else
23+
let deleteCmd =
24+
let handler (dir: DirectoryInfo, recursive: bool) =
25+
if dir.Exists then
26+
if recursive then
27+
printfn $"Recursively deleting {dir.FullName}"
28+
else
29+
printfn $"Deleting {dir.FullName}"
30+
else
2731
printfn $"{dir.FullName} does not exist."
2832

29-
let dir = Input.Argument("directory", DirectoryInfo(@"c:\default"))
33+
let dir = Input.Argument("directory", DirectoryInfo(@"c:\default"))
3034
let recursive = Input.Option("--recursive", false)
3135

3236
command "delete" {
3337
description "deletes a directory"
3438
inputs (dir, recursive)
3539
setHandler handler
40+
addAlias [ "d"; "del" ]
3641
}
37-
3842

39-
//[<EntryPoint>]
40-
let main argv =
43+
44+
// [<EntryPoint>]
45+
let main argv =
4146
rootCommand argv {
4247
description "File System Manager"
4348
setHandler id
4449
addCommands [ listCmd; deleteCmd ]
4550
}
46-
47-

0 commit comments

Comments
 (0)