From 50aa0c1166659ef8986efe7feee08cacdd16e330 Mon Sep 17 00:00:00 2001 From: Aaron Johnon Date: Tue, 3 Dec 2024 23:07:52 -0600 Subject: [PATCH] Improved help and version messages --- tbotsend.go | 36 ++++++++++++++++++++++++++++++++---- 1 file changed, 32 insertions(+), 4 deletions(-) diff --git a/tbotsend.go b/tbotsend.go index 4cfde21..23095be 100644 --- a/tbotsend.go +++ b/tbotsend.go @@ -8,6 +8,7 @@ import ( "net/url" "os" "path/filepath" + "regexp" "strconv" "strings" "time" @@ -31,22 +32,42 @@ var ( timeout int silent bool versionFlag bool + helpFlag bool ) func init() { pflag.StringVarP(&configFile, "config", "f", "", "Configuration file path") pflag.StringVarP(&chatID, "chatid", "c", "", "Chat ID value") - pflag.StringVarP(&token, "token", "k", "", "Bot token value") - pflag.IntVarP(&timeout, "timeout", "t", 10, "Timeout value (defaults to 10)") - pflag.BoolVarP(&silent, "silent", "s", false, "Disable notification") + pflag.StringVarP(&token, "token", "t", "", "Bot token value") + pflag.IntVarP(&timeout, "timeout", "", 10, "Timeout value") + pflag.BoolVarP(&silent, "silent", "s", false, "Disable notification sound") pflag.BoolVarP(&versionFlag, "version", "v", false, "Print version information and exit") + pflag.BoolVarP(&helpFlag, "help", "h", false, "Prints this message") } func main() { + //Set version message + versionMsg := fmt.Sprintf("\033[0;36mtbotsend\033[0m - Version: \033[0;33m%s\033[0m", Version) + pflag.Usage = func() { + fmt.Fprintf(os.Stderr, "%s\n", versionMsg) + fmt.Fprintf(os.Stderr, "%s\n", strings.Repeat("-", len(stripANSI(versionMsg)))) + fmt.Fprintf(os.Stderr, "Usage: %s [OPTIONS] {MESSAGE}\n\n", os.Args[0]) + fmt.Fprintf(os.Stderr, "Options:\n") + pflag.PrintDefaults() + fmt.Fprintf(os.Stderr, "\nExamples:\n") + fmt.Fprintf(os.Stderr, " %s -f config.yaml -c 123456 -t botToken123 --silent Hello world!\n", os.Args[0]) + fmt.Fprintf(os.Stderr, " %s --version\n", os.Args[0]) + } pflag.Parse() if versionFlag { - fmt.Printf("tbotsend version %s\n", Version) + //fmt.Printf("tbotsend version %s\n", Version) + fmt.Printf("%s\n", versionMsg) + os.Exit(0) + } + + if helpFlag { + pflag.Usage() os.Exit(0) } @@ -193,3 +214,10 @@ func printFailure(msg string) { func printSuccess() { fmt.Fprintf(os.Stdout, "\033[1mResult:\033[0m [\033[1;32mSUCCESS\033[0m]\n\n") } + +// Strip ANSI escape sequences for accurate string length assessment +func stripANSI(input string) string { + ansiPattern := `\033\[[0-9;]*[a-zA-Z]` + re := regexp.MustCompile(ansiPattern) + return re.ReplaceAllString(input, "") +}