Skip to content

Commit

Permalink
Fix error propagation from @ failures.
Browse files Browse the repository at this point in the history
Really fixes #185.
  • Loading branch information
alecthomas committed Dec 17, 2017
1 parent 7e2d128 commit 947dcec
Showing 1 changed file with 16 additions and 2 deletions.
18 changes: 16 additions & 2 deletions parser.go
Original file line number Diff line number Diff line change
Expand Up @@ -153,6 +153,10 @@ func (p *ParseContext) EOL() bool {
return p.Peek().Type == TokenEOL
}

func (p *ParseContext) Error() bool {
return p.Peek().Type == TokenError
}

// Next token in the parse context.
func (p *ParseContext) Next() *Token {
if len(p.peek) > 0 {
Expand Down Expand Up @@ -266,9 +270,12 @@ func (p *ParseContext) matchedCmd(cmd *CmdClause) {

// Expand arguments from a file. Lines starting with # will be treated as comments.
func ExpandArgsFromFile(filename string) (out []string, err error) {
if filename == "" {
return nil, fmt.Errorf("expected @ file to expand arguments from")
}
r, err := os.Open(filename)
if err != nil {
return nil, err
return nil, fmt.Errorf("failed to open arguments file %q: %s", filename, err)
}
defer r.Close()
scanner := bufio.NewScanner(r)
Expand All @@ -280,6 +287,9 @@ func ExpandArgsFromFile(filename string) (out []string, err error) {
out = append(out, line)
}
err = scanner.Err()
if err != nil {
return nil, fmt.Errorf("failed to read arguments from %q: %s", filename, err)
}
return
}

Expand All @@ -291,7 +301,7 @@ func parse(context *ParseContext, app *Application) (err error) {
ignoreDefault := context.ignoreDefault

loop:
for !context.EOL() {
for !context.EOL() && !context.Error() {
token := context.Peek()

switch token.Type {
Expand Down Expand Up @@ -365,6 +375,10 @@ loop:
}
}

if context.Error() {
return fmt.Errorf("%s", context.Peek().Value)
}

if !context.EOL() {
return fmt.Errorf("unexpected %s", context.Peek())
}
Expand Down

0 comments on commit 947dcec

Please sign in to comment.