convert receiveosc to urfave/cli

This commit is contained in:
2025-09-27 10:19:40 -05:00
parent ce5e65e8f8
commit 257465d216
+40 -10
View File
@@ -1,33 +1,63 @@
package main package main
import ( import (
"context"
"fmt" "fmt"
"net" "net"
"os"
osc "github.com/jwetzell/osc-go" osc "github.com/jwetzell/osc-go"
"github.com/spf13/cobra" "github.com/urfave/cli/v3"
) )
func main() { func main() {
var Host string var Host string
var Port string var Port int32
var Protocol string var Protocol string
var rootCmd = &cobra.Command{ cmd := &cli.Command{
Use: "receiveosc", Name: "makeosc",
Run: func(cmd *cobra.Command, args []string) { Usage: "make osc bytes",
netAddress := Host + ":" + Port Flags: []cli.Flag{
&cli.StringFlag{
Name: "host",
Usage: "host to send OSC message to",
Value: "127.0.0.1",
Destination: &Host,
},
&cli.Int32Flag{
Name: "port",
Usage: "port to send OSC message to",
Destination: &Port,
Value: 8888,
},
&cli.StringFlag{
Name: "protocol",
Usage: "protocol to use to send (tcp or udp)",
Value: "udp",
Destination: &Protocol,
Validator: func(flag string) error {
if flag != "udp" && flag != "tcp" {
return fmt.Errorf("protocol must be either 'udp' or 'tcp'")
}
return nil
},
},
},
Action: func(ctx context.Context, cmd *cli.Command) error {
netAddress := fmt.Sprintf("%s:%d", Host, Port)
if Protocol == "udp" { if Protocol == "udp" {
listenUDP(netAddress) listenUDP(netAddress)
} else if Protocol == "tcp" { } else if Protocol == "tcp" {
listenTCP(netAddress) listenTCP(netAddress)
} }
return nil
}, },
} }
rootCmd.Flags().StringVar(&Host, "host", "127.0.0.1", "host to send OSC message to")
rootCmd.Flags().StringVar(&Port, "port", "8888", "port to send OSC message to") if err := cmd.Run(context.Background(), os.Args); err != nil {
rootCmd.Flags().StringVar(&Protocol, "protocol", "udp", "protocol to use to send (tcp or udp)") panic(err)
rootCmd.Execute() }
} }
func listenTCP(netAddress string) { func listenTCP(netAddress string) {