Code with Go
About
  • Code With Go! 🚀
  • Hello World
  • Values
  • Variables
  • Constants
  • For Loop
  • If Else
  • Switch
  • Array
  • Slice
  • Map
  • Range
  • Functions
  • Multiple Return Values
  • Variadic Functions
  • Closure
  • Recursion
  • Pointers
  • Strings and Runes
  • Structs
  • Methods
  • Interfaces
  • Struct Embedding
  • Generics
  • Error
  • Goroutines
  • Channels
  • Channel Buffering
  • Channel Synchronization
  • Channel Directions
  • Select
  • Timeouts
  • Non-Blocking Channel Operations
  • Closing Channels
  • Range over Channels
  • Timers
  • Tickers
  • Worker Pools
  • Wait Groups
  • Rate Limiting
  • Atomic Counters
  • Mutexes
  • Stateful Goroutines
  • Sorting
  • Sorting by Functions
  • Panic
  • Defer
  • Recover
  • String Functions
  • String Formatting
  • Text Templates
  • Regular Expressions
  • JSON
  • XML
  • Time
  • Epoch
  • Time Formatting / Parsing
  • Random Numbers
  • Number Parsing
  • SHA256 Hashes
  • Base64 Encoding
  • Reading Files
  • Writing Files
  • Line Filters
  • File Paths
  • Directories
  • Temporary Files and Directories
  • Embed Directive
  • Testing and Benchmarking
  • Command-Line Arguments
  • Command-Line Flags
  • Command-Line Subcommands
  • Environment Variables
  • Logging
  • HTTP Client
  • HTTP Server
  • Context
  • Spawning Processes
  • Exec'ing Processes
  • Signals
  • Exit
  • Resize Image
Powered by GitBook
On this page

Was this helpful?

Edit on GitHub

Command-Line Subcommands

This Go program demonstrates the use of subcommands with different sets of flags using the flag package. It allows you to run the program with either a "foo" or "bar" subcommand, each having its own set of flags. Let's break down the code with inline comments:

package main

import (
	"flag"
	"fmt"
	"os"
)

func main() {
	// Define flags for the 'foo' subcommand
	fooCmd := flag.NewFlagSet("foo", flag.ExitOnError)
	fooEnable := fooCmd.Bool("enable", false, "enable")
	fooName := fooCmd.String("name", "", "name")

	// Define flags for the 'bar' subcommand
	barCmd := flag.NewFlagSet("bar", flag.ExitOnError)
	barLevel := barCmd.Int("level", 0, "level")

	// Check if there are enough arguments
	if len(os.Args) < 2 {
		fmt.Println("expected 'foo' or 'bar' subcommands")
		os.Exit(1)
	}

	// Determine the subcommand and parse its specific flags
	switch os.Args[1] {
	case "foo":
		fooCmd.Parse(os.Args[2:])
		fmt.Println("subcommand 'foo'")
		fmt.Println("  enable:", *fooEnable)
		fmt.Println("  name:", *fooName)
		fmt.Println("  tail:", fooCmd.Args())
	case "bar":
		barCmd.Parse(os.Args[2:])
		fmt.Println("subcommand 'bar'")
		fmt.Println("  level:", *barLevel)
		fmt.Println("  tail:", barCmd.Args())
	default:
		fmt.Println("expected 'foo' or 'bar' subcommands")
		os.Exit(1)
	}
}

Explanation:

  1. Defining Flags for Subcommands:

    • The program defines two sets of flags using flag.NewFlagSet for the 'foo' and 'bar' subcommands.

  2. Parsing Subcommands:

    • The program checks the number of command-line arguments. If there are not enough arguments, it prints an error message and exits.

  3. Switching Between Subcommands:

    • The program uses a switch statement to determine whether the first argument is "foo" or "bar."

  4. Parsing Subcommand-Specific Flags:

    • Depending on the subcommand, it parses the specific set of flags for that subcommand using fooCmd.Parse or barCmd.Parse.

  5. Printing Results:

    • After parsing the subcommand-specific flags, the program prints the results.

You can run the program with commands like:

go run main.go foo -enable -name John arg1 arg2

or

go run main.go bar -level 5 arg1 arg2

This allows you to have different flags for different subcommands.

PreviousCommand-Line FlagsNextEnvironment Variables

Last updated 1 year ago

Was this helpful?