-
Notifications
You must be signed in to change notification settings - Fork 27
/
options.go
134 lines (112 loc) · 3.77 KB
/
options.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
package cli
import (
"flag"
"fmt"
"path/filepath"
"sort"
"strings"
"github.com/fatih/color"
"github.com/nomad-software/meme/data"
"github.com/nomad-software/meme/output"
)
var (
ImageIds []string
)
// Initialise the package.
func init() {
images, err := data.Files.ReadDir(data.ImagePath)
output.OnError(err, "Could not read embedded images")
for _, image := range images {
id := strings.TrimSuffix(filepath.Base(image.Name()), data.ImageExtension)
ImageIds = append(ImageIds, id)
}
sort.Strings(ImageIds)
}
// Options holds the options passed on the command line.
type Options struct {
Gif bool
Bottom string
ClientID string
Help bool
Image string
ImageType string
OutName string
Shake bool
Top string
Trigger bool
ListTemplates bool
}
// ParseOptions parses the command line options.
func ParseOptions() Options {
var opt Options
var text string
flag.BoolVar(&opt.Help, "h", false, "Show help.\n")
flag.BoolVar(&opt.Help, "help", false, "Show help.\n")
flag.StringVar(&opt.ClientID, "cid", "", "The client id of an application registered with imgur.com.\nIf specified, the new meme will be uploaded to imgur.com.\n(See README for full details.)\n")
flag.StringVar(&opt.Image, "i", "", "A built-in template, a URL or the path to a local file.\nYou can also use '-' to read an image from stdin.\n")
flag.StringVar(&opt.OutName, "o", "", "The optional name of the output file.\nIf omitted, a temporary file will be created.\n")
flag.StringVar(&text, "t", "", "The meme text. Separate the top and bottom banners using a pipe '|'.\n")
flag.BoolVar(&opt.Gif, "gif", false, "Gif animations will be preserved and the output will be a gif.\nDoes nothing for other image types.\n")
flag.BoolVar(&opt.Shake, "shake", false, "Shake the image to intensify it. Always outputs a gif.\n")
flag.BoolVar(&opt.Trigger, "trigger", false, "Shake the image and add a triggered banner. Always outputs a gif.\n")
flag.BoolVar(&opt.ListTemplates, "list-templates", false, "List all of the built in templates.\n")
flag.Parse()
parsed := strings.Split(text, "|")
if len(parsed) == 1 {
opt.Top = parsed[0]
} else {
opt.Top = parsed[0]
opt.Bottom = parsed[1]
}
return opt
}
// Valid validates the command line options and returns true if they are valid,
// false if not.
func (opt *Options) Valid() bool {
if opt.Image == "" {
output.Error("An image is required")
}
if !(opt.Gif || opt.Trigger || opt.Shake) && opt.OutName != "" {
if !strings.HasSuffix(strings.ToLower(opt.OutName), ".png") {
output.Error("The output file name must have the suffix of .png")
}
}
if (opt.Gif || opt.Trigger || opt.Shake) && opt.OutName != "" {
if !strings.HasSuffix(strings.ToLower(opt.OutName), ".gif") {
output.Error("The output file name must have the suffix of .gif")
}
}
return true
}
// PrintUsage prints who to use this command.
func (opt *Options) PrintUsage() {
var banner = ` _ __ ___ ___ _ __ ___ ___
| '_ ' _ \ / _ \ '_ ' _ \ / _ \
| | | | | | __/ | | | | | __/
|_| |_| |_|\___|_| |_| |_|\___|
`
color.Green(banner)
fmt.Println("")
flag.Usage()
fmt.Println("")
fmt.Println(" Templates")
fmt.Println("")
for x, name := range ImageIds {
if ((x + 1) % 2) == 0 {
fmt.Fprintln(output.Stdout, color.CyanString("%s", name))
} else {
fmt.Fprint(output.Stdout, color.CyanString(" %-30s", name))
}
}
if len(ImageIds)%3 != 0 {
fmt.Println("")
}
fmt.Println("")
fmt.Println(" Examples")
fmt.Println("")
color.Cyan(" meme -i kirk-khan -t \"|khaaaan\"")
color.Cyan(" meme -i brace-yourselves -t \"Brace yourselves|The memes are coming!\"")
color.Cyan(" meme -i http://i.imgur.com/FsWetC0.jpg -t \"|China\"")
color.Cyan(" meme -i ~/Pictures/face.png -t \"Hello\"")
fmt.Println("")
}