Skip to content

Commit

Permalink
fix: use getopt library for argument parsing
Browse files Browse the repository at this point in the history
fixes #23

Signed-off-by: moson-mo <mo-son@mailbox.org>
  • Loading branch information
moson-mo committed Oct 21, 2022
1 parent 8f5ee49 commit e4a9bc5
Show file tree
Hide file tree
Showing 4 changed files with 39 additions and 59 deletions.
2 changes: 2 additions & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -31,8 +31,10 @@ require (
github.com/gdamore/encoding v1.0.0 // indirect
github.com/lucasb-eyer/go-colorful v1.2.0 // indirect
github.com/mattn/go-runewidth v0.0.14 // indirect
github.com/pborman/getopt/v2 v2.1.0
github.com/rivo/uniseg v0.4.2 // indirect
github.com/stretchr/testify v1.7.1
golang.org/x/exp v0.0.0-20221019170559-20944726eadf
golang.org/x/sys v0.1.0 // indirect
golang.org/x/term v0.1.0 // indirect
golang.org/x/text v0.4.0 // indirect
Expand Down
4 changes: 4 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,8 @@ github.com/modern-go/reflect2 v1.0.2 h1:xBagoLtFs94CBntxluKeaWgTMpvLxC4ur3nMaC9G
github.com/modern-go/reflect2 v1.0.2/go.mod h1:yWuevngMOJpCy52FWWMvUC8ws7m/LJsjYzDa0/r8luk=
github.com/patrickmn/go-cache v2.1.0+incompatible h1:HRMgzkcYKYpi3C8ajMPV8OFXaaRUnok+kx1WdO15EQc=
github.com/patrickmn/go-cache v2.1.0+incompatible/go.mod h1:3Qf8kWWT7OJRJbdiICTKqZju1ZixQ/KpMGzzAfe6+WQ=
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/tview v0.0.0-20220916081518-2e69b7385a37 h1:cTzFg1FfTXwXuODi7Doz70hsW+dAye1OBwAFWHCqmww=
Expand All @@ -62,6 +64,8 @@ github.com/stretchr/testify v1.7.1 h1:5TQK59W5E3v0r2duFAb7P95B6hEeOyEnHRa8MjYSMT
github.com/stretchr/testify v1.7.1/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg=
github.com/urfave/cli v1.22.3/go.mod h1:Gos4lmkARVdJ6EkW0WaNv/tZAAMe9V7XWyB60NtXRu0=
golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACkg1iLfiJU5Ep61QUkGW8qpdssI0+w=
golang.org/x/exp v0.0.0-20221019170559-20944726eadf h1:nFVjjKDgNY37+ZSYCJmtYf7tOlfQswHqplG2eosjOMg=
golang.org/x/exp v0.0.0-20221019170559-20944726eadf/go.mod h1:cyybsKvd6eL0RnXn6p/Grxp8F5bW7iYuBgsNCOHpMYE=
golang.org/x/net v0.0.0-20180218175443-cbe0f9307d01/go.mod h1:mL1N/T3taQHkDXs73rZJwtUhF3w3ftmwwsq0BUmARs4=
golang.org/x/net v0.0.0-20200202094626-16171245cfb2/go.mod h1:z5CRVTTTmAJ677TzLLGU+0bjPO0LkuOLi4/5GtJWs/s=
golang.org/x/net v0.0.0-20200301022130-244492dfa37a/go.mod h1:z5CRVTTTmAJ677TzLLGU+0bjPO0LkuOLi4/5GtJWs/s=
Expand Down
83 changes: 29 additions & 54 deletions internal/args/args.go
Original file line number Diff line number Diff line change
@@ -1,10 +1,9 @@
package args

import (
"errors"
"strings"

"github.com/moson-mo/pacseek/internal/util"
"github.com/pborman/getopt/v2"
)

// Flags struct holds our flag options
Expand All @@ -15,65 +14,41 @@ type Flags struct {
MonochromeMode bool
ShowUpdates bool
ShowInstalled bool
Help bool
}

// Parse is parsing our arguments and creates a Flags struct from it
func Parse(args []string) (*Flags, error) {
flags := &Flags{}
prevFlag := ""

// set search term to first argument if it's not a flag
if len(args) > 0 && !strings.HasPrefix(args[0], "-") {
flags.SearchTerm = args[0]
}

for i, flag := range args {
// set search term to the last argument if it's not a flag and the previous flag was not a string flag
if len(args) == i+1 && !strings.HasPrefix(flag, "-") && !util.SliceContains(stringFlags(), prevFlag) {
flags.SearchTerm = flag
func Parse() Flags {
repos := getopt.String('r', "", "Limit searching to a comma separated list of repositories")
term := getopt.String('s', "", "Search-term")
ascii := getopt.Bool('a', "ASCII mode")
mono := getopt.Bool('m', "Monochrome mode")
upd := getopt.Bool('u', "Show updates after startup")
inst := getopt.Bool('i', "Show installed packages after startup")
help := getopt.BoolLong("help", 'h', "Show usage / help")
qhelp := getopt.BoolLong("?", '?', "Show usage / help")

err := getopt.Getopt(nil)
if err != nil {
return Flags{
Help: true,
}
// if we have a flag
if strings.HasPrefix(flag, "-") {
flag = strings.ReplaceAll(flag, "-", "") // strip dashes from flag
}

// iterate over characters in flag to allow several options in just one flag, e.g. "-amui"
for _, r := range flag {
if !util.SliceContains(possibleFlags(), string(r)) {
return nil, errors.New("flag not supported: " + flag)
}
switch r {
case 'a':
flags.AsciiMode = true
case 'm':
flags.MonochromeMode = true
case 'u':
flags.ShowUpdates = true
case 'i':
flags.ShowInstalled = true
case 's':
if len(args) <= i+1 {
return nil, errors.New("search-term argument is missing")
}
flags.SearchTerm = args[i+1]
case 'r':
if len(args) <= i+1 {
return nil, errors.New("repositories argument is missing")
}
flags.Repositories = strings.Split(args[i+1], ",")
}
}
}
prevFlag = flag
flags := Flags{
Repositories: strings.Split(*repos, ","),
SearchTerm: *term,
AsciiMode: *ascii,
MonochromeMode: *mono,
ShowUpdates: *upd,
ShowInstalled: *inst,
}
flags.SearchTerm = strings.ToLower(flags.SearchTerm)

return flags, nil
}
flags.Help = *help || *qhelp

func possibleFlags() []string {
return []string{"s", "r", "a", "m", "u", "i"}
}
if flags.SearchTerm == "" && len(getopt.Args()) > 0 {
flags.SearchTerm = getopt.Args()[0]
}

func stringFlags() []string {
return []string{"s", "r"}
return flags
}
9 changes: 4 additions & 5 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,11 +43,10 @@ func main() {
fmt.Println("pacseek should not be run as root.")
}

f, err := args.Parse(os.Args[1:])
if err != nil {
fmt.Println(err)
f := args.Parse()
if f.Help {
printHelp()
os.Exit(1)
os.Exit(0)
}

conf, err := config.Load()
Expand All @@ -61,7 +60,7 @@ func main() {
printErrorExit("Error loading configuration file", err)
}
}
ps, err := pacseek.New(conf, *f)
ps, err := pacseek.New(conf, f)
if err != nil {
printErrorExit("Error during pacseek initialization", err)
}
Expand Down

0 comments on commit e4a9bc5

Please sign in to comment.