Skip to content

Commit abdc9e9

Browse files
committed
rewrite hardcoded arguments
1 parent 8a9f596 commit abdc9e9

File tree

4 files changed

+86
-78
lines changed

4 files changed

+86
-78
lines changed

Package.resolved

+9
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,15 @@
99
"revision": "4356ec54e073741449640d3d50a1fd24fd1e1b8b",
1010
"version": "2.1.0"
1111
}
12+
},
13+
{
14+
"package": "swift-argument-parser",
15+
"repositoryURL": "https://github.com/apple/swift-argument-parser",
16+
"state": {
17+
"branch": null,
18+
"revision": "83b23d940471b313427da226196661856f6ba3e0",
19+
"version": "0.4.4"
20+
}
1221
}
1322
]
1423
},

Package.swift

+2-1
Original file line numberDiff line numberDiff line change
@@ -19,13 +19,14 @@ let package = Package(
1919
// Dependencies declare other packages that this package depends on.
2020
// .package(url: /* package url */, from: "1.0.0"),
2121
.package(url: "https://github.com/gumob/PunycodeSwift.git", from: "2.0.0"),
22+
.package(url: "https://github.com/apple/swift-argument-parser", from: "0.4.0"),
2223
],
2324
targets: [
2425
// Targets are the basic building blocks of a package. A target can define a module or a test suite.
2526
// Targets can depend on other targets in this package, and on products in packages which this package depends on.
2627
.target(
2728
name: "CommandLineWrapper",
28-
dependencies: ["ContentBlockerConverter", "Shared"]),
29+
dependencies: ["ContentBlockerConverter", "Shared", "ArgumentParser"]),
2930
.target(
3031
name: "ContentBlockerConverter",
3132
dependencies: ["Punnycode", "Shared"]),

README.md

+6-6
Original file line numberDiff line numberDiff line change
@@ -38,17 +38,17 @@ The result contains following properties:
3838
- advancedBlocking: json string of advanced blocking rules
3939

4040
### How to use converter from command line:
41-
4241
```
43-
./ConverterTool -safariVersion=14 -optimize=true -advancedBlocking=false <<STDIN -o other --options
44-
test_rule_one
45-
test_rule_two
46-
STDIN
42+
ConverterTool [--safari-version <safari-version>] [--optimize <optimize>] [--advanced-blocking <advanced-blocking>] [<rules>]
43+
```
44+
e.g.
45+
```
46+
cat rules.txt | ./ConverterTool --safari-version 13 --optimize false --advanced-blocking false
4747
```
4848

4949
The tool then reads stdin line by line for rule until an empty line.
5050

51-
### How to release on Github
51+
### How to release on GitHub
5252

5353
Push a new tag in `v*.*.*` format, then provided github action is intended to build and publish new release with an asset binary.
5454

Sources/CommandLineWrapper/main.swift

+69-71
Original file line numberDiff line numberDiff line change
@@ -1,95 +1,93 @@
11
import Foundation
22
import ContentBlockerConverter
33
import Shared
4-
5-
/**
6-
* Command line wrapper
7-
* Usage:
8-
* ./CommandLineWrapper -safariVersion=14 -optimize=true -advancedBlocking=false
9-
*/
4+
import ArgumentParser
105

116
func writeToStdError(str: String) {
12-
let handle = FileHandle.standardError;
13-
7+
let handle = FileHandle.standardError
8+
149
if let data = str.data(using: String.Encoding.utf8, allowLossyConversion: false) {
1510
handle.write(data)
1611
}
1712
}
1813

1914
func writeToStdOut(str: String) {
20-
let handle = FileHandle.standardOutput;
21-
15+
let handle = FileHandle.standardOutput
16+
2217
if let data = str.data(using: String.Encoding.utf8, allowLossyConversion: false) {
2318
handle.write(data)
2419
}
2520
}
2621

2722
func encodeJson(_ result: ConversionResult) throws -> String {
28-
let encoder = JSONEncoder();
23+
let encoder = JSONEncoder()
2924
encoder.outputFormatting = .prettyPrinted
30-
31-
let json = try encoder.encode(result);
32-
return String(data: json, encoding: .utf8)!.replacingOccurrences(of: "\\/", with: "/");
25+
26+
let json = try encoder.encode(result)
27+
return String(data: json, encoding: .utf8)!.replacingOccurrences(of: "\\/", with: "/")
3328
}
3429

