<commit_before>//
//  OptionsParser.swift
//  configen
//
//  Created by <NAME> on 10/08/2016.
//  Copyright © 2016 The App Business. All rights reserved.
//

import Foundation

final class OptionsParser {

  let appName: String
  let inputPlistFilePath: String
  let inputHintsFilePath: String
  let outputClassName: String
  let outputClassDirectory: String
  let isObjC: Bool

  init(appName: String) {
    let cli = CommandLineKit()
    let inputPlistFilePath = StringOption(shortFlag: "p", longFlag: "plist-path", required: true, helpMessage: "Path to the input plist file")
    let inputHintsFilePath = StringOption(shortFlag: "h", longFlag: "hints-path", required: true, helpMessage: "Path to the input hints file")
    let outputClassName = StringOption(shortFlag: "n", longFlag: "class-name", required: true, helpMessage: "The output config class name")
    let outputClassDirectory = StringOption(shortFlag: "o", longFlag: "output-directory", required: true, helpMessage: "The output config class directory")
    let useObjc = BoolOption(shortFlag: "c", longFlag: "objective-c", helpMessage: "Whether to generate Objective-C files instead of Swift")
    cli.addOptions(inputPlistFilePath, inputHintsFilePath, outputClassName, outputClassDirectory, useObjc)

    do {
      try cli.parse()
    } catch {
      cli.printUsage(error)
      fatalError()
    }

    self.appName = appName
    self.inputPlistFilePath = inputPlistFilePath.value!
    self.inputHintsFilePath = inputHintsFilePath.value!
    self.outputClassName = outputClassName.value!
    self.outputClassDirectory = outputClassDirectory.value!
    self.isObjC = useObjc.value
  }

  lazy var plistDictionary: [String: AnyObject] = { [unowned self] in
    let inputPlistFilePathURL = URL(fileURLWithPath: self.inputPlistFilePath)
    guard let data = try? Data(contentsOf: inputPlistFilePathURL) else {
      fatalError("No data at path: \(self.inputPlistFilePath)")
    }

    guard let plistDictionary = (try? PropertyListSerialization.propertyList(from: data, options: [], format: nil)) as? [String: AnyObject] else {
      fatalError("Failed to create plist")
    }

    return plistDictionary
    }()
  
  lazy var sortedHintsTuple: [(key: String, value: String)] = { [unowned self] in
    guard let hintsString = try? String(contentsOfFile: self.inputHintsFilePath, encoding: String.Encoding.utf8) else {
      fatalError("No data at path: \(self.inputHintsFilePath)")
    }

    var hintsDictionary = [String: String]()
    let hintLines = hintsString.components(separatedBy: CharacterSet.newlines)
    for hintLine in hintLines where hintLine.trimmed.count > 0 {
      let hints = hintLine.components(separatedBy: CharacterSet(charactersIn: ":")).map { $0.trimmed }
      guard hints.count == 2 else {
        fatalError("Expected \"variableName : Type\", instead of \"\(hintLine)\"")
      }
      let (variableName, type) = (hints[0], hints[1])
      hintsDictionary[variableName] = type
    }
    
    return hintsDictionary.sorted(by: { $0.0 < $1.0 })
    }()
}<commit_msg>Points free style<commit_after>//
//  OptionsParser.swift
//  configen
//
//  Created by <NAME> on 10/08/2016.
//  Copyright © 2016 The App Business. All rights reserved.
//

import Foundation

final class OptionsParser {

  let appName: String
  let inputPlistFilePath: String
  let inputHintsFilePath: String
  let outputClassName: String
  let outputClassDirectory: String
  let isObjC: Bool

  init(appName: String) {
    let cli = CommandLineKit()
    let inputPlistFilePath = StringOption(shortFlag: "p", longFlag: "plist-path", required: true, helpMessage: "Path to the input plist file")
    let inputHintsFilePath = StringOption(shortFlag: "h", longFlag: "hints-path", required: true, helpMessage: "Path to the input hints file")
    let outputClassName = StringOption(shortFlag: "n", longFlag: "class-name", required: true, helpMessage: "The output config class name")
    let outputClassDirectory = StringOption(shortFlag: "o", longFlag: "output-directory", required: true, helpMessage: "The output config class directory")
    let useObjc = BoolOption(shortFlag: "c", longFlag: "objective-c", helpMessage: "Whether to generate Objective-C files instead of Swift")
    cli.addOptions(inputPlistFilePath, inputHintsFilePath, outputClassName, outputClassDirectory, useObjc)

    do {
      try cli.parse()
    } catch {
      cli.printUsage(error)
      fatalError()
    }

    self.appName = appName
    self.inputPlistFilePath = inputPlistFilePath.value!
    self.inputHintsFilePath = inputHintsFilePath.value!
    self.outputClassName = outputClassName.value!
    self.outputClassDirectory = outputClassDirectory.value!
    self.isObjC = useObjc.value
  }

  lazy var plistDictionary: [String: AnyObject] = { [unowned self] in
    let inputPlistFilePathURL = URL(fileURLWithPath: self.inputPlistFilePath)
    guard let data = try? Data(contentsOf: inputPlistFilePathURL) else {
      fatalError("No data at path: \(self.inputPlistFilePath)")
    }

    guard let plistDictionary = (try? PropertyListSerialization.propertyList(from: data, options: [], format: nil)) as? [String: AnyObject] else {
      fatalError("Failed to create plist")
    }

    return plistDictionary
    }()
  
  lazy var sortedHintsTuple: [(key: String, value: String)] = { [unowned self] in
    guard let hintsString = try? String(contentsOfFile: self.inputHintsFilePath, encoding: String.Encoding.utf8) else {
      fatalError("No data at path: \(self.inputHintsFilePath)")
    }

    var hintsDictionary = [String: String]()
    let hintLines = hintsString.components(separatedBy: CharacterSet.newlines)
    for hintLine in hintLines where hintLine.trimmed.count > 0 {
      let hints = hintLine.components(separatedBy: CharacterSet(charactersIn: ":")).map { $0.trimmed }
      guard hints.count == 2 else {
        fatalError("Expected \"variableName : Type\", instead of \"\(hintLine)\"")
      }
      let (variableName, type) = (hints[0], hints[1])
      hintsDictionary[variableName] = type
    }
    
    return hintsDictionary.sorted(by: <)
    }()
}
{'fraction_non_alphanumeric': 0.10980006555227795, 'fraction_numerical': 0.006555227794165847, 'mean_word_length': 3.9820408163265304, 'pattern_counts': {'":': 2, '<': 7, '<?xml version=': 0, '>': 7, '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': '6691042', 'n_tokens_mistral': 1854, 'n_tokens_neox': 1776, 'n_words': 500}