Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

💥 Command Aliases #155

Draft
wants to merge 1 commit into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions Sources/Commandant/Command.swift
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,9 @@ public protocol CommandProtocol {
/// `help`).
var verb: String { get }

/// Optional list of additional verbs which can be used to invoke this command.
var aliases: [String] { get }
Copy link

@muescha muescha Jun 18, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
var aliases: [String] { get }
var aliases: [String]? { get }

but let aliases be optional and not defined (null)

PS:
need to add

extension CommandProtocol {
    var aliases: [String]? { return nil }
}

or do just a default implementation:

extension CommandProtocol {
    var aliases: [String] { return [] }
}
``


/// A human-readable, high-level description of what this command is used
/// for.
var function: String { get }
Expand Down Expand Up @@ -96,6 +99,10 @@ public final class CommandRegistry<ClientError: Error> {
{
for command in commands {
commandsByVerb[command.verb] = CommandWrapper(command)
// Register command for each additional alias
for alias in command.aliases {
commandsByVerb[alias] = CommandWrapper(command)
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
commandsByVerb[alias] = CommandWrapper(command)
let wrappedCommand = CommandWrapper(command)
wrappedCommand.verb = alias
wrappedCommand.function = "alias for '${command.verb}': {$command.function}"
commandsByVerb[alias] = wrappedCommand

}
}
return self
}
Expand Down
2 changes: 2 additions & 0 deletions Sources/Commandant/HelpCommand.swift
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,11 @@ import Foundation
/// 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 aliases: [String] = []
public let function: String

private let registry: CommandRegistry<ClientError>
Expand Down