From 2c6baf20e4a5f1ca866812ba1de23b6bd5e0359c Mon Sep 17 00:00:00 2001 From: onyx-and-iris <75868496+onyx-and-iris@users.noreply.github.com> Date: Sun, 18 Sep 2022 05:39:42 +0100 Subject: [PATCH] vm-cli reworked. now accepts -kind and -delay flags. arg parsing moved into separate run_commands function. README added to vm-cli example --- examples/vm-cli/README.md | 10 ++++-- examples/vm-cli/main.go | 70 ++++++++++++++++++++++++++------------- 2 files changed, 55 insertions(+), 25 deletions(-) diff --git a/examples/vm-cli/README.md b/examples/vm-cli/README.md index 0082421..c527cb8 100644 --- a/examples/vm-cli/README.md +++ b/examples/vm-cli/README.md @@ -4,16 +4,22 @@ A simple voicemeeter-cli program. Offers ability to toggle, get and set paramete ## Use -Toggle with `!` prefix, get by excluding `=` and set by including `=`. Mix and match arguments, for example: +Toggle with `!` prefix, get by excluding `=` and set by including `=`. Mix and match arguments. +You may pass an optional flag -kind to set the kind of Voicemeeter. Defaults to banana. +You may pass an optional flag -delay to set a delay on the getters. Defaults to 15ms. -`go run .\main.go strip[0].mute=0 strip[0].mute !strip[0].mute strip[0].mute bus[0].gain=-8.8` +for example: + +`go run .\main.go -kind=potato -delay=18 strip[0].mute=0 strip[0].mute !strip[0].mute strip[0].mute bus[0].gain=-8.8 command.lock=1` Expected output: ``` +Logged into Voicemeeter potato Running command strip[0].mute=0 Value of strip[0].mute is: 0 Toggling strip[0].mute Value of strip[0].mute is: 1 Running command bus[0].gain=-8.8 +Running command command.lock=1 ``` diff --git a/examples/vm-cli/main.go b/examples/vm-cli/main.go index d17e165..2cb5ee7 100644 --- a/examples/vm-cli/main.go +++ b/examples/vm-cli/main.go @@ -1,45 +1,33 @@ package main import ( + "flag" "fmt" "log" - "os" "strings" "github.com/onyx-and-iris/voicemeeter-api-go" ) func main() { - vm, err := vmConnect() + kindId := flag.String("kind", "banana", "kind of voicemeeter") + delay := flag.Int("delay", 15, "delay between commands") + flag.Parse() + + vm, err := vmConnect(kindId, delay) if err != nil { log.Fatal(err) } defer vm.Logout() - for _, arg := range os.Args[1:] { - if arg[0] == '!' { - vm.SetFloat(arg[1:], 1-vm.GetFloat(arg[1:])) - fmt.Println("Toggling", arg[1:]) - } else { - if strings.Contains(arg, "=") { - fmt.Println("Running command", arg) - vm.SendText(arg) - } else { - s := strings.Split(arg, ".") - if strings.Contains(s[1], "label") { - val := vm.GetString(arg) - fmt.Println("Value of", arg, "is:", val) - } else { - val := vm.GetFloat(arg) - fmt.Println("Value of", arg, "is:", val) - } - } - } + err = run_commands(vm) + if err != nil { + fmt.Println(err) } } -func vmConnect() (*voicemeeter.Remote, error) { - vm, err := voicemeeter.NewRemote("banana", 15) +func vmConnect(kindId *string, delay *int) (*voicemeeter.Remote, error) { + vm, err := voicemeeter.NewRemote(*kindId, *delay) if err != nil { return nil, err } @@ -51,3 +39,39 @@ func vmConnect() (*voicemeeter.Remote, error) { return vm, nil } + +func run_commands(vm *voicemeeter.Remote) error { + for _, arg := range flag.Args() { + if arg[0] == '!' { + val, err := vm.GetFloat(arg[1:]) + if err != nil { + err = fmt.Errorf("unable to toggle %s", arg[1:]) + return err + } + vm.SetFloat(arg[1:], 1-val) + fmt.Println("Toggling", arg[1:]) + } else { + if strings.Contains(arg, "=") { + fmt.Println("Running command", arg) + err := vm.SendText(arg) + if err != nil { + err = fmt.Errorf("unable to set %s", arg) + return err + } + } else { + val_f, err := vm.GetFloat(arg) + if err != nil { + val_s, err := vm.GetString(arg) + if err != nil { + err = fmt.Errorf("unable to get %s", arg) + return err + } + fmt.Println("Value of", arg, "is:", val_s) + } else { + fmt.Println("Value of", arg, "is:", val_f) + } + } + } + } + return nil +}