SREP-711: Add non-interactive command input to ssm command by MateSaary · Pull Request #716 · openshift/backplane-cli
We might not need to do this by our own.
Guideline 10:
The first -- argument that is not an option-argument should be accepted as a delimiter indicating the end of options. Any following arguments should be treated as operands, even if they begin with the '-' character.
The backplane elevate command is doing something similar, can use it as reference:
// main.go
package main
import (
"fmt"
"os"
"os/exec"
"strings"
"github.com/spf13/cobra"
)
func main() {
rootCmd := &cobra.Command{Use: "mycli"}
rootCmd.AddCommand(echoCmd())
if err := rootCmd.Execute(); err != nil {
os.Exit(1)
}
}
func echoCmd() *cobra.Command {
var shout bool
cmd := &cobra.Command{
Use: "echo",
Short: "Pass arguments to the echo command",
Run: func(cmd *cobra.Command, args []string) {
fmt.Println("Flag --shout:", shout)
fmt.Println("Args to echo:", args)
if shout {
for i, v := range args {
args[i] = strings.ToUpper(v)
}
}
// Run echo with args
out, err := exec.Command("echo", args...).CombinedOutput()
if err != nil {
fmt.Println("Error:", err)
return
}
fmt.Println("Output:", string(out))
},
}
cmd.Flags().BoolVar(&shout, "shout", false, "Uppercase all args")
return cmd
}
go mod init mycli
go mod tidy
go run main.go echo --shout -- hello world
Flag --shout: true
Args to echo: [hello world]
Output: HELLO WORLD
Hope this helps. Let me know if I miss anything. Thanks!