Skip to content

Commit

Permalink
exit if it has directory problem
Browse files Browse the repository at this point in the history
  • Loading branch information
PoplarYang committed Mar 25, 2020
1 parent 37a53fa commit 6b09c07
Show file tree
Hide file tree
Showing 5 changed files with 74 additions and 9 deletions.
5 changes: 3 additions & 2 deletions Package.swift
Original file line number Diff line number Diff line change
Expand Up @@ -8,14 +8,15 @@ let package = Package(
dependencies: [
// Dependencies declare other packages that this package depends on.
// .package(url: /* package url */, from: "1.0.0"),
.package(url: "https://github.com/IngmarStein/CommandLineKit", from: "2.3.0")
.package(url: "https://github.com/IngmarStein/CommandLineKit", from: "2.3.0"),
.package(url: "https://github.com/kylef/PathKit", from: "1.0.0"),
],
targets: [
// Targets are the basic building blocks of a package. A target can define a module or a test suite.
// Targets can depend on other targets in this package, and on products in packages which this package depends on.
.target(
name: "clipboard",
dependencies: ["CommandLineKit"]),
dependencies: ["CommandLineKit", "PathKit"]),
.testTarget(
name: "clipboardTests",
dependencies: ["clipboard"]),
Expand Down
14 changes: 11 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,17 @@ $ swift build

## Usage
```shell
$ ./clipboard
$ ./clipboard [ -o /path/to/image.png ]


$ ./clipboard -h
Usage: ./clipboard [options]
-o, --out:
File path you want to save PNG to, default: ./clipboard.png
-h, --help:
Prints a help message.
```

## TODO
- [ ] Judge clipboard content
- [ ] support command line argvs, such as save-dir, output-name
- [] Judge clipboard content
- [X] support command line argvs, such as save-dir, output-name
29 changes: 25 additions & 4 deletions Sources/clipboard/main.swift
Original file line number Diff line number Diff line change
Expand Up @@ -8,21 +8,31 @@

import Foundation
import AppKit
import PathKit
import CommandLineKit

class SaveManager{

let cli = CommandLineKit.CommandLine()

// command line argvs parse
func argvParse() -> (String) {
let cli = CommandLineKit.CommandLine()

let filePath = StringOption(shortFlag: "o",
longFlag: "out",
required: false,
helpMessage: "File path you want to save PNG to, default: ./clipboard.png")
cli.addOptions(filePath)
let help = BoolOption(shortFlag: "h",
longFlag: "help",
helpMessage: "Prints a help message.")
cli.addOptions(filePath, help)

do {
try cli.parse()

if help.wasSet && !filePath.wasSet {
cli.printUsage()
exit(-1)
}
return filePath.value ?? "./clipboard.png"
} catch {
cli.printUsage(error)
Expand Down Expand Up @@ -59,10 +69,20 @@ class SaveManager{
// Image PNG
if let data = pasteboard.data(forType: .png){
let pngURL = URL(fileURLWithPath: filePath)

let folderURL = pngURL.deletingLastPathComponent()
let path = Path(folderURL.path)
if !path.isDirectory {
print("\(folderURL.path) not directory.")
cli.printUsage()
exit(-1)
}

do {
try data.write(to: pngURL, options: .atomic)
} catch {
print(error)
cli.printUsage(error)
exit(-1)
}
print("File Path: \(pngURL)")
print("File Type: PNG")
Expand All @@ -79,6 +99,7 @@ class SaveManager{
// }

print("File Type: not png")
cli.printUsage()
exit(-1)
}
}
Expand Down
17 changes: 17 additions & 0 deletions clipboard.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@

/* Begin PBXBuildFile section */
E01F2FF3242B04DC004B872E /* CommandLineKit in Frameworks */ = {isa = PBXBuildFile; productRef = E01F2FF2242B04DC004B872E /* CommandLineKit */; };
E01F2FF6242B22CC004B872E /* PathKit in Frameworks */ = {isa = PBXBuildFile; productRef = E01F2FF5242B22CC004B872E /* PathKit */; };
OBJ_24 /* main.swift in Sources */ = {isa = PBXBuildFile; fileRef = OBJ_10 /* main.swift */; };
OBJ_31 /* Package.swift in Sources */ = {isa = PBXBuildFile; fileRef = OBJ_6 /* Package.swift */; };
OBJ_42 /* XCTestManifests.swift in Sources */ = {isa = PBXBuildFile; fileRef = OBJ_13 /* XCTestManifests.swift */; };
Expand Down Expand Up @@ -60,6 +61,7 @@
isa = PBXFrameworksBuildPhase;
buildActionMask = 0;
files = (
E01F2FF6242B22CC004B872E /* PathKit in Frameworks */,
E01F2FF3242B04DC004B872E /* CommandLineKit in Frameworks */,
);
runOnlyForDeploymentPostprocessing = 0;
Expand Down Expand Up @@ -160,6 +162,7 @@
name = clipboard;
packageProductDependencies = (
E01F2FF2242B04DC004B872E /* CommandLineKit */,
E01F2FF5242B22CC004B872E /* PathKit */,
);
productName = clipboard;
productReference = "clipboard::clipboard::Product" /* clipboard */;
Expand Down Expand Up @@ -201,6 +204,7 @@
mainGroup = OBJ_5;
packageReferences = (
E01F2FF1242B04DC004B872E /* XCRemoteSwiftPackageReference "CommandLineKit" */,
E01F2FF4242B22CC004B872E /* XCRemoteSwiftPackageReference "PathKit" */,
);
productRefGroup = OBJ_15 /* Products */;
projectDirPath = "";
Expand Down Expand Up @@ -513,6 +517,14 @@
minimumVersion = 2.3.0;
};
};
E01F2FF4242B22CC004B872E /* XCRemoteSwiftPackageReference "PathKit" */ = {
isa = XCRemoteSwiftPackageReference;
repositoryURL = "https://github.com/kylef/PathKit";
requirement = {
kind = upToNextMajorVersion;
minimumVersion = 1.0.0;
};
};
/* End XCRemoteSwiftPackageReference section */

/* Begin XCSwiftPackageProductDependency section */
Expand All @@ -521,6 +533,11 @@
package = E01F2FF1242B04DC004B872E /* XCRemoteSwiftPackageReference "CommandLineKit" */;
productName = CommandLineKit;
};
E01F2FF5242B22CC004B872E /* PathKit */ = {
isa = XCSwiftPackageProductDependency;
package = E01F2FF4242B22CC004B872E /* XCRemoteSwiftPackageReference "PathKit" */;
productName = PathKit;
};
/* End XCSwiftPackageProductDependency section */
};
rootObject = OBJ_1 /* Project object */;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,24 @@
"revision": "688ec349bf793fbdd3562d2e301c189db2f7a655",
"version": "2.3.0"
}
},
{
"package": "PathKit",
"repositoryURL": "https://github.com/kylef/PathKit",
"state": {
"branch": null,
"revision": "73f8e9dca9b7a3078cb79128217dc8f2e585a511",
"version": "1.0.0"
}
},
{
"package": "Spectre",
"repositoryURL": "https://github.com/kylef/Spectre.git",
"state": {
"branch": null,
"revision": "f14ff47f45642aa5703900980b014c2e9394b6e5",
"version": "0.9.0"
}
}
]
},
Expand Down

0 comments on commit 6b09c07

Please sign in to comment.