doc.go (2280B)
1 package main 2 3 import ( 4 "flag" 5 "fmt" 6 "strconv" 7 ) 8 9 // Stamped by goreleaser at build time, through the linker. 10 var ( 11 version = "master" 12 commit = "" 13 date = "" 14 builtBy = "" 15 ) 16 17 // buildInfo renders the version and whatever else the build stamped. 18 func buildInfo() string { 19 out := "icnsify " + version 20 for _, part := range []struct{ label, value string }{ 21 {"commit", commit}, 22 {"built", date}, 23 {"by", builtBy}, 24 } { 25 if part.value != "" { 26 out += fmt.Sprintf(", %s %s", part.label, part.value) 27 } 28 } 29 return out 30 } 31 32 // option records a flag registered under both a long and a short name, so 33 // usage can list the pair once, GNU style. 34 type option struct { 35 long, short string 36 def, usage string 37 } 38 39 var options []option 40 41 // stringFlag registers a string option reachable as --long or -short. 42 func stringFlag(p *string, long, short, def, usage string) { 43 flag.StringVar(p, long, def, usage) 44 flag.StringVar(p, short, def, usage) 45 options = append(options, option{long: long, short: short, def: def, usage: usage}) 46 } 47 48 // intFlag registers an integer option reachable as --long or -short. 49 func intFlag(p *int, long, short string, def int, usage string) { 50 flag.IntVar(p, long, def, usage) 51 flag.IntVar(p, short, def, usage) 52 options = append(options, option{long: long, short: short, def: strconv.Itoa(def), usage: usage}) 53 } 54 55 // boolFlag registers a boolean option reachable as --long or -short. 56 func boolFlag(p *bool, long, short string, usage string) { 57 flag.BoolVar(p, long, false, usage) 58 flag.BoolVar(p, short, false, usage) 59 options = append(options, option{long: long, short: short, usage: usage}) 60 } 61 62 func usage() { 63 w := flag.CommandLine.Output() 64 fmt.Fprintf(w, "%s\n\nUsage: icnsify [-i input] [-o output] [-f format] [-r quality] [-c]\n\nOptions:\n", buildInfo()) 65 for _, o := range options { 66 fmt.Fprintf(w, " -%s, --%s\n %s", o.short, o.long, o.usage) 67 if o.def != "" && o.def != "0" { 68 fmt.Fprintf(w, " (default %s)", o.def) 69 } 70 fmt.Fprintln(w) 71 } 72 fmt.Fprint(w, ` 73 You can also pipe to stdin and from stdout. Pipes are detected automatically 74 when --input is not given, and --output is then ignored. 75 76 cat icon.png | icnsify > icon.icns 77 cat icon.icns | icnsify > icon.png 78 cat icon.png | icnsify -f ico > icon.ico 79 `) 80 }