Skip to content

Commit

Permalink
use getopt for cli args
Browse files Browse the repository at this point in the history
this allows long and short options
  • Loading branch information
tmck-code committed Mar 11, 2024
1 parent 54605ef commit 27ec953
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 15 deletions.
1 change: 1 addition & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ require (
github.com/mattn/go-isatty v0.0.17 // indirect
github.com/mattn/go-runewidth v0.0.14 // indirect
github.com/mitchellh/colorstring v0.0.0-20190213212951-d06e56a500db // indirect
github.com/pborman/getopt/v2 v2.1.0 // indirect
github.com/rivo/uniseg v0.2.0 // indirect
golang.org/x/sys v0.6.0 // indirect
golang.org/x/term v0.6.0 // indirect
Expand Down
2 changes: 2 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@ github.com/mitchellh/colorstring v0.0.0-20190213212951-d06e56a500db h1:62I3jR2Em
github.com/mitchellh/colorstring v0.0.0-20190213212951-d06e56a500db/go.mod h1:l0dey0ia/Uv7NcFFVbCLtqEBQbrT4OCwCSKTEv6enCw=
github.com/mitchellh/go-wordwrap v1.0.1 h1:TLuKupo69TCn6TQSyGxwI1EblZZEsQ0vMlAFQflz0v0=
github.com/mitchellh/go-wordwrap v1.0.1/go.mod h1:R62XHJLzvMFRBbcrT7m7WgmE1eOyTSsCt+hzestvNj0=
github.com/pborman/getopt/v2 v2.1.0 h1:eNfR+r+dWLdWmV8g5OlpyrTYHkhVNxHBdN2cCrJmOEA=
github.com/pborman/getopt/v2 v2.1.0/go.mod h1:4NtW75ny4eBw9fO1bhtNdYTlZKYX5/tBLtsOpwKIKd0=
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
github.com/rivo/uniseg v0.2.0 h1:S1pD9weZBuJdFmowNwbpi7BJ8TNftyUImj/0WQi72jY=
Expand Down
34 changes: 19 additions & 15 deletions pokesay.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,10 @@ package main

import (
"embed"
"flag"
"fmt"
"strings"

"github.com/pborman/getopt/v2"
"github.com/tmck-code/pokesay/src/pokedex"
"github.com/tmck-code/pokesay/src/pokesay"
"github.com/tmck-code/pokesay/src/timer"
Expand All @@ -29,33 +29,37 @@ var (
CategoryRoot string = "build/assets/categories"
MetadataRoot string = "build/assets/metadata"
CowDataRoot string = "build/assets/cows"

verbose bool
)

func parseFlags() pokesay.Args {
// list operations
listCategories := flag.Bool("list-categories", false, "list all available categories")
listNames := flag.Bool("list-names", false, "list all available names")
listCategories := getopt.BoolLong("list-categories", 'L', "list all available categories")
listNames := getopt.BoolLong("list-names", 'l', "list all available names")

// selection/filtering
category := flag.String("category", "", "choose a pokemon from a specific category")
name := flag.String("name", "", "choose a pokemon from a specific name")
width := flag.Int("width", 80, "the max speech bubble width")
category := getopt.StringLong("category", 'c', "", "choose a pokemon from a specific category")
name := getopt.StringLong("name", 'n', "", "choose a pokemon from a specific name")
width := getopt.IntLong("width", 'w', 80, "the max speech bubble width")

getopt.FlagLong(&verbose, "verbose", 'v', "print verbose output", "verbose")

// speech bubble options
noWrap := flag.Bool("no-wrap", false, "disable text wrapping (fastest)")
tabWidth := flag.Int("tab-width", 4, "replace any tab characters with N spaces")
noTabSpaces := flag.Bool("no-tab-spaces", false, "do not replace tab characters (fastest)")
fastest := flag.Bool("fastest", false, "run with the fastest possible configuration (-nowrap -notabspaces)")
noWrap := getopt.BoolLong("no-wrap", 'W', "disable text wrapping Long(fastest)")
tabWidth := getopt.IntLong("tab-width", 't', 4, "replace any tab characters with N spaces")
noTabSpaces := getopt.BoolLong("no-tab-spaces", 's', "do not replace tab characters Long(fastest)")
fastest := getopt.BoolLong("fastest", 'f', "run with the fastest possible configuration Long(-nowrap -notabspaces)")

// info box options
japaneseName := flag.Bool("japanese-name", false, "print the japanese name")
noCategoryInfo := flag.Bool("no-category-info", false, "do not print pokemon categories")
drawInfoBorder := flag.Bool("info-border", false, "draw a border around the info line")
japaneseName := getopt.BoolLong("japanese-name", 'j', "print the japanese name")
noCategoryInfo := getopt.BoolLong("no-category-info", 'C', "do not print pokemon categories")
drawInfoBorder := getopt.BoolLong("info-border", 'b', "draw a border around the info line")

// other option
unicodeBorders := flag.Bool("unicode-borders", false, "use unicode characters to draw the border around the speech box (and info box if -info-border is enabled)")
unicodeBorders := getopt.BoolLong("unicode-borders", 'u', "use unicode characters to draw the border around the speech box Long(and info box if -info-border is enabled)")

flag.Parse()
getopt.Parse()
var args pokesay.Args

if *fastest {
Expand Down

0 comments on commit 27ec953

Please sign in to comment.