Skip to content

Commit 5b3989d

Browse files
committed
Added a new example to TestConsole to troubleshoot token replacer logic using underlying S.CL library only.
1 parent f3981a0 commit 5b3989d

File tree

2 files changed

+53
-24
lines changed

2 files changed

+53
-24
lines changed

src/TestConsole/Program.fs

Lines changed: 2 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -9,18 +9,12 @@ let args commandLine =
99
let main _ =
1010

1111
//ProgramAlt1.main (args "--int-option 1 --bool-option true --file-option \"c:\test\"")
12-
1312
//ProgramNoArgs.main (args "")
14-
1513
//ProgramNestedSubCommands.main (args "io list \"c:/data/\" --enable-logging") // Also contains global options
16-
1714
//ProgramSubCommand.main (args "list c:/data/")
1815
//ProgramSubCommand.main (args "delete c:/data/ --recursive")
19-
2016
//ProgramTask.main (args "-w hello -w world")
21-
17+
ProgramTokenReplacer.SCL.main (args "--package @shoelace-style/shoelace")
2218
//ProgramTokenReplacer.main (args "--package @shoelace-style/shoelace")
23-
2419
//ProgramExtraInputs.main (args "-a A -b B -c C -d D -e E -1 1 -2 2 -3 3 -4 4 -5 5")
25-
26-
ProgramAppendWords.main (args "-w hello -w world -s \", \"")
20+
//ProgramAppendWords.main (args "-w hello -w world -s \", \"")
Lines changed: 51 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,31 +1,66 @@
11
module ProgramTokenReplacer
22

3+
module SCL =
4+
open System.CommandLine
5+
6+
let packageOpt = Option<string>("--package", [|"-p"|], Description = "A package with a leading @ name")
7+
8+
let action (parseResult: ParseResult) =
9+
let package = parseResult.GetValue packageOpt
10+
printfn $"The value for --package is: %s{package}"
11+
12+
let main (argv: string[]) =
13+
let cfg = new CommandLineConfiguration(new RootCommand())
14+
cfg.ResponseFileTokenReplacer <- null // in beta5, you must set ResponseFileTokenReplacer to null to skip @ processing
15+
let cmd = cfg.RootCommand
16+
17+
cmd.Description <- "My sample app"
18+
cmd.Add(packageOpt)
19+
cmd.SetAction(action)
20+
21+
cmd.Parse(argv).Invoke()
22+
23+
324
open FSharp.SystemCommandLine
425
open Input
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
26+
open System.CommandLine.Parsing
1327

1428
let main argv =
15-
16-
// The package option needs to accept strings that start with "@" symbol.
17-
// For example, "--package @shoelace-style/shoelace".
18-
// To accomplish this, we will need to modify the default pipeline settings below.
19-
let package = option "--package" |> aliases ["-p"] |> desc "A package with a leading @ name"
29+
let app (package: string) =
30+
if package.StartsWith("@") then
31+
printfn $"{package}"
32+
0
33+
else
34+
eprintfn "The package name does not start with a leading @"
35+
1
2036

2137
rootCommand argv {
2238
description "Can be called with a leading @ package"
2339
configure (fun cfg ->
2440
// Skip @ processing
2541
//cfg.UseTokenReplacer(fun _ _ _ -> false)
26-
cfg.ResponseFileTokenReplacer <- null // in beta5, you must set ResponseFileTokenReplacer to null to skip @ processing
27-
//cfg.ResponseFileTokenReplacer <- new TryReplaceToken(fun _ _ _ -> false)
42+
//cfg.ResponseFileTokenReplacer <- null // in beta5, you must set ResponseFileTokenReplacer to null to skip @ processing
43+
cfg.ResponseFileTokenReplacer <- new TryReplaceToken(fun _ _ _ -> true)
44+
cfg
2845
)
29-
inputs package
46+
inputs (
47+
// The package option needs to accept strings that start with "@" symbol.
48+
// For example, "--package @shoelace-style/shoelace".
49+
// To accomplish this, we will need to modify the default pipeline settings below.
50+
option<string> "--package"
51+
|> aliases ["-p"]
52+
|> desc "A package with a leading @ name"
53+
|> editOption (fun o ->
54+
o.CustomParser <- (fun res ->
55+
// Custom parser to allow leading @ in the package name
56+
let value = res.GetValueOrDefault<string>()
57+
if value.StartsWith("@")
58+
then value
59+
else
60+
res.AddError("oops")
61+
"---"
62+
)
63+
)
64+
)
3065
setAction app
3166
}

0 commit comments

Comments
 (0)