This repository has been archived by the owner on Oct 31, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 14
Address #27 from fsprojects/Projekt #32
Open
baronfel
wants to merge
8
commits into
fsprojects-archive:master
Choose a base branch
from
baronfel:unify_command_line_args
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
90deb10
use commandline.parser
fe6cac8
needed to add company attr to make the deafult help screen for this n…
0d80ce1
cleanup of the new Args implementation to be in line with standards. …
048f0bb
correct formatting around records.
721a559
fixup project.fsproj
0a75b69
clarify the help text for reference verb
4acc5d8
fake out msbuild with regard to the templates directory
cee9d36
try out examples in usage screen
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains 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
This file contains 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
This file contains 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
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,111 +1,113 @@ | ||
module Projekt.Args | ||
|
||
open Projekt.Types | ||
open System.IO | ||
open Nessos.UnionArgParser | ||
|
||
type private Args = | ||
| Template of string | ||
| [<AltCommandLine("-fxv")>] FrameworkVersion of string | ||
| Organisation of string | ||
| Direction of string | ||
| Repeat of int | ||
| Link of string | ||
| Compile of bool | ||
with | ||
interface IArgParserTemplate with | ||
member s.Usage = | ||
match s with | ||
| Template _ -> "init -- specify the template (library|console) [default: library]" | ||
| Direction _ -> "movefile -- specify the direction (down|up)" | ||
| Repeat _ -> "movefile -- specify the distance [default: 1]" | ||
| FrameworkVersion _ -> "init -- specify the framework version (4.0|4.5|4.5.1) [default: 4.5]" | ||
| Organisation _ -> "init -- specify the organisation" | ||
| Link _ -> "addfile -- specify an optional Link attribute" | ||
| Compile _ -> "addfile -- should the file be compiled or not [default: true]" | ||
module Args = | ||
open CommandLine | ||
open CommandLine.Text | ||
|
||
let private templateArg (res : ArgParseResults<Args>) = | ||
match res.TryGetResult(<@ Template @>) with | ||
| Some (ToLower "console") -> Console | ||
| Some (ToLower "library") -> Library | ||
| None -> Library | ||
| _ -> failwith "invalid template argument specified" | ||
[<Verb("init", HelpText = "create a new project")>] | ||
type InitOptions = | ||
{ [<Value(0, Required = true, MetaName = "project path")>] Path : string | ||
[<Option(Default = "library")>] Template : string | ||
[<Option(Default = "4.5")>] FrameworkVersion : string | ||
[<Option>] Organization : string option } | ||
with | ||
member x.ToOperation = | ||
match x.Path with | ||
| FullPath p -> | ||
let template = x.Template |> Template.Create |> Some | ||
let frmwkVersion = x.FrameworkVersion |> FrameworkVersion.Create |> Some | ||
(ProjectInitData.create | ||
p | ||
template | ||
frmwkVersion | ||
x.Organization) |> Init | ||
|
||
let private frameworkVersionArg (res : ArgParseResults<Args>) = | ||
match res.TryGetResult(<@ FrameworkVersion @>) with | ||
| Some "4.0" -> V4_0 | ||
| Some "4.5" -> V4_5 | ||
| Some "4.5.1" -> V4_5_1 | ||
| None -> V4_5 | ||
| _ -> failwith "invalid framework version argument specified" | ||
| _ -> failwith "not given a full path" | ||
[<Usage(ApplicationAlias = "projekt")>] | ||
static member Examples | ||
with get () = | ||
seq { | ||
yield Example("normal usage", {Path = @"c:\code\projekt\"; Template = ""; FrameworkVersion = ""; Organization = None}) | ||
yield Example("make an exe project", {Path = @"c:\code\projekt\"; Template = "console"; FrameworkVersion = ""; Organization = None}) | ||
yield Example("target .net 4.0", {Path = @"c:\code\projekt\"; Template = ""; FrameworkVersion = "4.0"; Organization = None}) | ||
} | ||
|
||
let private parseDirection s = | ||
match s with | ||
| ToLower "up" -> Up | ||
| ToLower "down" -> Down | ||
| _ -> failwith "invalid direction specified" | ||
[<Verb("reference", HelpText = "reference another dependency in this project")>] | ||
type private ReferenceOptions = | ||
{ [<Value(0, Required = true, MetaName = "project path")>] ProjectPath : string | ||
[<Value(1, Required = true, MetaName = "reference path")>] ReferencePath : string } | ||
with | ||
member x.ToOperation = | ||
match x.ProjectPath, x.ReferencePath with | ||
| FullPath project, FullPath reference -> | ||
{ ProjPath = project | ||
Reference = reference } | ||
|> Reference | ||
| _,_ -> failwith "one or both paths were invalid" | ||
|
||
let private parser = UnionArgParser.Create<Args>() | ||
|
||
let private (|Options|) (args : string list) = | ||
let results = parser.Parse(List.toArray args) | ||
results | ||
|
||
let (|FullPath|_|) (path : string) = | ||
try | ||
Path.GetFullPath path |> Some | ||
[<Verb("movefile", HelpText = "Move a file within a project")>] | ||
type private MoveFileOptions = | ||
{ [<Value(0, Required = true, MetaName = "project path")>] ProjectPath : string | ||
[<Value(1, Required = true, MetaName = "file path")>] FilePath : string | ||
[<Option(Required = true)>] direction : string | ||
[<Option(Default = 1)>] repeat : int } | ||
with | ||
| _ -> None | ||
|
||
let commandUsage = "projekt (init|reference|movefile|addfile|delfile|version) /path/to/project [/path/to/(file|project)]" | ||
member x.ToOperation = | ||
match x.ProjectPath, x.FilePath, Direction.Create x.direction with | ||
| FullPath project, FullPath file, dir -> | ||
{ ProjPath = project | ||
FilePath = file | ||
Direction = dir | ||
Repeat = x.repeat } | ||
|> MoveFile | ||
| _,_,_ -> failwith "invalid paths or direction" | ||
|
||
let parse (ToList args) : Result<Operation> = | ||
try | ||
match args with | ||
| [] -> | ||
parser.Usage commandUsage | ||
|> Failure | ||
[<Verb("addfile", HelpText = "Add a file to a project")>] | ||
type private AddFileOptions = | ||
{ [<Value(0, Required = true, MetaName = "project path")>] ProjectPath : string | ||
[<Value(1, Required = true, MetaName = "file path")>] FilePath : string | ||
[<Option>] link : string option | ||
[<Option(Default = true)>] compile : bool } | ||
with | ||
member x.ToOperation = | ||
match x.ProjectPath, x.FilePath with | ||
| FullPath project, FullPath file -> | ||
{ ProjPath = project | ||
FilePath = file | ||
Link = x.link | ||
Compile = x.compile } | ||
|> AddFile | ||
| _,_ -> failwith "invalid paths" | ||
|
||
| ToLower "version" :: _ -> | ||
Success Version | ||
[<Verb("delfile", HelpText = "Delete a file from a project")>] | ||
type private DelFileOptions = | ||
{ [<Value(0, Required = true, MetaName = "project path")>] ProjectPath : string | ||
[<Value(1, Required = true, MetaName = "file path")>] FilePath : string } | ||
with | ||
member x.ToOperation = | ||
match x.ProjectPath, x.FilePath with | ||
| FullPath project, FullPath file -> | ||
// typing needed here because of the duplication between MoveFileData and DelFileData records | ||
// TODO: maybe consolidate? | ||
({ ProjPath = project | ||
FilePath = file } : DelFileData) | ||
|> DelFile | ||
| _,_ -> failwith "invalid paths" | ||
|
||
| ToLower "init" :: FullPath path :: Options opts -> | ||
let template = templateArg opts | ||
let fxv = frameworkVersionArg opts | ||
let org = | ||
match opts.TryGetResult(<@ Organisation @>) with | ||
| Some org -> org | ||
| None -> "MyOrg" | ||
Init (ProjectInitData.create (path, template, fxv, org)) |> Success | ||
|
||
| ToLower "addfile" :: FullPath project :: FullPath file :: Options opts -> | ||
let compile = opts.GetResult(<@ Compile @>, true) | ||
AddFile { ProjPath = project | ||
FilePath = file | ||
Link = opts.TryGetResult <@ Link @> | ||
Compile = compile } | ||
|> Success | ||
|
||
| [ToLower "delfile"; FullPath project; FullPath file] -> | ||
DelFile { ProjPath = project; FilePath = file } | ||
|> Success | ||
|
||
| ToLower "movefile" :: FullPath project :: FullPath file :: Options opts | ||
when opts.Contains <@ Direction @> -> | ||
let private parser = CommandLine.Parser.Default | ||
|
||
let direction = opts.PostProcessResult(<@ Direction @>, parseDirection) | ||
MoveFile { ProjPath = project | ||
FilePath = file | ||
Direction = direction | ||
Repeat = opts.GetResult(<@ Repeat @>, 1)} | ||
|> Success | ||
|
||
| [ToLower "reference"; FullPath project; FullPath reference] -> | ||
Reference { ProjPath = project; Reference = reference } |> Success | ||
let parse args = | ||
let parsed = parser.ParseArguments<InitOptions, ReferenceOptions, MoveFileOptions, AddFileOptions, DelFileOptions>(args) | ||
// tried to get fancy here with a statically resolved type param to invoke the ToOperation member on the individal option cases, but I couldn't get it to work.... | ||
|
||
| _ -> Failure (parser.Usage (sprintf "Error: '%s' is not a recognized command or received incorrect arguments.\n\n%s" args.Head commandUsage)) | ||
with | ||
| :? System.ArgumentException as e -> | ||
let lines = e.Message.Split([|'\n'|]) | ||
let msg = parser.Usage (sprintf "%s\n\n%s" lines.[0] commandUsage) | ||
Failure msg | ||
parsed.Return<InitOptions, ReferenceOptions, MoveFileOptions, AddFileOptions, DelFileOptions, Result<Operation>>( | ||
(fun (init : InitOptions) -> init.ToOperation |> Success), | ||
(fun (ref : ReferenceOptions) -> ref.ToOperation |> Success), | ||
(fun (mv : MoveFileOptions) -> mv.ToOperation |> Success), | ||
(fun (add : AddFileOptions) -> add.ToOperation |> Success), | ||
(fun (del : DelFileOptions) -> del.ToOperation |> Success), | ||
(fun errs -> errs |> Seq.map (fun e -> e.ToString()) |> String.concat ";" |> Failure) | ||
) | ||
|
This file contains 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
This file contains 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
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
are the parens needed here?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think they are, since this call is a C#-style method call which needs the whole batch of tupled arguments . If I remove the parens and the separating commas the call doesn't compile.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think if you leave the commas in you can remove the parens - minor detail tho :)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Sadly no :( I tried that earlier to no avail.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
ah well :)
On Mon, 3 Aug 2015 at 16:17 Chester Husk III [email protected]
wrote: