using command line parser · c30b7d1371 - SVN.BY: Go Git Service
Bläddra i källkod

using command line parser

Related Work Items: #22
Tiernan OToole 8 år sedan
förälder
incheckning
c30b7d1371
3 ändrade filer med 41 tillägg och 21 borttagningar
  1. 3 0
      B2Uploader/B2Uploader.csproj
  2. 37 21
      B2Uploader/Program.cs
  3. 1 0
      B2Uploader/packages.config

+ 3 - 0
B2Uploader/B2Uploader.csproj

@@ -34,6 +34,9 @@
34 34
     <WarningLevel>4</WarningLevel>
35 35
   </PropertyGroup>
36 36
   <ItemGroup>
37
+    <Reference Include="CommandLine">
38
+      <HintPath>..\packages\CommandLineParser.2.0.275-beta\lib\net45\CommandLine.dll</HintPath>
39
+    </Reference>
37 40
     <Reference Include="Newtonsoft.Json, Version=7.0.0.0, Culture=neutral, PublicKeyToken=30ad4fe6b2a6aeed, processorArchitecture=MSIL">
38 41
       <HintPath>..\packages\Newtonsoft.Json.7.0.1\lib\net45\Newtonsoft.Json.dll</HintPath>
39 42
       <Private>True</Private>

+ 37 - 21
B2Uploader/Program.cs

@@ -1,4 +1,5 @@
1 1
 using B2Classes;
2
+using CommandLine;
2 3
 using Newtonsoft.Json;
3 4
 using System;
4 5
 using System.Collections.Generic;
@@ -11,35 +12,50 @@ using System.Threading.Tasks;
11 12
 
12 13
 namespace B2Uploader
13 14
 {
14
-    class Program
15
+    class CmdLineOptions
15 16
     {
16
-        static void Main(string[] args)
17
-        {
18
-            if(args.Count() != 3)
19
-            {
20
-                Console.WriteLine("Need accountID, AppKey and Folder to upload");
21
-                return;
22
-            }
17
+        [Option('i', "accountid", HelpText = "Account ID", Required=true)]
18
+        public string AccountId { get; set; }
23 19
 
24
-            if (!Directory.Exists(args[2]))
25
-            {
26
-                Console.WriteLine("Directory to upload MUST EXIST!");
27
-                return;
28
-            }
20
+        [Option('a', "appkey", HelpText = "Application Key", Required=true)]
21
+        public string ApplicationKey { get; set; }
29 22
 
30
-            var auth = AuthorizeUser(args[0], args[1]);
31
-            var buckets = ListBuckets(new ListBucketsRequest() { accountId = auth.accountId }, auth.authorizationToken, auth.apiUrl);
23
+        [Option('d', "directory", HelpText="Directory you want to upload", Required=true)]
24
+        public string Directory {get;set;}
32 25
 
33
-            var bucket = buckets.buckets.First();
26
+        [Option('v', "verbose", HelpText="Verbose Output")]
27
+        public bool Verbose{get;set;}
28
+    }
34 29
 
35
-            foreach(string s in Directory.GetFiles(args[2]))
36
-            {
37
-                var uploadURL = GetUploadURL(new GetUploadURLRequest { bucketId = bucket.bucketId }, auth.apiUrl, auth.authorizationToken);
38
-                var response = UploadFile(uploadURL.authorizationToken, "b2/x-auto", s, uploadURL.uploadUrl);
39
-            }
30
+    class Program
31
+    {
32
+        static void Main(string[] args)
33
+        {
34
+            var result = CommandLine.Parser.Default.ParseArguments<CmdLineOptions>(args);
40 35
 
36
+            var existCode = result.MapResult(options => {
37
+                if (!Directory.Exists(options.Directory))
38
+                {
39
+                    Console.WriteLine("Directory to upload MUST EXIST!");
40
+                    return 0;
41
+                }
42
+
43
+                var auth = AuthorizeUser(options.AccountId, options.ApplicationKey);
44
+                var buckets = ListBuckets(new ListBucketsRequest() { accountId = auth.accountId }, auth.authorizationToken, auth.apiUrl);
41 45
 
46
+                var bucket = buckets.buckets.First();
42 47
 
48
+                foreach(string s in Directory.GetFiles(options.Directory))
49
+                {
50
+                    var uploadURL = GetUploadURL(new GetUploadURLRequest { bucketId = bucket.bucketId }, auth.apiUrl, auth.authorizationToken);
51
+                    var response = UploadFile(uploadURL.authorizationToken, "b2/x-auto", s, uploadURL.uploadUrl);
52
+                }
53
+                return 1;
54
+            },
55
+            errors =>{
56
+                Console.WriteLine(errors);
57
+                return 1;
58
+            });
43 59
         }
44 60
 
45 61
         static AuthorizeResponse AuthorizeUser(string accountId, string applicationKey)

+ 1 - 0
B2Uploader/packages.config

@@ -1,4 +1,5 @@
1 1
 <?xml version="1.0" encoding="utf-8"?>
2 2
 <packages>
3
+  <package id="CommandLineParser" version="2.0.275-beta" targetFramework="net45" />
3 4
   <package id="Newtonsoft.Json" version="7.0.1" targetFramework="net452" />
4 5
 </packages>