summaryrefslogtreecommitdiffstats
path: root/CmdLine/main.swift
blob: 3876d05f7b4945b0137e7f542e85327b2ab9e7c9 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
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")