diff options
Diffstat (limited to 'CmdLine')
-rw-r--r-- | CmdLine/ClearScreen.swift | 8 | ||||
-rw-r--r-- | CmdLine/CmdTool.swift | 62 | ||||
-rw-r--r-- | CmdLine/main.swift | 76 |
3 files changed, 146 insertions, 0 deletions
diff --git a/CmdLine/ClearScreen.swift b/CmdLine/ClearScreen.swift new file mode 100644 index 0000000..bcb15e2 --- /dev/null +++ b/CmdLine/ClearScreen.swift @@ -0,0 +1,8 @@ +// +// ClearScreen.swift +// CmdLine +// +// Created by Jacky Jack on 31/05/2023. +// + +import Foundation diff --git a/CmdLine/CmdTool.swift b/CmdLine/CmdTool.swift new file mode 100644 index 0000000..41d35d8 --- /dev/null +++ b/CmdLine/CmdTool.swift @@ -0,0 +1,62 @@ +// +// CmdTool.swift +// CmdLine +// +// Created by Jacky Jack on 13/06/2023. +// + +import Foundation +import cmd + + +public class CmdTool { + init() { + + } +} + + + +public class CmdTableEntry { + var command:String + var callback:((Array<cmd.CmdToken>?) -> Void)? + //var helpDelegate:CmdTableHelp? + //var preconditionDelegate:CmdTablePrecondition? + //var autocompleteDelegate:CmdTableAutocomplete? + + init() { + command = "" + } +} + +public class CmdTable { + var table:Array<CmdTableEntry> = [] + + init() { + + } + + func addEntry(_ cmd: CmdTableEntry) { + table.append(cmd) + } + + func listCommands() { + for command in table { + print("CMD: \(command.command) callback:\(command.callback)") + } + } + + func execute(_ args: Array<CmdToken>) { + let cmd0 = args[0] + var params0 = Array<CmdToken>() + if args.count > 1 { + params0 = Array<CmdToken>(args[1..<args.count]) + } + for cmd in self.table { + if cmd.command == cmd0.val { + print("Found command executing ...") + cmd.callback!(params0) + } + } + } +} diff --git a/CmdLine/main.swift b/CmdLine/main.swift new file mode 100644 index 0000000..3876d05 --- /dev/null +++ b/CmdLine/main.swift @@ -0,0 +1,76 @@ +// +// main.swift +// CmdLine +// +// Created by Jacky Jack on 24/02/2023. +// + +import Foundation +import Darwin +//import cmd +import cmd + +print("Test CMD") +var tok:cmd_tok_t = cmd_tok_t() +var c="asd 123" +//cmd.(&tok, c, c.count) +//cmdparse_cmd(<#T##tl: UnsafeMutablePointer<cmd_tok_t>!##UnsafeMutablePointer<cmd_tok_t>!#>, <#T##str: UnsafePointer<CChar>!##UnsafePointer<CChar>!#>, <#T##str_size: Int##Int#>) +//var p = cmd.Par() +var p = cmd.CmdParser() +//p.parse() +let r = p.parse("123 0x0 0b1 \"this is me\" asd123 a@1 1.20 admin@main.lv") +let r1 = p.parse("ls") +let r2 = p.parse("one") +let r3 = p.parse("ls 123") +let r4 = p.parse("args 0x0 0b1 \"this is me\" asd123 a@1 1.20 admin@main.lv") +for e in 0..<r.count { + print("tok \(r[e].type) - \(r[e].val)") +} + +let cmd_ls = CmdTableEntry() +cmd_ls.command = "ls" +func call_ls(_ args:Array<CmdToken>?) { + print("List nothing") +} +cmd_ls.callback = call_ls + +let cmd_one = CmdTableEntry() +var cmd_one_i = 0 +cmd_one.command = "one" +func call_cmd(_ args:Array<CmdToken>?) { + print("+1 == \(cmd_one_i)") + cmd_one_i += 1 +} +cmd_one.callback = call_cmd + +let cmd_show_args = CmdTableEntry() +func call_show_args(_ _args:Array<CmdToken>?) { + if let args = _args { + for arg in args { + print("\(arg.val) ") + } + } +} +cmd_show_args.command = "args" +cmd_show_args.callback = call_show_args + +let commands = CmdTable() +commands.addEntry(cmd_ls) +commands.addEntry(cmd_one) +commands.addEntry(cmd_show_args) + + +commands.listCommands() + + +commands.execute(r) +commands.execute(r1) +commands.execute(r2) +commands.execute(r2) +commands.execute(r2) +commands.execute(r2) +commands.execute(r3) +commands.execute(r4) +print("Test CMD") + + |