Skip to content

Commit fde89e9

Browse files
committed
Reorganized README examples.
1 parent 4522bf0 commit fde89e9

File tree

1 file changed

+53
-91
lines changed

1 file changed

+53
-91
lines changed

README.md

Lines changed: 53 additions & 91 deletions
Original file line numberDiff line numberDiff line change
@@ -134,38 +134,9 @@ let zipFile =
134134

135135
## More Examples
136136

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
168138

139+
You may optionally return a status code from your handler function by returning an `int`.
169140

170141
### App with SubCommands
171142

@@ -235,40 +206,23 @@ let main argv =
235206
### Passing Context to Action
236207

237208
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.
239210
* You need to manually parse values via the `ParseResult`. (This is necessary if you have more than 8 inputs.)
240211

241-
You can pass the `ActionContext` via the `Input.Context()` method.
212+
You can pass the `ActionContext` via the `Input.context` value.
242213

243214
```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-
252215
let app (ctx: ActionContext, words: string array, separator: string) =
253216
task {
254217
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...
265219
}
266-
220+
267221
[<EntryPoint>]
268-
let main argv =
222+
let main argv =
269223
let ctx = Input.context
270224
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 ", "
272226
273227
rootCommand argv {
274228
description "Appends words together"
@@ -279,7 +233,28 @@ let main argv =
279233
|> Async.RunSynchronously
280234
```
281235

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+
283258
Currently, a command handler function is limited to accept a tuple with no more than eight inputs.
284259
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).
285260

@@ -289,7 +264,7 @@ module Program
289264
open FSharp.SystemCommandLine
290265
open Input
291266
292-
module Parameters =
267+
module Parameters =
293268
let words = option "--word" |> alias "-w" |> desc "A list of words to be appended"
294269
let separator = optionMaybe "--separator" |> alias "-s" |> desc "A character that will separate the joined words."
295270
@@ -302,9 +277,9 @@ let app ctx =
302277
let separator = separator |> Option.defaultValue ", "
303278
System.String.Join(separator, words) |> printfn "Result: %s"
304279
0
305-
280+
306281
[<EntryPoint>]
307-
let main argv =
282+
let main argv =
308283
rootCommand argv {
309284
description "Appends words together"
310285
inputs Input.context
@@ -313,7 +288,10 @@ let main argv =
313288
}
314289
```
315290

316-
### Example using Microsoft.Extensions.Hosting
291+
</details>
292+
293+
<details>
294+
<summary><b>Microsoft.Extensions.Hosting</b></summary>
317295

318296
This example requires the following nuget packages:
319297

@@ -396,9 +374,12 @@ let main argv =
396374
|> Async.RunSynchronously
397375
```
398376

399-
### Global Options
377+
</details>
378+
379+
<details>
380+
<summary><b>Global Options</b></summary>
400381

401-
This example shows to create global options to all child commands.
382+
This example shows how to create global options for all child commands.
402383

403384
```F#
404385
module ProgramNestedSubCommands
@@ -491,7 +472,10 @@ let main (argv: string array) =
491472
parseResult.Invoke()
492473
```
493474

494-
### Database Migrations Example
475+
</details>
476+
477+
<details>
478+
<summary><b>Database Migrations Example</b></summary>
495479

496480
This real-life example for running database migrations demonstrates the following features:
497481
* Uses Microsoft.Extensions.Hosting.
@@ -588,10 +572,12 @@ let main argv =
588572
addCommand (repairCmd logger)
589573
addCommand (migrateCmd logger)
590574
}
591-
592575
```
593576

594-
### Manually Invoking a Root Command
577+
</details>
578+
579+
<details>
580+
<summary><b>Manually Invoking a Root Command</b></summary>
595581

596582
If you want to manually invoke your root command, use the `ManualInvocation.rootCommand` computation expression.
597583

@@ -611,7 +597,7 @@ let app (words: string array, separator: string option) =
611597
612598
[<EntryPoint>]
613599
let main argv =
614-
let words = option "--word" |> alias -w" |> desc "A list of words to be appended"
600+
let words = option "--word" |> alias "-w" |> desc "A list of words to be appended"
615601
let separator = optionMaybe "--separator" |> alias "-s" |> desc "A character that will separate the joined words."
616602
617603
let cmd =
@@ -632,33 +618,9 @@ Notes about invocation:
632618
* You can optionally pass in an `InvocationConfiguration`:
633619
* `parseResult.Invoke(InvocationConfiguration(EnableDefaultExceptionHandler = false))`
634620

635-
### Showing Help as the Default
636-
A common design is to show help information if no commands have been passed:
637-
638-
```F#
639-
open System.CommandLine.Invocation
640-
open System.CommandLine.Help
641-
open FSharp.SystemCommandLine
642-
open Input
621+
</details>
643622

644-
let helloCmd =
645-
let action name = printfn $"Hello, %s{name}."
646-
let name = argument "Name"
647-
command "hello" {
648-
description "Says hello."
649-
inputs name
650-
setAction action
651-
}
652-
653-
[<EntryPoint>]
654-
let main argv =
655-
rootCommand argv {
656-
description "Says hello or shows help by default."
657-
inputs Input.context
658-
helpAction
659-
addCommand helloCmd
660-
}
661-
```
623+
---
662624

663625
## Configuration
664626

0 commit comments

Comments
 (0)