gocli_simple/main.go

74 lines
1.4 KiB
Go

package main
import (
"flag"
"fmt"
"os"
)
// Version of the program
const VERSION = "1.0.0"
// Config holds all command-line arguments
type Args struct {
Version bool
Debug bool
Help bool
}
// Custom usage function to display help
func usage() {
fmt.Println("Usage: mycli [options]")
flag.PrintDefaults()
}
// ParseArgs parses the command-line arguments into a Args struct
func ParseArgs() Args {
var versionShortFlag bool
var versionLongFlag bool
var debugShortFlag bool
var debugLongFlag bool
var helpShortFlag bool
var helpLongFlag bool
flag.BoolVar(&versionShortFlag, "v", false, "")
flag.BoolVar(&versionLongFlag, "version", false, "Print version information")
flag.BoolVar(&debugShortFlag, "d", false, "")
flag.BoolVar(&debugLongFlag, "debug", false, "Enable debug mode")
flag.BoolVar(&helpShortFlag, "h", false, "")
flag.BoolVar(&helpLongFlag, "help", false, "Show help")
flag.Parse()
return Args{
Version: versionShortFlag || versionLongFlag,
Debug: debugShortFlag || debugLongFlag,
Help: helpShortFlag || helpLongFlag,
}
}
// init is called before main. Use it to set up your app
func init() {
}
func main() {
args := ParseArgs()
flag.Usage = usage
// Handle version flag
if args.Version {
fmt.Println(VERSION)
os.Exit(0)
}
// Handle help
if args.Help {
flag.Usage()
return
}
fmt.Println("Hello, World!")
if args.Debug {
fmt.Println("Debug mode is on!")
}
}