| 114 | } |
| 115 | |
| 116 | func parseArgparseUsage(lexer *usageLexer) (usage argparseUsage, err error) { |
| 117 | // First token should be application name |
| 118 | if !lexer.Next() { |
| 119 | err = lexer.Err() |
| 120 | if err == nil { |
| 121 | err = fmt.Errorf("bad usage: cannot find application name") |
| 122 | } |
| 123 | return |
| 124 | } |
| 125 | usage.applicationName = lexer.Token() |
| 126 | |
| 127 | // Then should go tokens of sub-command up to first '[' |
| 128 | lexer.Next() |
| 129 | usage.flagContext.Framework = "argparse" |
| 130 | for { |
| 131 | if !lexer.Valid() { |
| 132 | err = lexer.Err() |
| 133 | if err == nil { |
| 134 | err = fmt.Errorf("cannot find [-h] in usage") |
| 135 | } |
| 136 | return |
| 137 | } |
| 138 | if lexer.TokenIsSyntax() { |
| 139 | break |
| 140 | } |
| 141 | usage.flagContext.SubCommand = append(usage.flagContext.SubCommand, lexer.Token()) |
| 142 | lexer.Next() |
| 143 | } |
| 144 | |
| 145 | // now a bunch of groups should follow |
| 146 | parseOptionalGroup := func() error { |
| 147 | lexer.Next() |
| 148 | loop: |
| 149 | for { |
| 150 | if !lexer.Valid() { |
| 151 | err = lexer.Err() |
| 152 | if err == nil { |
| 153 | err = fmt.Errorf("unexpected end of usage while parsing optional group") |
| 154 | } |
| 155 | return err |
| 156 | } |
| 157 | switch lexer.Token() { |
| 158 | case "[": |
| 159 | return fmt.Errorf("optional group cannot be nested") |
| 160 | case "]": |
| 161 | break loop |
| 162 | } |
| 163 | lexer.Next() |
| 164 | } |
| 165 | lexer.Next() |
| 166 | return nil |
| 167 | } |
| 168 | parseChoiceGroup := func() error { |
| 169 | lexer.Next() |
| 170 | loop: |
| 171 | for { |
| 172 | if !lexer.Valid() { |
| 173 | err = lexer.Err() |