<commit_before>//
//  main.swift
//  Excerptor
//
//  Created by <NAME> on 20/05/2015.
//  Copyright (c) 2015 guoc. All rights reserved.
//

import Foundation

import CommandLine

print(Process.arguments)
if Process.argc == 1 || Process.argc == 3 && Process.arguments[1] == "-NSDocumentRevisionsDebugMode" && Process.arguments[2] == "YES" {
    NSApplicationMain(Process.argc, Process.unsafeArgv)
    exit(0)
}

enum Operation: String {
    case List = "list"
    case Export = "export"
}

enum ExtractionType: String {
    case Annotation = "annotation"
}

enum Format: String {
    case Text = "text"
    case JSON = "JSON"
}

let operation = EnumOption<Operation>(longFlag: "operation", helpMessage: "'list' / 'export', 'list' by default.")
let extractionType = EnumOption<ExtractionType>(longFlag: "type", helpMessage: "Type of data to be extracted, currently only supports 'annotation', by default 'annotation'.")
let format = EnumOption<Format>(longFlag: "format", helpMessage: "Display format, currently only supports 'text' and 'JSON', 'text' by default.")
let sourceFilePath = StringOption(longFlag: "source-file", required: true, helpMessage: "Path of the PDF file which containing required data.")
let targetFolderPath = StringOption(longFlag: "target-folder", helpMessage: "Path of the folder to store extracted data.")

let cli = CommandLine()

cli.addOptions(operation, extractionType, format, sourceFilePath, targetFolderPath)

runningInCLI = true
do {
    try cli.parse()
} catch {
    cli.printUsage(error)
    exit(EX_USAGE)
}
runningInCLI = false

let unwrappedOperation = operation.value ?? Operation.List

let unwrappedExtractionType = extractionType.value ?? ExtractionType.Annotation

let unwrappedFormat = format.value ?? Format.Text
let outputFormat: OutputFormat = {
    switch unwrappedFormat {
    case .JSON:
        return .JSON
    case .Text:
        return .Text
    }
}()

guard let unwrappedSourceFilePath = sourceFilePath.value else {
    exitWithError("--source-file is required")
}
guard let sourceFileURL = NSURL(fileURLWithPath: unwrappedSourceFilePath, isDirectory: false).URLByStandardizingPath else {
    exitWithError("\(unwrappedSourceFilePath): Incorrect source file path")
}

switch unwrappedOperation {

case Operation.List:
    printAnnotationsFrom(sourceFileURL, withFormat: outputFormat)
    
case Operation.Export:
    guard let unwrappedTargetFolderPath = targetFolderPath.value else {
        exitWithError("--target-folder is required")
    }
    guard let targetFolderURL = NSURL(fileURLWithPath: unwrappedTargetFolderPath, isDirectory: true).URLByStandardizingPath else {
        exitWithError("\(unwrappedTargetFolderPath): Incorrect target folder path")
    }
    writePDFAnnotationsFrom(sourceFileURL, toTargetFolder: targetFolderURL)

}
<commit_msg>Fix errors in CLI lead to alert windows.<commit_after>//
//  main.swift
//  Excerptor
//
//  Created by <NAME> on 20/05/2015.
//  Copyright (c) 2015 guoc. All rights reserved.
//

import Foundation

import CommandLine

if Process.argc == 1 || Process.argc == 3 && Process.arguments[1] == "-NSDocumentRevisionsDebugMode" && Process.arguments[2] == "YES" {
    NSApplicationMain(Process.argc, Process.unsafeArgv)
    exit(0)
}

enum Operation: String {
    case List = "list"
    case Export = "export"
}

enum ExtractionType: String {
    case Annotation = "annotation"
}

enum Format: String {
    case Text = "text"
    case JSON = "JSON"
}

let operation = EnumOption<Operation>(longFlag: "operation", helpMessage: "'list' / 'export', 'list' by default.")
let extractionType = EnumOption<ExtractionType>(longFlag: "type", helpMessage: "Type of data to be extracted, currently only supports 'annotation', by default 'annotation'.")
let format = EnumOption<Format>(longFlag: "format", helpMessage: "Display format, currently only supports 'text' and 'JSON', 'text' by default.")
let sourceFilePath = StringOption(longFlag: "source-file", required: true, helpMessage: "Path of the PDF file which containing required data.")
let targetFolderPath = StringOption(longFlag: "target-folder", helpMessage: "Path of the folder to store extracted data.")

let cli = CommandLine()

cli.addOptions(operation, extractionType, format, sourceFilePath, targetFolderPath)

runningInCLI = true
do {
    try cli.parse()
} catch {
    cli.printUsage(error)
    exit(EX_USAGE)
}

let unwrappedOperation = operation.value ?? Operation.List

let unwrappedExtractionType = extractionType.value ?? ExtractionType.Annotation

let unwrappedFormat = format.value ?? Format.Text
let outputFormat: OutputFormat = {
    switch unwrappedFormat {
    case .JSON:
        return .JSON
    case .Text:
        return .Text
    }
}()

guard let unwrappedSourceFilePath = sourceFilePath.value else {
    exitWithError("--source-file is required")
}
guard let sourceFileURL = NSURL(fileURLWithPath: unwrappedSourceFilePath, isDirectory: false).URLByStandardizingPath else {
    exitWithError("\(unwrappedSourceFilePath): Incorrect source file path")
}

switch unwrappedOperation {

case Operation.List:
    printAnnotationsFrom(sourceFileURL, withFormat: outputFormat)
    
case Operation.Export:
    guard let unwrappedTargetFolderPath = targetFolderPath.value else {
        exitWithError("--target-folder is required")
    }
    guard let targetFolderURL = NSURL(fileURLWithPath: unwrappedTargetFolderPath, isDirectory: true).URLByStandardizingPath else {
        exitWithError("\(unwrappedTargetFolderPath): Incorrect target folder path")
    }
    writePDFAnnotationsFrom(sourceFileURL, toTargetFolder: targetFolderURL)

}

runningInCLI = false

{'fraction_non_alphanumeric': 0.10634892406188867, 'fraction_numerical': 0.006046594344655877, 'mean_word_length': 4.5904572564612325, 'pattern_counts': {'":': 0, '<': 11, '<?xml version=': 0, '>': 11, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2055404', 'n_tokens_mistral': 1651, 'n_tokens_neox': 1591, 'n_words': 458}