Command-Line Subcommands in Go
Go: Command-Line Subcommands
Go provides a way to implement command-line subcommands using the flag package and the os package. Here's an example of how to implement subcommands in Go:
Command-Line Subcommands example in Go
package main
import (
"flag"
"fmt"
"os"
)
func main() {
// Define a subcommand "greet"
greetCmd := flag.NewFlagSet("greet", flag.ExitOnError)
greetName := greetCmd.String("name", "", "the name to greet")
// Parse the command-line arguments to determine which subcommand to run
if len(os.Args) < 2 {
fmt.Println("Please specify a subcommand")
os.Exit(1)
}
switch os.Args[1] {
case "greet":
greetCmd.Parse(os.Args[2:])
default:
fmt.Printf("Unknown subcommand: %s\n", os.Args[1])
os.Exit(1)
}
// Run the selected subcommand
if greetCmd.Parsed() {
if *greetName == "" {
fmt.Println("Please provide a name to greet using the -name flag")
os.Exit(1)
}
fmt.Printf("Hello, %s!\n", *greetName)
}
}
This code defines a subcommand greet using the flag.NewFlagSet
function, which creates a new flag set with a specified name and error handling behavior. The subcommand has a single flag -name to specify the name to greet.
The code then parses the command-line arguments to determine which subcommand to run. If no subcommand is specified, the program prints an error message and exits. If the greet subcommand is specified, the code parses the remaining arguments using the greetCmd.Parse
function.
Finally, the code runs the selected subcommand, in this case, the greet subcommand. If the greetName flag is not provided, the program prints an error message and exits. Otherwise, it uses the value of the flag as the name and prints a personalized greeting.
You can add more subcommands by defining additional flag sets and adding more cases to the switch statement.
I hope that helps you get started with working with command-line subcommands in Go! Let me know if you have any more questions
Previous Article
Next Article
Golang Tutorials
- Hello World
- Operators in Go
- Declarations in Go
- Values in Go
- Variables in Go
- For in Go
- If/Else in Go
- Switch in Go
- Arrays in Go
- Slices in Go
- Maps in Go
- Range in Go
- Functions in Go
- Closures in Go
- Recursion in Go
- Pointers in Go
- Strings and Runes in Go
- Structs in Go
- Methods in Go
- Interfaces in Go
- Generics in Go
- Errors in Go
- Goroutines in Go
- Channels in Go
- Select in Go
- Timeouts in Go
- Timers in Go
- Worker Pools in Go
- WaitGroups in Go
- Mutexes in Go
- Sorting in Go
- Panic in Go
- Defer in Go
- Recover in Go
- JSON in Go
- XML in Go
- Time in Go
- Epoch in Go
- Time Formatting in Go
- Random Numbers in Go
- Number Parsing in Go
- URL Parsing in Go
- SHA256 Hashes in Go
- Base64 Encoding in Go
- Reading Files in Go
- Writing Files in Go
- File Paths in Go
- Directories in Go
- Testing and Benchmarking in Go
- Command-Line Arguments in Go
- Command-Line Flags in Go
- Command-Line Subcommands in Go
- Environment Variables in Go
- HTTP Client in Go
- HTTP Server in Go
- Context in Go
- Signals in Go