Last active
April 26, 2018 00:54
-
-
Save tehmoon/728390fc94c9463849bb95f24d172acf to your computer and use it in GitHub Desktop.
Bootstrap go
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package main | |
import ( | |
"flag" | |
"fmt" | |
"os" | |
) | |
type Flags struct { | |
} | |
func parseFlags() (*Flags, error) { | |
flags := &Flags{} | |
// Declareflags here | |
err := flag.CommandLine.Parse(os.Args[1:]) | |
if err != nil { | |
return nil, err | |
} | |
return flags, err | |
} | |
func initFlags() { | |
flag.CommandLine = flag.NewFlagSet(os.Args[0], flag.ContinueOnError) | |
flag.Usage = func() { | |
fmt.Fprintf(os.Stderr, "Usage of %s:\n", os.Args[0]) | |
flag.PrintDefaults() | |
os.Exit(2 /* change number here */ | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package main | |
func init() { | |
initFlags() | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package main | |
import ( | |
"fmt" | |
"os" | |
) | |
func main() { | |
flags, err := parseFlags() | |
if err != nil { | |
if err == flag.ErrHelp { | |
os.Exit(2 /* change numbmer here */) | |
} | |
// do something here | |
flag.Usage() | |
} | |
fmt.Println(flags) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment