From 97fa6c94c5b12bd2430887c07978ac335ddeb7c4 Mon Sep 17 00:00:00 2001 From: Ralph Slooten Date: Sat, 24 Aug 2019 23:17:55 +1200 Subject: [PATCH] Switch to pflag --- CHANGELOG.md | 5 +++++ Makefile | 3 ++- goptimize.go | 14 +++++++------- main.go | 32 ++++++++++++++++++++++---------- 4 files changed, 36 insertions(+), 18 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index b318eb6..89f5a32 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,5 +1,10 @@ # Changelog +## [dev] + +- Switch to [pflag](https://github.com/spf13/pflag) for better flag management + + ## [0.0.1] - Initial release diff --git a/Makefile b/Makefile index 08d7a4b..623867a 100644 --- a/Makefile +++ b/Makefile @@ -6,7 +6,7 @@ build = echo "\n\nBuilding $(1)-$(2)" && GOOS=$(1) GOARCH=$(2) go build ${LDFLAG && bzip2 dist/goptimize_${VERSION}_$(1)_$(2) goptimize: *.go - go get github.com/disintegration/imaging golang.org/x/image/bmp golang.org/x/image/tiff github.com/axllent/gitrel + go get github.com/disintegration/imaging golang.org/x/image/bmp golang.org/x/image/tiff github.com/axllent/gitrel github.com/spf13/pflag go build ${LDFLAGS} -o goptimize rm -rf /tmp/go-* @@ -16,6 +16,7 @@ clean: release: mkdir -p dist rm -f dist/goptimize_${VERSION}_* + go get github.com/disintegration/imaging golang.org/x/image/bmp golang.org/x/image/tiff github.com/axllent/gitrel github.com/spf13/pflag $(call build,linux,amd64) $(call build,linux,386) $(call build,linux,arm) diff --git a/goptimize.go b/goptimize.go index 60049e6..8f56c0d 100644 --- a/goptimize.go +++ b/goptimize.go @@ -116,19 +116,19 @@ func Goptimize(file string) { if format.String() == "JPEG" { // run one or the other, running both has no advantage if jpegtran != "" { - RunOptimiser(tmpFilename, true, jpegtran, "-optimize", "-outfile") + RunOptimizer(tmpFilename, true, jpegtran, "-optimize", "-outfile") } else if jpegoptim != "" { - RunOptimiser(tmpFilename, false, jpegoptim, "-f", "-s", "-o") + RunOptimizer(tmpFilename, false, jpegoptim, "-f", "-s", "-o") } } else if format.String() == "PNG" { if pngquant != "" { - RunOptimiser(tmpFilename, true, pngquant, "-f", "--output") + RunOptimizer(tmpFilename, true, pngquant, "-f", "--output") } if optipng != "" { - RunOptimiser(tmpFilename, true, optipng, "-out") + RunOptimizer(tmpFilename, true, optipng, "-out") } } else if format.String() == "GIF" && gifsicle != "" { - RunOptimiser(tmpFilename, true, gifsicle, "-o") + RunOptimizer(tmpFilename, true, gifsicle, "-o") } // re-open modified temporary file @@ -215,9 +215,9 @@ func Goptimize(file string) { } -// RunOptimiser will run the specified command on a copy of the temporary file, +// RunOptimizer will run the specified command on a copy of the temporary file, // and overwrite it if the output is smaller than the original -func RunOptimiser(src string, outFileArg bool, args ...string) { +func RunOptimizer(src string, outFileArg bool, args ...string) { // create a new temp file tmpFile, err := ioutil.TempFile(os.TempDir(), "Goptimized-") diff --git a/main.go b/main.go index 67e0e04..65197d2 100644 --- a/main.go +++ b/main.go @@ -1,7 +1,6 @@ package main import ( - "flag" "fmt" "os" "os/exec" @@ -9,6 +8,7 @@ import ( "strconv" "github.com/axllent/gitrel" + "github.com/spf13/pflag" ) var ( @@ -26,11 +26,15 @@ var ( ) func main() { + // set up new flag instance + flag := pflag.NewFlagSet(os.Args[0], pflag.ExitOnError) + // set the default help flag.Usage = func() { fmt.Println("Goptimize - downscales and optimizes images") fmt.Printf("\nUsage: %s [options] \n", os.Args[0]) fmt.Println("\nOptions:") + flag.SortFlags = false flag.PrintDefaults() fmt.Println("\nExamples:") fmt.Printf(" %s image.png\n", os.Args[0]) @@ -47,14 +51,15 @@ func main() { } var maxSizes string - var update, showversion bool + var update, showversion, showhelp bool - flag.IntVar(&quality, "q", 75, "quality - JPEG only") - flag.StringVar(&outputDir, "o", "", "output directory (default overwrites original)") - flag.BoolVar(&preserveModTimes, "p", true, "preserve file modification times") - flag.StringVar(&maxSizes, "m", "", "downscale to a maximum width & height in pixels (x)") - flag.BoolVar(&update, "u", false, "update to latest release") - flag.BoolVar(&showversion, "v", false, "show version number") + flag.IntVarP(&quality, "quality", "q", 75, "quality - JPEG only") + flag.StringVarP(&maxSizes, "max", "m", "", "downscale to a maximum width & height in pixels (x)") + flag.StringVarP(&outputDir, "out", "o", "", "output directory (default overwrites original)") + flag.BoolVarP(&preserveModTimes, "preserve", "p", true, "preserve file modification times") + flag.BoolVarP(&update, "update", "u", false, "update to latest release") + flag.BoolVarP(&showversion, "version", "v", false, "show version number") + flag.BoolVarP(&showhelp, "help", "h", false, "show help") // third-party optimizers flag.StringVar(&gifsicle, "gifsicle", "gifsicle", "gifsicle binary") @@ -63,8 +68,10 @@ func main() { flag.StringVar(&optipng, "optipng", "optipng", "optipng binary") flag.StringVar(&pngquant, "pngquant", "pngquant", "pngquant binary") - // parse flags - flag.Parse() + flag.SortFlags = false + + // parse args excluding os.Args[0] + flag.Parse(os.Args[1:]) // detect optimizer paths gifsicle, _ = exec.LookPath(gifsicle) @@ -73,6 +80,11 @@ func main() { optipng, _ = exec.LookPath(optipng) pngquant, _ = exec.LookPath(pngquant) + if showhelp { + flag.Usage() + os.Exit(1) + } + if showversion { fmt.Println(fmt.Sprintf("Version: %s", version)) latest, _, _, err := gitrel.Latest("axllent/goptimize", "goptimize")