Skip to content

Commit b58711e

Browse files
committed
2 parents bd53d14 + cfd6793 commit b58711e

File tree

1 file changed

+49
-0
lines changed

1 file changed

+49
-0
lines changed

README.md

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -338,6 +338,55 @@ let main argv =
338338
}
339339
```
340340

341+
## Customizing the Default Pipeline
342+
343+
System.CommandLine has a `CommandLineBuilder` that allows the user to customize various behaviors.
344+
345+
FSharp.SystemCommandLine is configured to use the built-in defaults (via `CommandLineBuilder().UseDefaults()`), but you can easily override them via the `usePipeline` custom operation which gives you access to the `CommandLineBuilder`.
346+
347+
For example, the default behavior intercepts input strings that start with a "@" character via the "TryReplaceToken" feature. This will cause an issue if you need to accept input that starts with "@". Fortunately, you can disable this via `usePipeline`:
348+
349+
```F#
350+
module TokenReplacerExample
351+
352+
open FSharp.SystemCommandLine
353+
open System.CommandLine.Builder // Necessary when overriding the builder via usePipeline
354+
355+
let app (package: string) =
356+
if package.StartsWith("@") then
357+
printfn $"{package}"
358+
0
359+
else
360+
eprintfn "The package name does not start with a leading @"
361+
1
362+
363+
[<EntryPoint>]
364+
let main argv =
365+
366+
// The package option needs to accept strings that start with "@" symbol.
367+
// For example, "--package @shoelace-style/shoelace".
368+
// To accomplish this, we will need to modify the default pipeline settings below.
369+
let package = Input.Option([ "--package"; "-p" ], "A package name that may have a leading '@' character.")
370+
371+
rootCommand argv {
372+
description "Can be called with a leading '@' package"
373+
374+
usePipeline (fun builder ->
375+
// Override default token replacer to ignore `@` processing
376+
builder.UseTokenReplacer(fun _ _ _ -> false)
377+
)
378+
379+
inputs package
380+
setHandler app
381+
}
382+
383+
```
384+
385+
As you can see, there are a lot of options that can be configured here (note that you need to `open System.CommandLine.Builder`):
386+
387+
![image](https://user-images.githubusercontent.com/1030435/199282781-1800b79c-7638-4242-8ca0-777d7237e20a.png)
388+
389+
341390
## Defining Inputs Manually
342391

343392
While the `Input.Argument` and `Input.Option` helper methods are useful for most common scenarios, sometimes it may be necessary to manually define inputs using the underlying `System.CommandLine` base library. This will make it easier to investigate the various overloads and take advantage of other features like custom validation.

0 commit comments

Comments
 (0)