Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Newer
Older
100644 99 lines (80 sloc) 2.308 kb
16f1cd13 »
2012-09-03 Simplified API
1 package flags
2
3 import (
4 "fmt"
5 "unicode/utf8"
6 )
7
8 func (p *Parser) removeGroup(group *Group) {
9 for i, grp := range p.Groups {
10 if grp == group {
11 p.Groups = append(p.Groups[:i], p.Groups[i+1:]...)
12 break
13 }
14 }
15 }
16
b3e81244 »
2012-09-03 Renamed Info to Option
17 func (p *Parser) parseOption(group *Group, args []string, name string, option *Option, canarg bool, argument *string, index int) (error, int) {
16f1cd13 »
2012-09-03 Simplified API
18 var err error
19
b3e81244 »
2012-09-03 Renamed Info to Option
20 if !option.canArgument() {
16f1cd13 »
2012-09-03 Simplified API
21 if canarg && argument != nil {
22 return newError(ErrNoArgumentForBool,
5f56c1f2 »
2012-09-03 Run gofmt
23 fmt.Sprintf("bool flag `%s' cannot have an argument", option)),
24 index
16f1cd13 »
2012-09-03 Simplified API
25 }
26
b3e81244 »
2012-09-03 Renamed Info to Option
27 err = option.Set(nil)
16f1cd13 »
2012-09-03 Simplified API
28 } else if canarg && (argument != nil || index < len(args)) {
29 if argument == nil {
30 argument = &args[index]
31 index++
32 }
33
b3e81244 »
2012-09-03 Renamed Info to Option
34 err = option.Set(argument)
35 } else if option.OptionalArgument {
36 err = option.Set(&option.Default)
16f1cd13 »
2012-09-03 Simplified API
37 } else {
38 return newError(ErrExpectedArgument,
5f56c1f2 »
2012-09-03 Run gofmt
39 fmt.Sprintf("expected argument for flag `%s'", option)),
16f1cd13 »
2012-09-03 Simplified API
40 index
41 }
42
06d68c01 »
2012-09-03 Slightly nicer error message for failed argument conversion
43 if err != nil {
44 if _, ok := err.(*Error); !ok {
45 err = newError(ErrMarshal,
5f56c1f2 »
2012-09-03 Run gofmt
46 fmt.Sprintf("invalid argument for flag `%s' (expected %s)",
47 option,
48 option.value.Type()))
06d68c01 »
2012-09-03 Slightly nicer error message for failed argument conversion
49 }
16f1cd13 »
2012-09-03 Simplified API
50 }
51
52 return err, index
53 }
54
55 func (p *Parser) parseLong(args []string, name string, argument *string, index int) (error, int) {
56 for _, grp := range p.Groups {
b3e81244 »
2012-09-03 Renamed Info to Option
57 if option := grp.LongNames[name]; option != nil {
58 return p.parseOption(grp, args, name, option, true, argument, index)
16f1cd13 »
2012-09-03 Simplified API
59 }
60 }
61
62 return newError(ErrUnknownFlag,
5f56c1f2 »
2012-09-03 Run gofmt
63 fmt.Sprintf("unknown flag `%s'", name)),
16f1cd13 »
2012-09-03 Simplified API
64 index
65 }
66
b3e81244 »
2012-09-03 Renamed Info to Option
67 func (p *Parser) getShort(name rune) (*Option, *Group) {
16f1cd13 »
2012-09-03 Simplified API
68 for _, grp := range p.Groups {
b3e81244 »
2012-09-03 Renamed Info to Option
69 option := grp.ShortNames[name]
16f1cd13 »
2012-09-03 Simplified API
70
b3e81244 »
2012-09-03 Renamed Info to Option
71 if option != nil {
72 return option, grp
16f1cd13 »
2012-09-03 Simplified API
73 }
74 }
75
76 return nil, nil
77 }
78
79 func (p *Parser) parseShort(args []string, name rune, islast bool, argument *string, index int) (error, int) {
80 names := make([]byte, utf8.RuneLen(name))
81 utf8.EncodeRune(names, name)
82
b3e81244 »
2012-09-03 Renamed Info to Option
83 option, grp := p.getShort(name)
16f1cd13 »
2012-09-03 Simplified API
84
b3e81244 »
2012-09-03 Renamed Info to Option
85 if option != nil {
86 if option.canArgument() && !islast && !option.OptionalArgument {
16f1cd13 »
2012-09-03 Simplified API
87 return newError(ErrExpectedArgument,
5f56c1f2 »
2012-09-03 Run gofmt
88 fmt.Sprintf("expected argument for flag `%s'", option)),
89 index
16f1cd13 »
2012-09-03 Simplified API
90 }
91
b3e81244 »
2012-09-03 Renamed Info to Option
92 return p.parseOption(grp, args, string(names), option, islast, argument, index)
16f1cd13 »
2012-09-03 Simplified API
93 }
94
95 return newError(ErrUnknownFlag,
5f56c1f2 »
2012-09-03 Run gofmt
96 fmt.Sprintf("unknown flag `%s'", string(names))),
16f1cd13 »
2012-09-03 Simplified API
97 index
98 }
Something went wrong with that request. Please try again.