Created
February 12, 2022 04:26
-
-
Save baronfel/b9a34f6882e142cbd07bedb341b182bd to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#r "nuget: System.CommandLine, 2.0.0-beta3.22106.2" | |
open System.CommandLine | |
open System.CommandLine.Builder | |
open System.CommandLine.Parsing | |
open System.CommandLine.Invocation | |
open System.Threading.Tasks | |
open System | |
let args = fsi.CommandLineArgs[1..] | |
let dummyProjectData = Map [ | |
"dummy", Map [ | |
"name", "Dummy" | |
] | |
] | |
let rootCommand = RootCommand() | |
let projectCommand = Command("project", "Manage projects") | |
let projectArg = Argument<string>("project", "The project to build") | |
projectCommand.AddArgument(projectArg) | |
let setSubCommand = Command("set", "Set a configuration value") | |
let keyArg = Argument<string>("key", "The configuration key") | |
keyArg.Arity <- ArgumentArity.ExactlyOne | |
let valueArg = Argument<string>("value", "The configuration value") | |
valueArg.Arity <- ArgumentArity.ZeroOrOne | |
setSubCommand.AddArgument(keyArg) | |
setSubCommand.AddArgument(valueArg) | |
setSubCommand.SetHandler( | |
Func<_,_,_,_,_>(fun (project: string) (key: string) (value: string) (console: IConsole) -> | |
task { | |
match value with | |
| null -> | |
console.WriteLine($"getting value for {key} from {project}") | |
let v = | |
dummyProjectData | |
|> Map.tryFind project | |
|> Option.bind (Map.tryFind key) | |
match v with | |
| None -> console.WriteLine("not found") | |
| Some v -> console.WriteLine(v) | |
return 0 | |
// get value for key | |
| v -> | |
console.WriteLine($"setting {key} to {v} in {project}") | |
return 0 | |
} :> Task | |
), projectArg, keyArg, valueArg) | |
projectCommand.AddCommand setSubCommand | |
rootCommand.AddCommand projectCommand | |
let parser = CommandLineBuilder(rootCommand).UseDefaults().Build() | |
let parse (args: string[]) = | |
parser.Parse(args) | |
let run (args: string[]) = | |
parser.Invoke(args) |
Author
baronfel
commented
Feb 12, 2022
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment