mirror of
https://github.com/c9s/bbgo.git
synced 2024-11-25 08:15:15 +00:00
bbgo: implement parseCommand
This commit is contained in:
parent
7053802041
commit
ccaa8c5c86
|
@ -1,11 +1,10 @@
|
||||||
package bbgo
|
package bbgo
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"errors"
|
|
||||||
"go/scanner"
|
|
||||||
"go/token"
|
|
||||||
"reflect"
|
"reflect"
|
||||||
"strconv"
|
"strconv"
|
||||||
|
"strings"
|
||||||
|
"text/scanner"
|
||||||
|
|
||||||
"gopkg.in/tucnak/telebot.v2"
|
"gopkg.in/tucnak/telebot.v2"
|
||||||
)
|
)
|
||||||
|
@ -19,31 +18,19 @@ func (i *Interact) HandleTelegramMessage(msg *telebot.Message) {
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func parseCommand(text string) (args []string, err error) {
|
func parseCommand(src string) (args []string) {
|
||||||
src := []byte(text)
|
|
||||||
|
|
||||||
// Initialize the scanner.
|
|
||||||
var errHandler scanner.ErrorHandler = func(pos token.Position, msg string) {
|
|
||||||
err = errors.New(msg)
|
|
||||||
}
|
|
||||||
|
|
||||||
var s scanner.Scanner
|
var s scanner.Scanner
|
||||||
fset := token.NewFileSet() // positions are relative to fset
|
s.Init(strings.NewReader(src))
|
||||||
file := fset.AddFile("", fset.Base(), len(src)) // register input "file"
|
s.Filename = "command"
|
||||||
s.Init(file, src, errHandler, 0)
|
for tok := s.Scan(); tok != scanner.EOF; tok = s.Scan() {
|
||||||
|
text := s.TokenText()
|
||||||
// Repeated calls to Scan yield the token sequence found in the input.
|
if text[0] == '"' && text[len(text) - 1] == '"' {
|
||||||
for {
|
text, _ = strconv.Unquote(text)
|
||||||
_, tok, lit := s.Scan()
|
}
|
||||||
if tok == token.EOF {
|
args = append(args,text)
|
||||||
break
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// we are not using the token type right now, but we will use them later
|
return args
|
||||||
args = append(args, lit)
|
|
||||||
}
|
|
||||||
|
|
||||||
return args, nil
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func parseFuncArgsAndCall(f interface{}, args []string) error {
|
func parseFuncArgsAndCall(f interface{}, args []string) error {
|
||||||
|
|
|
@ -30,6 +30,15 @@ func Test_parseFuncArgsAndCall_ErrorFunction(t *testing.T) {
|
||||||
}
|
}
|
||||||
|
|
||||||
func Test_parseCommand(t *testing.T) {
|
func Test_parseCommand(t *testing.T) {
|
||||||
|
args := parseCommand(`closePosition "BTC USDT" 3.1415926 market`)
|
||||||
|
t.Logf("args: %+v", args)
|
||||||
|
for i, a := range args {
|
||||||
|
t.Logf("args(%d): %#v", i, a)
|
||||||
|
}
|
||||||
|
|
||||||
|
assert.Equal(t, 4, len(args))
|
||||||
|
assert.Equal(t, "closePosition", args[0])
|
||||||
|
assert.Equal(t, "BTC USDT", args[1])
|
||||||
|
assert.Equal(t, "3.1415926", args[2])
|
||||||
|
assert.Equal(t, "market", args[3])
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue
Block a user