|
1 | 1 | module ProgramTokenReplacer |
2 | 2 |
|
| 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 | + |
3 | 24 | open FSharp.SystemCommandLine |
4 | 25 | 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 |
13 | 27 |
|
14 | 28 | 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 |
20 | 36 |
|
21 | 37 | rootCommand argv { |
22 | 38 | description "Can be called with a leading @ package" |
23 | 39 | configure (fun cfg -> |
24 | 40 | // Skip @ processing |
25 | 41 | //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 |
28 | 45 | ) |
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 | + ) |
30 | 65 | setAction app |
31 | 66 | } |
0 commit comments