From 2e1aee0313884d0edb573a6991a44a8e93a0cabe Mon Sep 17 00:00:00 2001 From: ChaoticByte Date: Wed, 11 Jun 2025 17:45:14 +0200 Subject: [PATCH] Parse -h, --help and --version and show help, version --- main.go | 30 +++++++++++++++++++++++++----- 1 file changed, 25 insertions(+), 5 deletions(-) diff --git a/main.go b/main.go index 47ce30b..e2fea3a 100644 --- a/main.go +++ b/main.go @@ -10,19 +10,39 @@ import ( "time" ) +var executableName string var logger Logger + +func showVersion() { + fmt.Printf("wid-notifier %s\n", Version) +} + +func showHelp() { + fmt.Printf("Usage: %v \n\nIf the config file doesn't exist, an incomplete \n" + + "configuration with default values is created.\n\n", + executableName) + showVersion() +} + func main() { // get cli arguments args := os.Args + executableName = args[0] if len(args) < 2 { - fmt.Printf( "Usage: %v \n\nIf the config file doesn't exist, an incomplete \n" + - "configuration with default values is created.\n\nVersion: %s\n", - args[0], - Version) + showHelp() os.Exit(1) } - configFilePath := os.Args[1] + for _, arg := range args { + if arg == "-h" || arg == "--help" { + showHelp() + os.Exit(0) + } else if arg == "--version" { + showVersion() + os.Exit(0) + } + } + configFilePath := args[1] // create logger logger = NewLogger(2) // init