Created
May 15, 2013 14:27
-
-
Save mavnn/5584376 to your computer and use it in GitHub Desktop.
UnionArgParser - making your console apps a thing of beauty
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
open System | |
open NuGet | |
open ReferenceManagement | |
open UnionArgParser | |
type Argument = | |
| [<MandatoryAttribute>] Action of string | |
| [<MandatoryAttribute>] ProjectFile of string | |
| [<MandatoryAttribute>] PackageId of string | |
| Version of string | |
with | |
interface IArgParserTemplate with | |
member s.Usage = | |
match s with | |
| Action _ -> "Specify an action: Install, Remove or Update" | |
| ProjectFile _ -> "Path to project file to update." | |
| PackageId _ -> "NuGet package id for action." | |
| Version _ -> "Optional specific version of package." | |
type ActionType = | |
| Install | |
| Remove | |
| Update | |
let processAction (a : string) = | |
match a.ToLower() with | |
| "install" -> Install | |
| "remove" -> Remove | |
| "update" -> Update | |
| _ -> failwith "Invalid Action; please use install, remove or update." | |
let processProjectFile (f : string) = | |
match IO.File.Exists(f) with | |
| true -> f | |
| false -> failwith "ProjectFile does not exist." | |
let processVersion (v : string) = | |
SemanticVersion(v) | |
[<EntryPoint>] | |
let main argv = | |
try | |
let parser = UnionArgParser<Argument>() | |
let results = parser.Parse() | |
let action = results.PostProcessResult <@ Action @> processAction | |
let proj = results.PostProcessResult <@ ProjectFile @> processProjectFile | |
let package = results.GetResult <@ PackageId @> | |
let version = results.TryPostProcessResult <@ Version @> processVersion | |
printfn "Action type: %A" action | |
match action with | |
| Install -> | |
match version with | |
| None -> InstallReference proj package | |
| Some v -> InstallReferenceOfSpecificVersion proj package v | |
| Update -> | |
match version with | |
| None -> UpdateReference proj package | |
| Some v -> UpdateReferenceToSpecificVersion proj package v | |
| Remove -> | |
RemoveReference proj package | |
0 | |
with | |
| ex -> | |
printfn "%A" ex | |
1 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Find out more about UnionArgParse at http://egtsrp.blogspot.gr/2013/03/a-declarative-argument-parser-for-f.html