From 573ff44ffe3caffb98a2e7fdb368c59a8cb4fc83 Mon Sep 17 00:00:00 2001 From: Jordan Marr Date: Tue, 1 Nov 2022 12:19:35 -0400 Subject: [PATCH 1/3] Added `usePipeline` ex (UseTokenReplacer override) --- README.md | 48 ++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 48 insertions(+) diff --git a/README.md b/README.md index 12769d1..bc82a05 100644 --- a/README.md +++ b/README.md @@ -338,6 +338,54 @@ let main argv = } ``` +## Changing System.CommandLine Pipeline Defaults + +System.CommandLine has a `CommandLineBuilder` that allows the user to customize various behaviors. +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`. + +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`: + +```F# +module TokenReplacerExample + +open FSharp.SystemCommandLine +open System.CommandLine.Builder // Necessary when overriding the builder via usePipeline + +let app (package: string) = + if package.StartsWith("@") then + printfn $"{package}" + 0 + else + eprintfn "The package name does not start with a leading @" + 1 + +//[] +let main argv = + + // The package option needs to accept strings that start with "@" symbol. + // For example, "--package @shoelace-style/shoelace". + // To accomplish this, we will need to modify the default pipeline settings below. + let package = Input.Option([ "--package"; "-p" ], "A package name that may have a leading '@' character.") + + rootCommand argv { + description "Can be called with a leading '@' package" + + usePipeline (fun builder -> + // Override default token replacer to ignore `@` processing + builder.UseTokenReplacer(fun _ _ _ -> false) + ) + + inputs package + setHandler app + } + +``` + +As you can see, there are a lot of options that can be configured here (note that you need to `open System.CommandLine.Builder`): + +![image](https://user-images.githubusercontent.com/1030435/199282781-1800b79c-7638-4242-8ca0-777d7237e20a.png) + + ## Defining Inputs Manually 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. From 580bd8fcc91311df061648271d38f343c932a032 Mon Sep 17 00:00:00 2001 From: Jordan Marr Date: Tue, 1 Nov 2022 12:24:11 -0400 Subject: [PATCH 2/3] Update README.md --- README.md | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/README.md b/README.md index bc82a05..abf004b 100644 --- a/README.md +++ b/README.md @@ -338,10 +338,10 @@ let main argv = } ``` -## Changing System.CommandLine Pipeline Defaults +## Customizing the Default Pipeline System.CommandLine has a `CommandLineBuilder` that allows the user to customize various behaviors. -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`. +FSharp.SystemCommandLine is configured to use built-in defaults (via `CommandLineBuilder().UseDefaults()`), but you can easily override them should the need arise via the `usePipeline` custom operation which gives you access to the `CommandLineBuilder`. 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`: @@ -359,7 +359,7 @@ let app (package: string) = eprintfn "The package name does not start with a leading @" 1 -//[] +[] let main argv = // The package option needs to accept strings that start with "@" symbol. From cfd6793e3f5bdb647aa14d7a930ccc11db810881 Mon Sep 17 00:00:00 2001 From: Jordan Marr Date: Tue, 1 Nov 2022 12:35:02 -0400 Subject: [PATCH 3/3] Update README.md --- README.md | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/README.md b/README.md index abf004b..cd41d0a 100644 --- a/README.md +++ b/README.md @@ -341,7 +341,8 @@ let main argv = ## Customizing the Default Pipeline System.CommandLine has a `CommandLineBuilder` that allows the user to customize various behaviors. -FSharp.SystemCommandLine is configured to use built-in defaults (via `CommandLineBuilder().UseDefaults()`), but you can easily override them should the need arise via the `usePipeline` custom operation which gives you access to the `CommandLineBuilder`. + +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`. 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`: