Compare commits

..

2 Commits

Author SHA1 Message Date
Aaron Johnon 50aa0c1166 Improved help and version messages
9 months ago
Aaron Johnon 2b00d96627 Added version flag
9 months ago

@ -8,14 +8,17 @@ import (
"net/url"
"os"
"path/filepath"
"strings"
"regexp"
"strconv"
"strings"
"time"
"github.com/spf13/pflag"
"gopkg.in/yaml.v2"
)
var Version = "development"
type Config struct {
ChatID string `yaml:"CHATID"`
Token string `yaml:"TOKEN"`
@ -23,23 +26,51 @@ type Config struct {
}
var (
configFile string
chatID string
token string
timeout int
silent bool
configFile string
chatID string
token string
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("%s\n", versionMsg)
os.Exit(0)
}
if helpFlag {
pflag.Usage()
os.Exit(0)
}
args := pflag.Args()
message := strings.Join(args, " ")
@ -184,3 +215,9 @@ 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, "")
}

Loading…
Cancel
Save