You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: README.md
+53-91Lines changed: 53 additions & 91 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -134,38 +134,9 @@ let zipFile =
134
134
135
135
## More Examples
136
136
137
-
### Simple App that Returns a Status Code
138
-
139
-
You may optionally return a status code from your handler function.
140
-
141
-
```F#
142
-
open System.IO
143
-
open FSharp.SystemCommandLine
144
-
open Input
145
-
146
-
let unzip (zipFile: FileInfo, outputDirMaybe: DirectoryInfo option) =
147
-
// Default to the zip file dir if None
148
-
let outputDir = defaultArg outputDirMaybe zipFile.Directory
149
-
150
-
if zipFile.Exists then
151
-
printfn $"Unzipping {zipFile.Name} to {outputDir.FullName}"
152
-
0 // Program successfully completed.
153
-
else
154
-
printfn $"File does not exist: {zipFile.FullName}"
155
-
2 // The system cannot find the file specified.
156
-
157
-
[<EntryPoint>]
158
-
let main argv =
159
-
rootCommand argv {
160
-
description "Unzips a .zip file"
161
-
inputs (
162
-
argument "zipfile" |> desc "The file to unzip",
163
-
optionMaybe "--output" |> alias "-o" |> desc "The output directory"
164
-
)
165
-
setAction unzip
166
-
}
167
-
```
137
+
### Returning a Status Code
168
138
139
+
You may optionally return a status code from your handler function by returning an `int`.
169
140
170
141
### App with SubCommands
171
142
@@ -235,40 +206,23 @@ let main argv =
235
206
### Passing Context to Action
236
207
237
208
You may need to pass the `ActionContext` to your handler function for the following reasons:
238
-
* You need to access to the `CancellationToken` for an asynchronous action.
209
+
* You need access to the `CancellationToken` for an asynchronous action.
239
210
* You need to manually parse values via the `ParseResult`. (This is necessary if you have more than 8 inputs.)
240
211
241
-
You can pass the `ActionContext` via the `Input.Context()` method.
212
+
You can pass the `ActionContext` via the `Input.context` value.
242
213
243
214
```F#
244
-
module Program
245
-
246
-
open FSharp.SystemCommandLine
247
-
open Input
248
-
open System.Threading
249
-
open System.Threading.Tasks
250
-
open System.CommandLine.Invocation
251
-
252
215
let app (ctx: ActionContext, words: string array, separator: string) =
253
216
task {
254
217
let cancel = ctx.CancellationToken
255
-
for i in [1..20] do
256
-
if cancel.IsCancellationRequested then
257
-
printfn "Cancellation Requested"
258
-
raise (new System.OperationCanceledException())
259
-
else
260
-
printfn $"{i}"
261
-
do! Task.Delay(1000)
262
-
263
-
System.String.Join(separator, words)
264
-
|> printfn "Result: %s"
218
+
// Use cancellation token for async work...
265
219
}
266
-
220
+
267
221
[<EntryPoint>]
268
-
let main argv =
222
+
let main argv =
269
223
let ctx = Input.context
270
224
let words = Input.option "--word" |> alias "-w" |> desc "A list of words to be appended"
271
-
let separator = Input.option "--separator" |> alias "-s" |> defaultValue ", " |> desc "A character that will separate the joined words."
225
+
let separator = Input.option "--separator" |> alias "-s" |> defaultValue ", "
272
226
273
227
rootCommand argv {
274
228
description "Appends words together"
@@ -279,7 +233,28 @@ let main argv =
279
233
|> Async.RunSynchronously
280
234
```
281
235
282
-
### Example with more than 8 inputs
236
+
### Showing Help as the Default
237
+
238
+
A common design is to show help information if no commands have been passed:
239
+
240
+
```F#
241
+
[<EntryPoint>]
242
+
let main argv =
243
+
rootCommand argv {
244
+
description "Shows help by default."
245
+
inputs Input.context
246
+
helpAction
247
+
addCommand helloCmd
248
+
}
249
+
```
250
+
251
+
---
252
+
253
+
## Advanced Examples
254
+
255
+
<details>
256
+
<summary><b>More than 8 inputs</b></summary>
257
+
283
258
Currently, a command handler function is limited to accept a tuple with no more than eight inputs.
284
259
If you need more, you can pass in the `ActionContext` to your action handler and manually get as many input values as you like (assuming they have been registered in the command builder's `addInputs` operation).
285
260
@@ -289,7 +264,7 @@ module Program
289
264
open FSharp.SystemCommandLine
290
265
open Input
291
266
292
-
module Parameters =
267
+
module Parameters =
293
268
let words = option "--word" |> alias "-w" |> desc "A list of words to be appended"
294
269
let separator = optionMaybe "--separator" |> alias "-s" |> desc "A character that will separate the joined words."
295
270
@@ -302,9 +277,9 @@ let app ctx =
302
277
let separator = separator |> Option.defaultValue ", "
0 commit comments