Skip to content

Commit

Permalink
Remove positional arguments support
Browse files Browse the repository at this point in the history
  • Loading branch information
qbart committed Dec 6, 2021
1 parent f79b9e3 commit efabcd5
Show file tree
Hide file tree
Showing 5 changed files with 6 additions and 23 deletions.
25 changes: 6 additions & 19 deletions cliargs/parser.go
Original file line number Diff line number Diff line change
@@ -1,43 +1,30 @@
package cliargs

import (
"errors"
"flag"
)

type parser struct {
args []string
flags *flag.FlagSet
requiredNonFlagArgs int
stringValues map[string]*string
args []string
flags *flag.FlagSet
stringValues map[string]*string
}

func New(args []string) *parser {
flags := flag.NewFlagSet("", flag.ExitOnError)
return &parser{
args: args,
flags: flags,
requiredNonFlagArgs: -1,
stringValues: map[string]*string{},
args: args,
flags: flags,
stringValues: map[string]*string{},
}
}

func (p *parser) RequireNonFlagArgs(n int) {
p.requiredNonFlagArgs = n
}

func (p *parser) Parse() error {
err := p.flags.Parse(p.args)
if err != nil {
return err
}

if p.requiredNonFlagArgs != -1 {
if len(p.flags.Args()) != p.requiredNonFlagArgs {
return errors.New("Invalid number of arguments")
}
}

return nil
}

Expand Down
1 change: 0 additions & 1 deletion krab/action_custom.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,6 @@ func (a *ActionCustom) Synopsis() string {
func (a *ActionCustom) Run(args []string) int {
ui := a.Ui
flags := cliargs.New(args)
flags.RequireNonFlagArgs(0)

for _, arg := range a.Action.Arguments.Args {
flags.Add(arg.Name)
Expand Down
1 change: 0 additions & 1 deletion krab/action_migrate_down.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,6 @@ func (a *ActionMigrateDown) Synopsis() string {
func (a *ActionMigrateDown) Run(args []string) int {
ui := a.Ui
flags := cliargs.New(args)
flags.RequireNonFlagArgs(0)

for _, arg := range a.Set.Arguments.Args {
flags.Add(arg.Name)
Expand Down
1 change: 0 additions & 1 deletion krab/action_migrate_status.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,6 @@ func (a *ActionMigrateStatus) Synopsis() string {
func (a *ActionMigrateStatus) Run(args []string) int {
ui := a.Ui
flags := cliargs.New(args)
flags.RequireNonFlagArgs(0)

for _, arg := range a.Set.Arguments.Args {
flags.Add(arg.Name)
Expand Down
1 change: 0 additions & 1 deletion krab/action_migrate_up.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,6 @@ func (a *ActionMigrateUp) Synopsis() string {
func (a *ActionMigrateUp) Run(args []string) int {
ui := a.Ui
flags := cliargs.New(args)
flags.RequireNonFlagArgs(0)

for _, arg := range a.Set.Arguments.Args {
flags.Add(arg.Name)
Expand Down

0 comments on commit efabcd5

Please sign in to comment.