<commit_before>//
//  Moderator.swift
//
//  Created by <NAME>.
//  Copyright (c) 2016 NotTooBad Software. All rights reserved.
//

// Should ideally and eventually be compatible with http://pubs.opengroup.org/onlinepubs/9699919799/basedefs/V1_chap12.html ,
// with the addition of "--longname". For more, see http://blog.nottoobadsoftware.com/uncategorized/cross-platform-command-line-arguments-syntax/ .

public typealias UsageText = (title: String, description: String)?

public struct Argument <Value> {
	public let usage: UsageText
	public let parse: ([String]) throws -> (value: Value, remainder: [String])

	public init (usage: UsageText = nil, p: @escaping ([String]) throws -> (value: Value, remainder: [String])) {
		self.parse = p
		self.usage = usage
	}

	public init (usage: UsageText = nil, value: Value) {
		self.parse = { args in (value, args) }
		self.usage = usage
	}
}

extension Argument {
	public func map <Outvalue> (_ f: @escaping (Value) throws -> Outvalue) -> Argument<Outvalue> {
		return Argument<Outvalue>(usage: self.usage) { args in
			let result = try self.parse(args)
			return (value: try f(result.value), remainder: result.remainder)
		}
	}
}

public struct ArgumentError: Error, CustomStringConvertible {
	public let errormessage: String
	public internal(set) var usagetext: String? = nil

	public init (errormessage: String, usagetext: String? = nil) {
		self.errormessage = errormessage
		self.usagetext = usagetext
	}

	public var description: String { return errormessage + (usagetext.map { "\n" + $0 } ?? "") }
}

extension Argument {
	static func isOption (index: Array<String>.Index, args: [String]) -> Bool {
		if let i = args.index(of: "--"), i < index { return false }
		let argument = args[index].characters
		if argument.first == "-", let second = argument.dropFirst().first {
			if !("0"..."9" ~= second) { return true }
		}
		return false
	}

	static func option(names: [String], description: String? = nil) -> Argument<Bool> {
		for illegalcharacter in [" ","-","="] {
			precondition(!names.contains(where: {$0.contains(illegalcharacter)}), "Option names cannot contain '\(illegalcharacter)'")<commit_msg>Do not allow option names to begin with a number.<commit_after>//
//  Moderator.swift
//
//  Created by <NAME>.
//  Copyright (c) 2016 NotTooBad Software. All rights reserved.
//

// Should ideally and eventually be compatible with http://pubs.opengroup.org/onlinepubs/9699919799/basedefs/V1_chap12.html ,
// with the addition of "--longname". For more, see http://blog.nottoobadsoftware.com/uncategorized/cross-platform-command-line-arguments-syntax/ .

public typealias UsageText = (title: String, description: String)?

public struct Argument <Value> {
	public let usage: UsageText
	public let parse: ([String]) throws -> (value: Value, remainder: [String])

	public init (usage: UsageText = nil, p: @escaping ([String]) throws -> (value: Value, remainder: [String])) {
		self.parse = p
		self.usage = usage
	}

	public init (usage: UsageText = nil, value: Value) {
		self.parse = { args in (value, args) }
		self.usage = usage
	}
}

extension Argument {
	public func map <Outvalue> (_ f: @escaping (Value) throws -> Outvalue) -> Argument<Outvalue> {
		return Argument<Outvalue>(usage: self.usage) { args in
			let result = try self.parse(args)
			return (value: try f(result.value), remainder: result.remainder)
		}
	}
}

public struct ArgumentError: Error, CustomStringConvertible {
	public let errormessage: String
	public internal(set) var usagetext: String? = nil

	public init (errormessage: String, usagetext: String? = nil) {
		self.errormessage = errormessage
		self.usagetext = usagetext
	}

	public var description: String { return errormessage + (usagetext.map { "\n" + $0 } ?? "") }
}

extension Argument {
	static func isOption (index: Array<String>.Index, args: [String]) -> Bool {
		if let i = args.index(of: "--"), i < index { return false }
		let argument = args[index].characters
		if argument.first == "-", let second = argument.dropFirst().first {
			if !("0"..."9" ~= second) { return true }
		}
		return false
	}

	static func option(names: [String], description: String? = nil) -> Argument<Bool> {
		for illegalcharacter in [" ","-","="] {
			precondition(!names.contains(where: {$0.contains(illegalcharacter)}), "Option names cannot contain '\(illegalcharacter)'")
		}
		for digit in 0...9 {
			precondition(!names.contains(where: {$0.hasPrefix(String(digit))}), "Option names cannot begin with a number.")
{'fraction_non_alphanumeric': 0.14587973273942093, 'fraction_numerical': 0.01002227171492205, 'mean_word_length': 4.086070215175538, 'pattern_counts': {'":': 0, '<': 19, '<?xml version=': 0, '>': 29, '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': '6161035', 'n_tokens_mistral': 1503, 'n_tokens_neox': 1394, 'n_words': 420}