<commit_before>//
//  HelpCommand.swift
//  Commandant
//
//  Created by <NAME> on 2014-10-10.
//  Copyright (c) 2014 Carthage. All rights reserved.
//

import Foundation
import Result

/// A basic implementation of a `help` command, using information available in a
/// `CommandRegistry`.
///
/// If you want to use this command, initialize it with the registry, then add
/// it to that same registry:
///
/// 	let commands: CommandRegistry<MyErrorType> = …
/// 	let helpCommand = HelpCommand(registry: commands)
/// 	commands.register(helpCommand)
public struct HelpCommand<ClientError: Error>: CommandProtocol {
	public typealias Options = HelpOptions<ClientError>

	public let verb = "help"
	public let function = "Display general or command-specific help"

	private let registry: CommandRegistry<ClientError>

	/// Initializes the command to provide help from the given registry of
	/// commands.
	public init(registry: CommandRegistry<ClientError>) {
		self.registry = registry
	}

	public func run(_ options: Options) -> Result<(), ClientError> {
		if let verb = options.verb {
			if let command = self.registry[verb] {
				print(command.function)
				if let usageError = command.usage() {
					print("\n\(usageError)")
				}
				return .success(())
			} else {
				fputs("Unrecognized command: '\(verb)'\n", stderr)<commit_msg>[ADD] Possibility to set help command’s function<commit_after>//
//  HelpCommand.swift
//  Commandant
//
//  Created by <NAME> on 2014-10-10.
//  Copyright (c) 2014 Carthage. All rights reserved.
//

import Foundation
import Result

/// A basic implementation of a `help` command, using information available in a
/// `CommandRegistry`.
///
/// If you want to use this command, initialize it with the registry, then add
/// it to that same registry:
///
/// 	let commands: CommandRegistry<MyErrorType> = …
/// 	let helpCommand = HelpCommand(registry: commands)
/// 	commands.register(helpCommand)
public struct HelpCommand<ClientError: Error>: CommandProtocol {
	public typealias Options = HelpOptions<ClientError>

	public let verb = "help"
	public let function: String
	
	private let registry: CommandRegistry<ClientError>

	/// Initializes the command to provide help from the given registry of
	/// commands.
	public init(registry: CommandRegistry<ClientError>, function: String? = nil) {
		self.registry = registry
		self.function = function ?? "Display general or command-specific help"
	}

	public func run(_ options: Options) -> Result<(), ClientError> {
		if let verb = options.verb {
			if let command = self.registry[verb] {
				print(command.function)
				if let usageError = command.usage() {
					print("\n\(usageError)")
				}
				return .success(())
			} else {
				fputs("Unrecognized command: '\(verb)'\n", stderr)
{'fraction_non_alphanumeric': 0.1225153595952295, 'fraction_numerical': 0.008673653776653415, 'mean_word_length': 3.605657237936772, 'pattern_counts': {'":': 0, '<': 17, '<?xml version=': 0, '>': 19, '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': '3612038', 'n_tokens_mistral': 858, 'n_tokens_neox': 769, 'n_words': 271}