iOS 17.0+
MacOS 14.0+
macCatalyst 17.0+
tvOS 17.0+
visionOS 1.0+
将以下 URL 添加到项目的 Package Dependencies(软件包依赖项)
https://github.com/MiaKoring/SwiftSlashCommands
导入 SlashCommands
import SlashCommands
创建一个命令
class Example: Command {
init(completion: @escaping ([String : Any]) -> Void) {
self.completion = completion
}
var id: UUID = UUID()
var userAccessible = true //false if this should be a system intern command which only the code can execute
var command: String = "example"
var description = "description of what the command does"
var parameters: [CommandParameter] = [ //enter as many parameters as you like or leave empty
CommandParameter(id: 0, name: "param1", datatype: .bool, required: true), //throws an error if not included in the executed command
CommandParameter(id: 1, name: "param2", datatype: .int, required: false) //doesn't need to be in the executed command
]
//set which level of permission is needed to execute this command,
//set to .none and usen.none in the execution if you don't have permission layers or as default state
var minPermissions: Permission = .none
//Useful for selection if there are multiple commands with the same call-string
var commandOwner: String = "Example"
//expects a function in the initializer that gets called when the command gets executed
var completion: ([String : Any]) -> Void
//gets a dictionary of parameter on call.
//Eg. "/example param1: true param2: 1" results in ["param1": true, "param2": 1], values get checked if they are the correct type
//Function can be declared anywhere, outside of the class as well
public static func complete(_ : [String:Any])-> Void {
print("-------------------------------------------")
print("example executed")
print("-------------------------------------------")
}
}
创建一个 CommandCollection 并添加命令
let collection = CommandCollection()
collection.commands.append(Example(completion: Example.complete))
获取给定输入字符串和权限级别的命令
//get all commands that you are allowed to use with .none permissions
let commands = collection.commands(for: "/", highestPermission: .none)
//commands contains [Example] now
//get all commands that you are allowed to use with .muted permissions
let commands = collection.commands(for: "/", highestPermission: .muted)
//commands is empty, since .muted is below .none
//get all commands that start with "e" that you are allowed to use with .none permissions
let commands = collection.commands(for: "/e", highestPermission: .none)
//commands contains [Example] now
//get all commands for "/example param1": (only searches for example) that you are allowed to use with .none permissions
let commands = collection.commands(for: "/example param1:", highestPermission: .none)
//commands contains [Example] now
执行一个命令
try collection.execute(collection.commands.first!, with: "/example param1: false", highestPermission: .none)
//prints in the console:
//-------------------------------------------
//example executed
//-------------------------------------------
错误处理
//the following errors can be thrown
public enum CommandError: Error, Equatable{
case missingParameter
case regexFailed
case paramInvalidType(String)
case insufficientPermissions
case invalidCommandnameForSelectedCommand
case missingSlash
}
请为可能发生的任何错误提交 issue。 如果您想请求功能,也请提交 issue,并明确描述该功能