Skip to content

Commit ee3d137

Browse files
committed
refactored subcommands into their own files
1 parent bec420b commit ee3d137

File tree

3 files changed

+76
-116
lines changed

3 files changed

+76
-116
lines changed

utiluti.xcodeproj/project.pbxproj

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,8 @@
1111
C6C84549291AB2E800AF407A /* Algorithms in Frameworks */ = {isa = PBXBuildFile; productRef = C6C84548291AB2E800AF407A /* Algorithms */; };
1212
C6C8454B291AB30100AF407A /* LSKit.swift in Sources */ = {isa = PBXBuildFile; fileRef = C6C8454A291AB30100AF407A /* LSKit.swift */; };
1313
C6C8454E291AB58200AF407A /* ArgumentParser in Frameworks */ = {isa = PBXBuildFile; productRef = C6C8454D291AB58200AF407A /* ArgumentParser */; };
14+
C6C84552291CEF1500AF407A /* URLScheme.swift in Sources */ = {isa = PBXBuildFile; fileRef = C6C84551291CEF1500AF407A /* URLScheme.swift */; };
15+
C6C84554291CEFC200AF407A /* TypeCommands.swift in Sources */ = {isa = PBXBuildFile; fileRef = C6C84553291CEFC200AF407A /* TypeCommands.swift */; };
1416
/* End PBXBuildFile section */
1517

1618
/* Begin PBXCopyFilesBuildPhase section */
@@ -29,6 +31,8 @@
2931
C6C8453D291AB2CF00AF407A /* utiluti */ = {isa = PBXFileReference; explicitFileType = "compiled.mach-o.executable"; includeInIndex = 0; path = utiluti; sourceTree = BUILT_PRODUCTS_DIR; };
3032
C6C84540291AB2CF00AF407A /* main.swift */ = {isa = PBXFileReference; indentWidth = 2; lastKnownFileType = sourcecode.swift; path = main.swift; sourceTree = "<group>"; tabWidth = 2; };
3133
C6C8454A291AB30100AF407A /* LSKit.swift */ = {isa = PBXFileReference; fileEncoding = 4; indentWidth = 2; lastKnownFileType = sourcecode.swift; path = LSKit.swift; sourceTree = "<group>"; tabWidth = 2; };
34+
C6C84551291CEF1500AF407A /* URLScheme.swift */ = {isa = PBXFileReference; indentWidth = 2; lastKnownFileType = sourcecode.swift; path = URLScheme.swift; sourceTree = "<group>"; tabWidth = 2; };
35+
C6C84553291CEFC200AF407A /* TypeCommands.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = TypeCommands.swift; sourceTree = "<group>"; };
3236
/* End PBXFileReference section */
3337

