Skip to content

Commit d02ede2

Browse files
authored
Merge pull request #10 from AngelMunoz/use-token-replacer
Add custom operation `useTokenReplacer`
2 parents a3fba01 + 9b70a00 commit d02ede2

4 files changed

Lines changed: 44 additions & 2 deletions

File tree

src/FSharp.SystemCommandLine/CommandBuilders.fs

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -333,6 +333,11 @@ type RootCommandParserBuilder<'A, 'B, 'C, 'D, 'E, 'F, 'G, 'H, 'Output>() =
333333
member this.UsePipeline (spec: CommandSpec<'Inputs, 'Output>, subCommand: CommandLineBuilder -> CommandLineBuilder) =
334334
this.CommandLineBuilder <- subCommand this.CommandLineBuilder
335335
spec
336+
337+
[<CustomOperation("useTokenReplacer")>]
338+
member this.UseTokenReplacer(spec: CommandSpec<'Inputs, 'Output>, replacer: TryReplaceToken) =
339+
this.CommandLineBuilder.UseTokenReplacer(replacer) |> ignore
340+
spec
336341

337342
/// Executes a Command with a handler that returns unit.
338343
member this.Run (spec: CommandSpec<'Inputs, unit>) =
@@ -372,6 +377,11 @@ type RootCommandBuilder<'A, 'B, 'C, 'D, 'E, 'F, 'G, 'H, 'Output>(args: string ar
372377
member this.UsePipeline (spec: CommandSpec<'Inputs, 'Output>, subCommand: CommandLineBuilder -> CommandLineBuilder) =
373378
this.CommandLineBuilder <- subCommand this.CommandLineBuilder
374379
spec
380+
381+
[<CustomOperation("useTokenReplacer")>]
382+
member this.UseTokenReplacer(spec: CommandSpec<'Inputs, 'Output>, replacer: TryReplaceToken) =
383+
this.CommandLineBuilder.UseTokenReplacer(replacer) |> ignore
384+
spec
375385

376386
/// Executes a Command with a handler that returns unit.
377387
member this.Run (spec: CommandSpec<'Inputs, unit>) =
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
module ProgramTokenReplacer
2+
3+
open FSharp.SystemCommandLine
4+
open System.CommandLine.Parsing
5+
6+
let app (package: string) =
7+
if package.StartsWith("@") then
8+
printfn $"{package}"
9+
0
10+
else
11+
eprintfn "The package name does not start with a leading @"
12+
1
13+
14+
//[<EntryPoint>]
15+
let main argv =
16+
let package =
17+
Input.Option([ "--package"; "-p" ], "A package with a leading @ name")
18+
19+
rootCommand argv {
20+
description "Can be called with a leading @ package"
21+
22+
useTokenReplacer (
23+
// in this case we want to skip @ processing
24+
TryReplaceToken (fun _ _ _ -> false)
25+
)
26+
27+
inputs package
28+
setHandler app
29+
}

src/TestConsole/Properties/launchSettings.json

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,10 @@
33
"TestConsole": {
44
"commandName": "Project",
55
"commandLineArgs": "-w hello -w world"
6+
},
7+
"Leading @": {
8+
"commandName": "Project",
9+
"commandLineArgs": "--package @shoelace-style/shoelace"
610
}
711
}
812
}

src/TestConsole/TestConsole.fsproj

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,11 +11,10 @@
1111
<Compile Include="ProgramNoArgs.fs" />
1212
<Compile Include="ProgramSubCommand.fs" />
1313
<Compile Include="ProgramTask.fs" />
14+
<Compile Include="ProgramTokenReplacer.fs" />
1415
<Compile Include="Program.fs" />
1516
</ItemGroup>
1617

17-
<ItemGroup />
18-
1918
<ItemGroup>
2019
<ProjectReference Include="..\FSharp.SystemCommandLine\FSharp.SystemCommandLine.fsproj" />
2120
</ItemGroup>

0 commit comments

Comments
 (0)