Add function: usage

This commit is contained in:
Reza Behzadan 2024-02-15 14:31:50 +03:30
parent 61878fd4d1
commit 5253d3c121

18
main.go
View File

@ -16,6 +16,12 @@ type Args struct {
Help 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 // ParseArgs parses the command-line arguments into a Args struct
func ParseArgs() Args { func ParseArgs() Args {
var versionShortFlag bool var versionShortFlag bool
@ -25,11 +31,11 @@ func ParseArgs() Args {
var helpShortFlag bool var helpShortFlag bool
var helpLongFlag bool var helpLongFlag bool
flag.BoolVar(&versionShortFlag, "v", false, "Print version information") flag.BoolVar(&versionShortFlag, "v", false, "")
flag.BoolVar(&versionLongFlag, "version", false, "Print version information") flag.BoolVar(&versionLongFlag, "version", false, "Print version information")
flag.BoolVar(&debugShortFlag, "d", false, "Enable debug mode") flag.BoolVar(&debugShortFlag, "d", false, "")
flag.BoolVar(&debugLongFlag, "debug", false, "Enable debug mode") flag.BoolVar(&debugLongFlag, "debug", false, "Enable debug mode")
flag.BoolVar(&helpShortFlag, "h", false, "Show help") flag.BoolVar(&helpShortFlag, "h", false, "")
flag.BoolVar(&helpLongFlag, "help", false, "Show help") flag.BoolVar(&helpLongFlag, "help", false, "Show help")
flag.Parse() flag.Parse()
@ -46,6 +52,7 @@ func init() {
func main() { func main() {
args := ParseArgs() args := ParseArgs()
flag.Usage = usage
// Handle version flag // Handle version flag
if args.Version { if args.Version {
@ -55,9 +62,8 @@ func main() {
// Handle help // Handle help
if args.Help { if args.Help {
fmt.Println("Usage: mycli [options]") flag.Usage()
flag.PrintDefaults() return
os.Exit(0)
} }
fmt.Println("Hello, World!") fmt.Println("Hello, World!")