3438
/* Begin PBXFrameworksBuildPhase section */
@@ -50,7 +54,9 @@
5054
C6C8453F291AB2CF00AF407A /* utiluti */,
5155
C6C8453E291AB2CF00AF407A /* Products */,
5256
);
57+
indentWidth = 2;
5358
sourceTree = "<group>";
59+
tabWidth = 2;
5460
};
5561
C6C8453E291AB2CF00AF407A /* Products */ = {
5662
isa = PBXGroup;
@@ -64,6 +70,8 @@
6470
isa = PBXGroup;
6571
children = (
6672
C6C84540291AB2CF00AF407A /* main.swift */,
73+
C6C84553291CEFC200AF407A /* TypeCommands.swift */,
74+
C6C84551291CEF1500AF407A /* URLScheme.swift */,
6775
C6C8454A291AB30100AF407A /* LSKit.swift */,
6876
);
6977
path = utiluti;
@@ -135,7 +143,9 @@
135143
isa = PBXSourcesBuildPhase;
136144
buildActionMask = 2147483647;
137145
files = (
146+
C6C84554291CEFC200AF407A /* TypeCommands.swift in Sources */,
138147
C6C84541291AB2CF00AF407A /* main.swift in Sources */,
148+
C6C84552291CEF1500AF407A /* URLScheme.swift in Sources */,
139149
C6C8454B291AB30100AF407A /* LSKit.swift in Sources */,
140150
);
141151
runOnlyForDeploymentPostprocessing = 0;

utiluti/URLScheme.swift

Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
//
2+
// URLScheme.swift
3+
// utiluti
4+
//
5+
// Created by Armin Briegel on 2022-11-10.
6+
//
7+
8+
import Foundation
9+
import ArgumentParser
10+
11+
struct URLCommands: ParsableCommand {
12+
static var configuration = CommandConfiguration(
13+
commandName: "url",
14+
abstract: "Manipulate default URL scheme handlers",
15+
subcommands: [ Get.self, List.self, Set.self],
16+
defaultSubcommand: Get.self)
17+
18+
struct URLScheme: ParsableArguments {
19+
@Argument(help: "the url scheme, e.g. 'http' or 'mailto'") var value: String
20+
}
21+
22+
struct Get: ParsableCommand {
23+
static var configuration
24+
= CommandConfiguration(abstract: "Get the path to the default application.")
25+
26+
@OptionGroup var scheme: URLScheme
27+
28+
func run() {
29+
let appURL = LSKit.defaultAppURL(forScheme: scheme.value)
30+
print(appURL?.path ?? "no default app found")
31+
}
32+
}
33+
34+
struct List: ParsableCommand {
35+
static var configuration
36+
= CommandConfiguration(abstract: "List all applications that can handle this URL scheme.")
37+
38+
@OptionGroup var scheme: URLScheme
39+
40+
func run() {
41+
let appURLs = LSKit.appURLs(forScheme: scheme.value)
42+
43+
for appURL in appURLs {
44+
print(appURL.path)
45+
}
46+
}
47+
}
48+
49+
struct Set: ParsableCommand {
50+
static var configuration
51+
= CommandConfiguration(abstract: "Set the default app for this URL scheme.")
52+
53+
@OptionGroup var scheme: URLScheme
54+
@Argument(help: "bundle identifier for the app") var identifier: String
55+
56+
func run() {
57+
let result = LSKit.setDefaultApp(identifier: identifier, forScheme: scheme.value)
58+
59+
if result == 0 {
60+
print("set \(identifier) for \(scheme.value)")
61+
} else {
62+
print("cannot set default app for \(scheme.value) (error \(result))")
63+
}
64+
}
65+
}
66+
}

utiluti/main.swift

Lines changed: 0 additions & 116 deletions
Original file line numberDiff line numberDiff line change
@@ -13,122 +13,6 @@ struct UtilUTI: ParsableCommand {
1313
commandName: "utiluti",
1414
abstract: "Read and set default URL scheme and file type handlers.",
1515
subcommands: [URLCommands.self, TypeCommands.self])
16-
17-
struct URLCommands: ParsableCommand {
18-
static var configuration = CommandConfiguration(
19-
commandName: "url",
20-
abstract: "Manipulate default URL scheme handlers",
21-
subcommands: [ Get.self, List.self, Set.self],
22-
defaultSubcommand: Get.self)
23-
24-
struct URLScheme: ParsableArguments {
25-
@Argument(help: "the url scheme, e.g. 'http' or 'mailto'") var urlScheme: String
26-
}
27-
28-
struct Get: ParsableCommand {
29-
static var configuration
30-
= CommandConfiguration(abstract: "Get the path to the default application.")
31-
32-
@OptionGroup var args: URLScheme
33-
34-
func run() {
35-
let appURL = LSKit.defaultAppURL(forScheme: args.urlScheme)
36-
print(appURL?.path ?? "no default app found")
37-
}
38-
}
39-
40-
struct List: ParsableCommand {
41-
static var configuration
42-
= CommandConfiguration(abstract: "List all applications that can handle this URL scheme.")
43-
44-
@OptionGroup var args: URLScheme
45-
46-
func run() {
47-
let appURLs = LSKit.appURLs(forScheme: args.urlScheme)
48-
49-
for appURL in appURLs {
50-
print(appURL.path)
51-
}
52-
}
53-
}
54-
55-
struct Set: ParsableCommand {
56-
static var configuration
57-
= CommandConfiguration(abstract: "Set the default app for this URL scheme.")
58-
59-
@OptionGroup var args: URLScheme
60-
@Argument(help: "bundle identifier for the app") var identifier: String
61-
62-
func run() {
63-
let result = LSKit.setDefaultApp(identifier: identifier, forScheme: args.urlScheme)
64-
65-
if result == 0 {
66-
print("set \(identifier) for \(args.urlScheme)")
67-
} else {
68-
print("cannot set default app for \(args.urlScheme) (error \(result))")
69-
}
70-
}
71-
}
72-
}
73-
74-
struct TypeCommands: ParsableCommand {
75-
static var configuration = CommandConfiguration(
76-
commandName: "type",
77-
abstract: "Manipulate default file type handlers",
78-
subcommands: [Get.self, List.self, Set.self],
79-
defaultSubcommand: Get.self
80-
)
81-
82-
struct UTIdentifier: ParsableArguments {
83-
@Argument(help: "universal type identifier, e.g. 'public.html'") var utidentifier: String
84-
}
85-
86-
87-
struct Get: ParsableCommand {
88-
static var configuration
89-
= CommandConfiguration(abstract: "Get the path to the default application.")
90-
91-
@OptionGroup var args: UTIdentifier
92-
93-
func run() {
94-
let appURL = LSKit.defaultAppURL(forTypeIdentifier: args.utidentifier)
95-
print(appURL?.path ?? "no default app found")
96-
}
97-
}
98-
99-
struct List: ParsableCommand {
100-
static var configuration
101-
= CommandConfiguration(abstract: "List all applications that can handle this type identifier.")
102-
103-
@OptionGroup var args: UTIdentifier
104-
105-
func run() {
106-
let appURLs = LSKit.appURLs(forTypeIdentifier: args.utidentifier)
107-
108-
for appURL in appURLs {
109-
print(appURL.path)
110-
}
111-
}
112-
}
113-
114-
struct Set: ParsableCommand {
115-
static var configuration
116-
= CommandConfiguration(abstract: "Set the default app for this type identifier.")
117-
118-
@OptionGroup var args: UTIdentifier
119-
@Argument var identifier: String
120-
121-
func run() {
122-
let result = LSKit.setDefaultApp(identifier: identifier, forTypeIdentifier: args.utidentifier)
123-
124-
if result == 0 {
125-
print("set \(identifier) for \(args.utidentifier)")
126-
} else {
127-
print("cannot set default app for \(args.utidentifier) (error \(result))")
128-
}
129-
}
130-
}
131-
}
13216
}
13317

13418
UtilUTI.main()

0 commit comments

Comments
 (0)