Skip to content

Commit fcc1820

Browse files
committed
Added a sample with nested sub commands
1 parent e2e7803 commit fcc1820

File tree

3 files changed

+76
-14
lines changed

3 files changed

+76
-14
lines changed
Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
module ProgramNestedSubCommands
2+
3+
open System.IO
4+
open FSharp.SystemCommandLine
5+
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+
14+
let dir = Input.Argument("directory", DirectoryInfo(@"c:\default"))
15+
16+
command "list" {
17+
description "lists contents of a directory"
18+
inputs dir
19+
setHandler handler
20+
addAlias "ls"
21+
}
22+
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
31+
printfn $"{dir.FullName} does not exist."
32+
33+
let dir = Input.Argument("directory", DirectoryInfo(@"c:\default"))
34+
let recursive = Input.Option("--recursive", false)
35+
36+
command "delete" {
37+
description "deletes a directory"
38+
inputs (dir, recursive)
39+
setHandler handler
40+
addAlias "del"
41+
}
42+
43+
let ioCmd =
44+
command "io" {
45+
description "Contains IO related subcommands."
46+
setHandler id
47+
addCommands [ deleteCmd; listCmd ]
48+
}
49+
50+
51+
// [<EntryPoint>]
52+
let main argv =
53+
rootCommand argv {
54+
description "Sample app for System.CommandLine"
55+
setHandler id
56+
addCommand ioCmd
57+
}
Lines changed: 18 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,20 @@
11
{
2-
"profiles": {
3-
"TestConsole": {
4-
"commandName": "Project",
5-
"commandLineArgs": "-w hello -w world"
6-
},
7-
"GT8": {
8-
"commandName": "Project",
9-
"commandLineArgs": "-a A -b B -c C -d D -e E -1 1 -2 2 -3 3 -4 4 -5 5"
10-
},
11-
"Leading @": {
12-
"commandName": "Project",
13-
"commandLineArgs": "--package @shoelace-style/shoelace"
14-
}
15-
}
2+
"profiles": {
3+
"TestConsole": {
4+
"commandName": "Project",
5+
"commandLineArgs": "-w hello -w world"
6+
},
7+
"GT8": {
8+
"commandName": "Project",
9+
"commandLineArgs": "-a A -b B -c C -d D -e E -1 1 -2 2 -3 3 -4 4 -5 5"
10+
},
11+
"Leading @": {
12+
"commandName": "Project",
13+
"commandLineArgs": "--package @shoelace-style/shoelace"
14+
},
15+
"IO Nested SubCommands": {
16+
"commandName": "Project",
17+
"commandLineArgs": "io list c:/data/"
18+
},
19+
}
1620
}

src/TestConsole/TestConsole.fsproj

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
<Content Include="Properties\launchSettings.json" />
1010
<Compile Include="ProgramAlt1.fs" />
1111
<Compile Include="ProgramNoArgs.fs" />
12+
<Compile Include="ProgramNestedSubCommands.fs" />
1213
<Compile Include="ProgramSubCommand.fs" />
1314
<Compile Include="ProgramTask.fs" />
1415
<Compile Include="ProgramTokenReplacer.fs" />

0 commit comments

Comments
 (0)