Skip to content

Commit

Permalink
handle missing space in usage block
Browse files Browse the repository at this point in the history
  • Loading branch information
kbatten committed Sep 16, 2013
1 parent 13e13e6 commit 3b27938
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 5 deletions.
16 changes: 13 additions & 3 deletions docopt.go
Original file line number Diff line number Diff line change
Expand Up @@ -116,7 +116,13 @@ func parse(doc string, argv []string, help bool, version string, optionsFirst bo
usage := usageSections[0]

options := parseDefaults(doc)
pat, err := parsePattern(formalUsage(usage), &options)
formal, err := formalUsage(usage)
if err != nil {
output = handleError(err, usage)
return
}

pat, err := parsePattern(formal, &options)
if err != nil {
output = handleError(err, usage)
return
Expand Down Expand Up @@ -542,10 +548,14 @@ func tokenListFromPattern(source string) *tokenList {
return newTokenList(result, errorLanguage)
}

func formalUsage(section string) string {
func formalUsage(section string) (string, error) {
_, _, section = stringPartition(section, ":") // drop "usage:"
pu := strings.Fields(section)

if len(pu) == 0 {
return "", newLanguageError("no fields found in usage (perhaps a spacing error).")
}

result := "( "
for _, s := range pu[1:] {
if s == pu[0] {
Expand All @@ -556,7 +566,7 @@ func formalUsage(section string) string {
}
result += ")"

return result
return result, nil
}

func extras(help bool, version string, options patternList, doc string) string {
Expand Down
8 changes: 6 additions & 2 deletions docopt_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -151,9 +151,13 @@ func TestFormalUsage(t *testing.T) {
prog is a program`
usage := parseSection("usage:", doc)[0]
if usage != "Usage: prog [-hv] ARG\n prog N M" {
t.Error()
t.Fatal()
}
formal, err := formalUsage(usage)
if err != nil {
t.Fatal(err)
}
if formalUsage(usage) != "( [-hv] ARG ) | ( N M )" {
if formal != "( [-hv] ARG ) | ( N M )" {
t.Error()
}
return
Expand Down

0 comments on commit 3b27938

Please sign in to comment.