Skip to content

Commit

Permalink
Merge pull request #26 from clagraff/minor-fixes
Browse files Browse the repository at this point in the history
added minor fixes to various things
  • Loading branch information
clagraff authored Nov 7, 2016
2 parents 97d1d27 + fe9266c commit 1ee7ce1
Show file tree
Hide file tree
Showing 7 changed files with 113 additions and 100 deletions.
48 changes: 27 additions & 21 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -30,17 +30,8 @@ import (
"github.com/clagraff/argparse"
)

func main() {
p := argparse.NewParser("Output a friendly greeting").Version("1.3.0a")
p.AddHelp().AddVersion() // Enable help and version flags

upperFlag := argparse.NewFlag("u", "upper", "Use uppercase text").Default("false")
nameOption := argparse.NewArg("n name", "name", "Name of person to greet").Default("John").Required()

p.AddOptions(upperFlag, nameOption)

// Parse all available program arguments (except for the program path).
if ns, leftovers, err := p.Parse(os.Args[1:]...); err != nil {
func callback(p *argparse.Parser, ns *argparse.Namespace, leftovers []string, err error) {
if err != nil {
switch err.(type) {
case argparse.ShowHelpErr, argparse.ShowVersionErr:
// For either ShowHelpErr or ShowVersionErr, the parser has already
Expand All @@ -51,19 +42,34 @@ func main() {
fmt.Println(err, "\n")
p.ShowHelp()
}
} else {
name := ns.String("name")
upper := ns.String("upper") == "true"

if upper == true {
name = strings.ToUpper(name)
}
return // Exit program
}

fmt.Printf("Hello, %s!\n", name)
if len(leftovers) > 0 {
fmt.Println("\nUnused args:", leftovers)
}
name := ns.Get("name").(string)
upper := ns.Get("upper").(string) == "true"

if upper == true {
name = strings.ToUpper(name)
}

fmt.Printf("Hello, %s!\n", name)
if len(leftovers) > 0 {
fmt.Println("\nUnused args:", leftovers)
}
}

func main() {
p := argparse.NewParser("Output a friendly greeting", callback).Version("1.3.0a")
p.AddHelp().AddVersion() // Enable help and version flags

upperFlag := argparse.NewFlag("u", "upper", "Use uppercase text").Default("false")
nameOption := argparse.NewArg("n name", "name", "Name of person to greet").Default("John").Required()

p.AddOptions(upperFlag, nameOption)

// Parse all available program arguments (except for the program path).
p.Parse(os.Args[1:]...)
}
```

Expand Down
6 changes: 5 additions & 1 deletion actions.go
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,11 @@ func Store(p *Parser, f *Option, args ...string) ([]string, error) {
} else if regexp.MustCompile(`^[1-9]+$`).MatchString(f.ArgNum) == true {
num, _ := strconv.Atoi(f.ArgNum)
if len(args) < num {
return args, TooFewArgsErr{*f}
if f.IsRequired == true {
return args, TooFewArgsErr{*f}
} else {
return args, nil
}
}

if num > 1 {
Expand Down
26 changes: 13 additions & 13 deletions actions_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import "testing"
// return the appropriate args & error when operating upon a option with
// one expected argument.
func TestStore_OneNargs(t *testing.T) {
p := NewParser("parser")
p := NewParser("parser", emptyNamespace())
f := NewOption("option", "option", "option").Nargs("1")
args := []string{"foobar"}

Expand All @@ -25,7 +25,7 @@ func TestStore_OneNargs(t *testing.T) {
}

args = []string{}
_, err = Store(p, f, args...)
_, err = Store(p, f.Required(), args...)
if err == nil {
t.Error("An error was expected but did not occurr")
}
Expand All @@ -35,7 +35,7 @@ func TestStore_OneNargs(t *testing.T) {
// return the appropriate args & error when operating upon a option with
// three expected arguments.
func TestStore_ThreeNargs(t *testing.T) {
p := NewParser("parser")
p := NewParser("parser", emptyNamespace())
f := NewOption("option", "option", "option").Nargs("3")
args := []string{"foo", "bar", "fizzbuzz"}

Expand All @@ -54,7 +54,7 @@ func TestStore_ThreeNargs(t *testing.T) {
}

args = []string{}
_, err = Store(p, f, args...)
_, err = Store(p, f.Required(), args...)
if err == nil {
t.Error("An error was expected but did not occurr")
}
Expand All @@ -64,7 +64,7 @@ func TestStore_ThreeNargs(t *testing.T) {
// return the appropriate args & error when operating upon a option with
// any number of expected arguments.
func TestStore_AnyNargs(t *testing.T) {
p := NewParser("parser")
p := NewParser("parser", emptyNamespace())
f := NewOption("option", "option", "option").Nargs("*")
args := []string{"foo", "bar", "fizz", "buzz", "hello", "world"}

Expand Down Expand Up @@ -93,7 +93,7 @@ func TestStore_AnyNargs(t *testing.T) {
// return the appropriate args & error when operating upon a option with
// at least one number of expected arguments.
func TestStore_LeastOneNargs(t *testing.T) {
p := NewParser("parser")
p := NewParser("parser", emptyNamespace())
f := NewOption("option", "option", "option").Nargs("+")
args := []string{"foo", "fizz", "world"}

Expand All @@ -120,7 +120,7 @@ func TestStore_LeastOneNargs(t *testing.T) {

// TestStoreConst tests the StoreConst Action will store a option's ConstValue.
func TestStoreConst(t *testing.T) {
p := NewParser("parser")
p := NewParser("parser", emptyNamespace())
f := NewOption("option", "option", "option").Nargs("0").Const("hello world")
args := []string{}

Expand All @@ -141,7 +141,7 @@ func TestStoreConst(t *testing.T) {

// TestStoreFalse tests the StoreFalse Action will store a false boolean.
func TestStoreFalse(t *testing.T) {
p := NewParser("parser")
p := NewParser("parser", emptyNamespace())
f := NewOption("option", "option", "option").Nargs("0")
args := []string{}

Expand All @@ -162,7 +162,7 @@ func TestStoreFalse(t *testing.T) {

// TestStoreTrue tests the StoreFalse Action will store a true boolean.
func TestStoreTrue(t *testing.T) {
p := NewParser("parser")
p := NewParser("parser", emptyNamespace())
f := NewOption("option", "option", "option").Nargs("0")
args := []string{}

Expand All @@ -185,7 +185,7 @@ func TestStoreTrue(t *testing.T) {
// return the appropriate args & error when operating upon a option with
// one expected argument.
func TestAppend_OneNargs(t *testing.T) {
p := NewParser("parser")
p := NewParser("parser", emptyNamespace())
f := NewOption("option", "option", "option").Nargs("1")
args := []string{"foobar"}

Expand Down Expand Up @@ -214,7 +214,7 @@ func TestAppend_OneNargs(t *testing.T) {
// return the appropriate args & error when operating upon a option with
// three expected arguments.
func TestAppend_ThreeNargs(t *testing.T) {
p := NewParser("parser")
p := NewParser("parser", emptyNamespace())
f := NewOption("option", "option", "option").Nargs("3")
args := []string{"foo", "bar", "fizzbuzz"}

Expand Down Expand Up @@ -243,7 +243,7 @@ func TestAppend_ThreeNargs(t *testing.T) {
// return the appropriate args & error when operating upon a option with
// any number of expected arguments.
func TestAppend_AnyNargs(t *testing.T) {
p := NewParser("parser")
p := NewParser("parser", emptyNamespace())
f := NewOption("option", "option", "option").Nargs("*")
args := []string{"foo", "bar", "fizz", "buzz", "hello", "world"}

Expand Down Expand Up @@ -272,7 +272,7 @@ func TestAppend_AnyNargs(t *testing.T) {
// return the appropriate args & error when operating upon a option with
// at least one number of expected arguments.
func TestAppend_LeastOneNargs(t *testing.T) {
p := NewParser("parser")
p := NewParser("parser", emptyNamespace())
f := NewOption("option", "option", "option").Nargs("+")
args := []string{"foo", "fizz", "world"}

Expand Down
6 changes: 3 additions & 3 deletions errors.go
Original file line number Diff line number Diff line change
Expand Up @@ -104,14 +104,14 @@ func (err MissingOneOrMoreArgsErr) Error() string {

// MissingParserErr indicated that commands were available, but none were used.
type MissingParserErr struct {
Parsers map[string]*Parser
Parsers []SubParser
}

// Error will return a string error message for the MissingParserErr
func (err MissingParserErr) Error() string {
var names []string
for name, _ := range err.Parsers {
names = append(names, name)
for _, subP := range err.Parsers {
names = append(names, subP.Name)
}
msg := "must use an available command: %s"
return fmt.Sprintf(msg, join("", "{", join(",", names...), "}"))
Expand Down
2 changes: 1 addition & 1 deletion option.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import (
// to StoreTrue, and its default value to false.
func NewFlag(names, dest, help string) *Option {
opt := NewOption(names, dest, help)
opt.Nargs("0").Action(StoreTrue).Default("false")
opt.Nargs("0").Action(StoreTrue).Default("false").NotRequired()

return opt
}
Expand Down
Loading

0 comments on commit 1ee7ce1

Please sign in to comment.