Skip to content

Commit

Permalink
Merge pull request #1 from fummicc1/feature/cli
Browse files Browse the repository at this point in the history
Add command line tool.
  • Loading branch information
fummicc1 authored Nov 17, 2024
2 parents 24b839c + f92e71a commit a9f41d0
Show file tree
Hide file tree
Showing 5 changed files with 243 additions and 21 deletions.
102 changes: 102 additions & 0 deletions .swiftpm/xcode/xcshareddata/xcschemes/GeoHash-Package.xcscheme
Original file line number Diff line number Diff line change
@@ -0,0 +1,102 @@
<?xml version="1.0" encoding="UTF-8"?>
<Scheme
LastUpgradeVersion = "1610"
version = "1.7">
<BuildAction
parallelizeBuildables = "YES"
buildImplicitDependencies = "YES"
buildArchitectures = "Automatic">
<BuildActionEntries>
<BuildActionEntry
buildForTesting = "YES"
buildForRunning = "YES"
buildForProfiling = "YES"
buildForArchiving = "YES"
buildForAnalyzing = "YES">
<BuildableReference
BuildableIdentifier = "primary"
BlueprintIdentifier = "GeoHashFramework"
BuildableName = "GeoHashFramework"
BlueprintName = "GeoHashFramework"
ReferencedContainer = "container:">
</BuildableReference>
</BuildActionEntry>
<BuildActionEntry
buildForTesting = "YES"
buildForRunning = "YES"
buildForProfiling = "YES"
buildForArchiving = "YES"
buildForAnalyzing = "YES">
<BuildableReference
BuildableIdentifier = "primary"
BlueprintIdentifier = "geohash"
BuildableName = "geohash"
BlueprintName = "geohash"
ReferencedContainer = "container:">
</BuildableReference>
</BuildActionEntry>
</BuildActionEntries>
</BuildAction>
<TestAction
buildConfiguration = "Debug"
selectedDebuggerIdentifier = "Xcode.DebuggerFoundation.Debugger.LLDB"
selectedLauncherIdentifier = "Xcode.DebuggerFoundation.Launcher.LLDB"
shouldUseLaunchSchemeArgsEnv = "YES"
shouldAutocreateTestPlan = "YES">
<Testables>
<TestableReference
skipped = "NO">
<BuildableReference
BuildableIdentifier = "primary"
BlueprintIdentifier = "GeoHashFrameworkTests"
BuildableName = "GeoHashFrameworkTests"
BlueprintName = "GeoHashFrameworkTests"
ReferencedContainer = "container:">
</BuildableReference>
</TestableReference>
</Testables>
</TestAction>
<LaunchAction
buildConfiguration = "Debug"
selectedDebuggerIdentifier = "Xcode.DebuggerFoundation.Debugger.LLDB"
selectedLauncherIdentifier = "Xcode.DebuggerFoundation.Launcher.LLDB"
launchStyle = "0"
useCustomWorkingDirectory = "NO"
ignoresPersistentStateOnLaunch = "NO"
debugDocumentVersioning = "YES"
debugServiceExtension = "internal"
allowLocationSimulation = "YES">
<MacroExpansion>
<BuildableReference
BuildableIdentifier = "primary"
BlueprintIdentifier = "geohash"
BuildableName = "geohash"
BlueprintName = "geohash"
ReferencedContainer = "container:">
</BuildableReference>
</MacroExpansion>
</LaunchAction>
<ProfileAction
buildConfiguration = "Release"
shouldUseLaunchSchemeArgsEnv = "YES"
savedToolIdentifier = ""
useCustomWorkingDirectory = "NO"
debugDocumentVersioning = "YES">
<MacroExpansion>
<BuildableReference
BuildableIdentifier = "primary"
BlueprintIdentifier = "geohash"
BuildableName = "geohash"
BlueprintName = "geohash"
ReferencedContainer = "container:">
</BuildableReference>
</MacroExpansion>
</ProfileAction>
<AnalyzeAction
buildConfiguration = "Debug">
</AnalyzeAction>
<ArchiveAction
buildConfiguration = "Release"
revealArchiveInOrganizer = "YES">
</ArchiveAction>
</Scheme>
69 changes: 49 additions & 20 deletions .swiftpm/xcode/xcshareddata/xcschemes/GeoHash.xcscheme
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,9 @@
buildForAnalyzing = "YES">
<BuildableReference
BuildableIdentifier = "primary"
BlueprintIdentifier = "GeoHashFramework"
BuildableName = "GeoHashFramework"
BlueprintName = "GeoHashFramework"
BlueprintIdentifier = "geohash"
BuildableName = "geohash"
BlueprintName = "geohash"
ReferencedContainer = "container:">
</BuildableReference>
</BuildActionEntry>
Expand All @@ -29,18 +29,6 @@
selectedLauncherIdentifier = "Xcode.DebuggerFoundation.Launcher.LLDB"
shouldUseLaunchSchemeArgsEnv = "YES"
shouldAutocreateTestPlan = "YES">
<Testables>
<TestableReference
skipped = "NO">
<BuildableReference
BuildableIdentifier = "primary"
BlueprintIdentifier = "GeoHashFrameworkTests"
BuildableName = "GeoHashFrameworkTests"
BlueprintName = "GeoHashFrameworkTests"
ReferencedContainer = "container:">
</BuildableReference>
</TestableReference>
</Testables>
</TestAction>
<LaunchAction
buildConfiguration = "Debug"
Expand All @@ -52,22 +40,63 @@
debugDocumentVersioning = "YES"
debugServiceExtension = "internal"
allowLocationSimulation = "YES">
<BuildableProductRunnable
runnableDebuggingMode = "0">
<BuildableReference
BuildableIdentifier = "primary"
BlueprintIdentifier = "geohash"
BuildableName = "geohash"
BlueprintName = "geohash"
ReferencedContainer = "container:">
</BuildableReference>
</BuildableProductRunnable>
<CommandLineArguments>
<CommandLineArgument
argument = "35.681382"
isEnabled = "NO">
</CommandLineArgument>
<CommandLineArgument
argument = "--help"
isEnabled = "NO">
</CommandLineArgument>
<CommandLineArgument
argument = "135.681382"
isEnabled = "NO">
</CommandLineArgument>
<CommandLineArgument
argument = "--coordinate &quot;35.681382,139.766084&quot;"
isEnabled = "YES">
</CommandLineArgument>
<CommandLineArgument
argument = "139.766084"
isEnabled = "NO">
</CommandLineArgument>
<CommandLineArgument
argument = "--length 8"
isEnabled = "NO">
</CommandLineArgument>
<CommandLineArgument
argument = "--length 10"
isEnabled = "YES">
</CommandLineArgument>
</CommandLineArguments>
</LaunchAction>
<ProfileAction
buildConfiguration = "Release"
shouldUseLaunchSchemeArgsEnv = "YES"
savedToolIdentifier = ""
useCustomWorkingDirectory = "NO"
debugDocumentVersioning = "YES">
<MacroExpansion>
<BuildableProductRunnable
runnableDebuggingMode = "0">
<BuildableReference
BuildableIdentifier = "primary"
BlueprintIdentifier = "GeoHashFramework"
BuildableName = "GeoHashFramework"
BlueprintName = "GeoHashFramework"
BlueprintIdentifier = "geohash"
BuildableName = "geohash"
BlueprintName = "geohash"
ReferencedContainer = "container:">
</BuildableReference>
</MacroExpansion>
</BuildableProductRunnable>
</ProfileAction>
<AnalyzeAction
buildConfiguration = "Debug">
Expand Down
15 changes: 15 additions & 0 deletions Package.resolved
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
{
"originHash" : "cd9ca0ea0320522c8fe77ddafee840264ac5a469caedc78393244502d02dd387",
"pins" : [
{
"identity" : "swift-argument-parser",
"kind" : "remoteSourceControl",
"location" : "https://github.com/apple/swift-argument-parser",
"state" : {
"revision" : "41982a3656a71c768319979febd796c6fd111d5c",
"version" : "1.5.0"
}
}
],
"version" : 3
}
20 changes: 19 additions & 1 deletion Package.swift
Original file line number Diff line number Diff line change
Expand Up @@ -9,14 +9,32 @@ let package = Package(
// Products define the executables and libraries a package produces, making them visible to other packages.
.library(
name: "GeoHashFramework",
targets: ["GeoHashFramework"])
targets: ["GeoHashFramework"]
),
.executable(
name: "geohash",
targets: ["GeoHashCLI"]
),
],
dependencies: [
.package(
url: "https://github.com/apple/swift-argument-parser",
.upToNextMajor(from: "1.5.0")
),
],
targets: [
// Targets are the basic building blocks of a package, defining a module or a test suite.
// Targets can depend on other targets in this package and products from dependencies.
.target(
name: "GeoHashFramework"
),
.executableTarget(
name: "GeoHashCLI",
dependencies: [
"GeoHashFramework",
.product(name: "ArgumentParser", package: "swift-argument-parser")
]
),
.testTarget(
name: "GeoHashFrameworkTests",
dependencies: ["GeoHashFramework"]
Expand Down
58 changes: 58 additions & 0 deletions Sources/GeoHashCLI/GeoHashCLI.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
//
// GeoHashCLI.swift
// GeoHashCLI
//
// Created by Fumiya Tanaka on 2024/11/17.
//

import ArgumentParser
import GeoHashFramework

@main
struct GeoHashCLI: ParsableCommand {

@Option(name: .shortAndLong, help: "length of GeoHash")
var length: Int = 8

@Argument(help: "latitude of coordinate")
var latitude: Double?

@Argument(help: "longitude of coordinate")
var longitude: Double?

@Option(help: "concatenated coordinate formatted as latitude,longitude")
var coordinate: String?

mutating func run() throws {
precondition(
(latitude != nil && longitude != nil) || coordinate != nil
)
let precision = GeoHashBitsPrecision.exact(digits: length * 5)

let geoHash: GeoHash

if let coordinate {
let components = coordinate.split(separator: ",")
guard components.count == 2 else {
throw ValidationError("Coordinate must be formatted as latitude,longitude")
}
latitude = Double(components[0])
longitude = Double(components[1])

geoHash = .init(
latitude: latitude!,
longitude: longitude!,
precision: precision
)
} else if let latitude, let longitude {
geoHash = .init(
latitude: latitude,
longitude: longitude,
precision: precision
)
} else {
throw ValidationError("Coordinate or latitude and longitude must be provided.")
}
print(geoHash.geoHash)
}
}

0 comments on commit a9f41d0

Please sign in to comment.