35-
do {
36-
Logger.log("AG: Conversion started");
37-
38-
let arguments: [String] = CommandLine.arguments;
39-
if (arguments.count < 4) {
40-
writeToStdError(str: "AG: Invalid arguments: Usage: ./CommandLineWrapper -safariVersion=14 -optimize=false -advancedBlocking)");
41-
exit(EXIT_FAILURE);
42-
}
43-
44-
let safariVersionIndex = String.Index(utf16Offset: 15, in: arguments[1]);
45-
let safariVersionStr = arguments[1][safariVersionIndex...];
46-
47-
guard let safariVersionNum = Int(safariVersionStr) else {
48-
throw SafariVersionError.invalidSafariVersion();
49-
};
50-
51-
guard let safariVersion = SafariVersion(rawValue: safariVersionNum) else {
52-
throw SafariVersionError.unsupportedSafariVersion();
53-
};
54-
55-
Logger.log("AG: Safari version: \(safariVersion)");
56-
57-
let optimize = arguments[2] == "-optimize=true";
58-
Logger.log("AG: Optimize: \(optimize)");
59-
60-
let advancedBlocking = arguments[3] == "-advancedBlocking=true";
61-
Logger.log("AG: AdvancedBlocking: \(advancedBlocking)");
62-
63-
var rules = [String]();
64-
var line: String? = nil;
65-
while (true) {
66-
line = readLine(strippingNewline: true);
67-
if (line == nil || line == "") {
68-
break;
30+
/**
31+
* Converter tool
32+
* Usage:
33+
* "cat rules.txt | ./ConverterTool -safariVersion=14 -optimize=true -advancedBlocking=false"
34+
*/
35+
struct ConverterTool: ParsableCommand {
36+
static let configuration = CommandConfiguration(commandName: "ConverterTool")
37+
38+
@Option(name: .shortAndLong, help: "Safari version.")
39+
var safariVersion: Int = 13
40+
41+
@Option(name: .shortAndLong, help: "Optimize.")
42+
var optimize = false
43+
44+
@Option(name: .shortAndLong, help: "Advanced blocking.")
45+
var advancedBlocking = false
46+
47+
@Argument(help: "Reads rules from standard input.")
48+
var rules: String?
49+
50+
mutating func run() throws {
51+
guard let safariVersion = SafariVersion(rawValue: safariVersion) else {
52+
throw SafariVersionError.unsupportedSafariVersion()
6953
}
70-
71-
rules.append(line!);
72-
}
73-
74-
Logger.log("AG: Rules to convert: \(rules.count)");
75-
76-
let result: ConversionResult? = ContentBlockerConverter().convertArray(
77-
rules: rules, safariVersion: safariVersion, optimize: optimize, advancedBlocking: advancedBlocking
78-
);
79-
80-
Logger.log("AG: Conversion done");
81-
82-
if (result == nil) {
83-
writeToStdError(str: "AG: ContentBlockerConverter: Empty result.");
84-
exit(EXIT_FAILURE);
54+
55+
Logger.log("Safari version - \(safariVersion)")
56+
Logger.log("Optimize - \(optimize)")
57+
Logger.log("Advanced blocking - \(advancedBlocking)")
58+
59+
var rules: [String] = []
60+
var line: String?
61+
while true {
62+
line = readLine(strippingNewline: true)
63+
guard let unwrappedLine = line, !unwrappedLine.isEmpty else {
64+
break
65+
}
66+
67+
rules.append(unwrappedLine)
68+
}
69+
70+
Logger.log("Rules to convert: \(rules.count)")
71+
72+
let result: ConversionResult? = ContentBlockerConverter()
73+
.convertArray(
74+
rules: rules,
75+
safariVersion: safariVersion,
76+
optimize: optimize,
77+
advancedBlocking: advancedBlocking
78+
)
79+
80+
Logger.log("Conversion done.")
81+
82+
guard let result = result else {
83+
writeToStdError(str: "ContentBlockerConverter: Empty result.")
84+
Foundation.exit(EXIT_FAILURE)
85+
}
86+
87+
let encoded = try encodeJson(result)
88+
89+
writeToStdOut(str: "\(encoded)")
8590
}
86-
87-
let encoded = try encodeJson(result!);
88-
89-
writeToStdOut(str: "\(encoded)");
90-
exit(EXIT_SUCCESS);
91-
} catch {
92-
writeToStdError(str: "AG: ContentBlockerConverter: Unexpected error: \(error)");
93-
exit(EXIT_FAILURE);
9491
}
9592

93+
ConverterTool.main()

0 commit comments

Comments
 (0)