gobyexample/subcmd/subcmd.go

37 lines
1.0 KiB
Go
Raw Permalink Normal View History

2024-05-04 15:27:54 +00:00
package subcmd
import (
"flag"
"fmt"
"os"
)
func SubCmd() {
agentCmd := flag.NewFlagSet("agent", flag.ExitOnError)
agentLevel := agentCmd.Uint64("level", 1, "Level of the agent from 1-5")
agentName := agentCmd.String("name", "", "Name of the agent")
missionCmd := flag.NewFlagSet("mission", flag.ExitOnError)
missionName := missionCmd.String("codename", "classified", "Codename of the mission")
missionImpossible := missionCmd.Bool("impossible", false, "Is the mission impossible?")
if len(os.Args) < 2 {
fmt.Fprintln(os.Stderr, "A subcommand must be provided ('mission', 'agent')")
os.Exit(2)
}
switch os.Args[1] {
case "agent":
agentCmd.Parse(os.Args[2:])
fmt.Println("Agent level:", *agentLevel)
fmt.Println("Agent name:", *agentName)
case "mission":
missionCmd.Parse(os.Args[2:])
fmt.Println("Mission codename:", *missionName)
fmt.Println("Mission impossible?", *missionImpossible)
default:
fmt.Fprintln(os.Stderr, "Invalid subcommand. Only valid subcommands are 'mission' and 'agent'")
os.Exit(2)
}
}