Practical Flag Usage
Real-World Flag Implementation Scenarios
Flags are essential for creating flexible, configurable command-line applications across various domains.
Configuration Management Application
package main
import (
"flag"
"fmt"
"os"
)
type Config struct {
Environment string
LogLevel string
Port int
Debug bool
}
func loadConfiguration() *Config {
config := &Config{}
flag.StringVar(&config.Environment, "env", "development", "Application environment")
flag.StringVar(&config.LogLevel, "log", "info", "Logging level")
flag.IntVar(&config.Port, "port", 8080, "Server listening port")
flag.BoolVar(&config.Debug, "debug", false, "Enable debug mode")
flag.Parse()
return config
}
func main() {
config := loadConfiguration()
fmt.Printf("LabEx Configuration:\n")
fmt.Printf("Environment: %s\n", config.Environment)
fmt.Printf("Log Level: %s\n", config.LogLevel)
fmt.Printf("Port: %d\n", config.Port)
fmt.Printf("Debug Mode: %v\n", config.Debug)
}
Flag Usage Patterns
Pattern |
Description |
Example |
Configuration Management |
Control application behavior |
--env=production |
Feature Toggling |
Enable/disable features |
--debug=true |
Resource Specification |
Define runtime resources |
--port=9000 |
Logging Control |
Manage logging verbosity |
--log=debug |
Advanced Flag Validation
package main
import (
"flag"
"fmt"
"os"
)
func validatePort(port int) bool {
return port > 1024 && port < 65535
}
func main() {
port := flag.Int("port", 8080, "Server port")
flag.Parse()
if !validatePort(*port) {
fmt.Println("Invalid port number. Must be between 1024 and 65535.")
os.Exit(1)
}
fmt.Printf("Starting server on port %d\n", *port)
}
Complex Flag Scenarios
graph TD
A[Flag Usage] --> B[Simple Flags]
A --> C[Validation Flags]
A --> D[Conditional Flags]
B --> E[Basic Configuration]
C --> F[Input Verification]
D --> G[Contextual Options]
Secure Configuration Management
package main
import (
"flag"
"fmt"
"log"
)
type DatabaseConfig struct {
Host string
Port int
Username string
Password string
SSL bool
}
func loadDatabaseConfig() *DatabaseConfig {
config := &DatabaseConfig{}
flag.StringVar(&config.Host, "db-host", "localhost", "Database host")
flag.IntVar(&config.Port, "db-port", 5432, "Database port")
flag.StringVar(&config.Username, "db-user", "", "Database username")
flag.StringVar(&config.Password, "db-pass", "", "Database password")
flag.BoolVar(&config.SSL, "db-ssl", false, "Enable SSL connection")
flag.Parse()
if config.Username == "" || config.Password == "" {
log.Fatal("Database credentials are required")
}
return config
}
func main() {
dbConfig := loadDatabaseConfig()
fmt.Printf("LabEx Database Configuration: %+v\n", dbConfig)
}
Best Practices for Flag Usage
- Provide meaningful default values
- Implement input validation
- Use clear, descriptive flag names
- Handle missing or invalid inputs gracefully
- Support help and usage documentation
Flag Help and Documentation
func main() {
flag.Usage = func() {
fmt.Println("LabEx Configuration Tool")
fmt.Println("Usage: app [options]")
flag.PrintDefaults()
}
flag.Parse()
}
By mastering these practical flag usage techniques, developers can create robust, flexible, and user-friendly command-line applications with comprehensive configuration capabilities.