Skip to content

Commit 6468075

Browse files
committed
Add argument parser, support copy the url in different formats
1 parent f007e41 commit 6468075

File tree

3 files changed

+83
-17
lines changed

3 files changed

+83
-17
lines changed

Package.swift

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,8 @@ let package = Package(
99
// Dependencies declare other packages that this package depends on.
1010
// .package(url: /* package url */, from: "1.0.0"),
1111
// Rainbow to support console output in color
12-
.package(url: "https://github.com/onevcat/Rainbow", from: "3.0.0")
12+
.package(url: "https://github.com/onevcat/Rainbow", from: "3.0.0"),
13+
.package(url: "https://github.com/apple/swift-package-manager.git", from: "0.1.0")
1314
],
1415
targets: [
1516
// Targets are the basic building blocks of a package. A target can define a module or a test suite.
@@ -19,6 +20,6 @@ let package = Package(
1920
dependencies: ["clip2imgurCore"]),
2021
.target(
2122
name: "clip2imgurCore",
22-
dependencies: ["Rainbow"])
23+
dependencies: ["Rainbow", "Utility"])
2324
]
2425
)

Sources/clip2imgurCore/CommandLineInterface.swift

Lines changed: 77 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -8,23 +8,78 @@
88
import Foundation
99
import Cocoa
1010
import Rainbow
11+
import Utility
12+
import Basic
13+
14+
private enum copyFormat{
15+
case markdown
16+
case html
17+
case plain
18+
}
1119

1220
// A class managing the commmand line interface for this project
1321
public class CommandLineInterface{
14-
public let argc: Int32
15-
public let argv: [String]
22+
public let argc = CommandLine.argc
23+
public let argv = CommandLine.arguments
1624
private let api = ImgurAPI()
1725

26+
private let parser: ArgumentParser
27+
private let useMDFormat: OptionArgument<Bool>
28+
private let useHTMLFormat: OptionArgument<Bool>
29+
private let doNotCopy: OptionArgument<Bool>
30+
1831
public init(){
19-
self.argc = CommandLine.argc
20-
self.argv = CommandLine.arguments
32+
// Init the argument parser and register flags
33+
let overview = "clip2imgur is a simple CLI that uploads your image in clipboard " +
34+
"to Imgur."
35+
self.parser = ArgumentParser(usage: "<flags>",
36+
overview: overview)
37+
self.useMDFormat = self.parser.add(
38+
option: "--markdown",
39+
shortName: "-m",
40+
kind: Bool.self,
41+
usage: "Copy the image url in Markdown format"
42+
)
43+
44+
self.useHTMLFormat = self.parser.add(
45+
option: "--html",
46+
shortName: "-t",
47+
kind: Bool.self,
48+
usage: "Copy the image url in HTML format"
49+
)
50+
51+
self.doNotCopy = self.parser.add(
52+
option: "--nocopy",
53+
shortName: "-n",
54+
kind: Bool.self,
55+
usage: "Do not copy the image url after submitting"
56+
)
2157
}
2258

23-
// The main logic is implemented here
59+
// The app main logic is implemented here
2460
public func run(){
25-
let cliImage = ClipboardImage()
26-
let url = self.postImage(from: cliImage.getClipboardImageBase64())
27-
copyToClipboard(from: url)
61+
do {
62+
// Parse the arguments
63+
let parsedArguments = try parser.parse(Array(self.argv.dropFirst()))
64+
65+
// Post the user's image
66+
let cliImage = ClipboardImage()
67+
let url = self.postImage(from: cliImage.getClipboardImageBase64())
68+
69+
// Decide how to copy the returned url
70+
if (parsedArguments.get(self.doNotCopy) == true){
71+
return
72+
} else if (parsedArguments.get(self.useHTMLFormat) == true){
73+
copyToClipboard(from: url, using: .html)
74+
} else if (parsedArguments.get(self.useMDFormat) == true){
75+
copyToClipboard(from: url, using: .markdown)
76+
} else {
77+
copyToClipboard(from: url, using: .plain)
78+
}
79+
} catch let error {
80+
printError(error.localizedDescription)
81+
self.parser.printUsage(on: stdoutStream)
82+
}
2883
print("The image url is coppied to your clipboard.".blue.bold)
2984
}
3085

@@ -38,7 +93,7 @@ public class CommandLineInterface{
3893
var response: String?
3994
while(true) {
4095
print("[Enter 'yes' to start authorization, enter 'no' to post anonymously]")
41-
print("> ".blink, terminator: "")
96+
print("> ".bold, terminator: "")
4297
response = readLine()
4398
let legalResponses = ["yes", "no", "\'yes\'", "\'no\'", "y", "n"]
4499
if (response != nil && legalResponses.contains(response!)){
@@ -75,9 +130,19 @@ public func printError(_ errorMessage: String){
75130
fputs("Error: \(errorMessage)\n".red.bold, stderr)
76131
}
77132

78-
// Copy the string to user's clipboard
79-
private func copyToClipboard(from str: String){
133+
// Copy the url to user's clipboard
134+
private func copyToClipboard(from url: String, using format: copyFormat){
80135
let clipboard = NSPasteboard.general
81136
clipboard.declareTypes([.string], owner: nil)
82-
clipboard.setString(str, forType: .string)
137+
var formatedURL: String
138+
// Copy the url in the specified format
139+
switch format {
140+
case .html:
141+
formatedURL = "<img src=\"\(url)\">"
142+
case .markdown:
143+
formatedURL = "![](\(url))"
144+
case .plain:
145+
formatedURL = url
146+
}
147+
clipboard.setString(formatedURL, forType: .string)
83148
}

Sources/clip2imgurCore/ImgurAPI.swift

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -115,7 +115,7 @@ public class ImgurAPI: Imgurable{
115115
print("The new URL looks like " +
116116
"https://imgur.com/?state=copy-url#access_token=...\n".underline)
117117
print("(4) Paste the full URL below: ".blue.bold)
118-
print("> ".blink, terminator: "")
118+
print("> ".bold, terminator: "")
119119
response = readLine()
120120

121121
if (response != nil && response!.hasPrefix("https://imgur.com")){
@@ -223,7 +223,7 @@ public class ImgurAPI: Imgurable{
223223

224224
link = json_data["link"] as? String
225225
if (link == nil){
226-
printError("Failed to fetch link")
226+
printError("Failed to fetch the link")
227227
exit(-1)
228228
}
229229
} catch let error as NSError {
@@ -236,8 +236,8 @@ public class ImgurAPI: Imgurable{
236236
)
237237
// Start the task and wait for it to complete
238238
dataTask.resume()
239+
print("Uploading...")
239240
sema.wait()
240-
241241
print("\n🎉 Successfully uploaded your screenshot to Imgur at \(link!.underline)\n")
242242
return link!
243243
}

0 commit comments

Comments
 (0)