Skip to content

Commit 573ff44

Browse files
authored
Added usePipeline ex (UseTokenReplacer override)
1 parent 8f6f68b commit 573ff44

File tree

1 file changed

+48
-0
lines changed

1 file changed

+48
-0
lines changed

README.md

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

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

343391
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)