| 1 | // Copyright 2012 Jesse van den Kieboom. All rights reserved. |
| 2 | // Use of this source code is governed by a BSD-style |
| 3 | // license that can be found in the LICENSE file. |
| 4 | |
| 5 | package flags |
| 6 | |
| 7 | import ( |
| 8 | "errors" |
| 9 | ) |
| 10 | |
| 11 | // The provided container is not a pointer to a struct |
| 12 | var ErrNotPointerToStruct = errors.New("provided data is not a pointer to struct") |
| 13 | |
| 14 | // The provided short name is longer than a single character |
| 15 | var ErrShortNameTooLong = errors.New("short names can only be 1 character") |
| 16 | |
| 17 | // An option group. The option group has a name and a set of options. |
| 18 | type Group struct { |
| 19 | Commander |
| 20 | |
| 21 | // The name of the group. |
| 22 | Name string |
| 23 | |
| 24 | Names map[string]*Option |
| 25 | IniNames map[string]*Option |
| 26 | |
| 27 | // A map of long names to option option descriptions. |
| 28 | LongNames map[string]*Option |
| 29 | |
| 30 | // A map of short names to option option descriptions. |
| 31 | ShortNames map[rune]*Option |
| 32 | |
| 33 | // A list of all the options in the group. |
| 34 | Options []*Option |
| 35 | |
| 36 | // An error which occurred when creating the group. |
| 37 | Error error |
| 38 | |
| 39 | // Groups embedded in this group |
| 40 | EmbeddedGroups []*Group |
| 41 | |
| 42 | IsCommand bool |
| 43 | LongDescription string |
| 44 | |
| 45 | data interface{} |
| 46 | } |
| 47 | |
| 48 | // The command interface should be implemented by any command added in the |
| 49 | // options. When implemented, the Execute method will be called for the last |
| 50 | // specified (sub)command providing the remaining command line arguments. |
| 51 | type Command interface { |
| 52 | // Execute will be called for the last active (sub)command. The |
| 53 | // args argument contains the remaining command line arguments. The |
| 54 | // error that Execute returns will be eventually passed out of the |
| 55 | // Parse method of the Parser. |
| 56 | Execute(args []string) error |
| 57 | } |
| 58 | |
| 59 | type Usage interface { |
| 60 | Usage() string |
| 61 | } |
| 62 | |
| 63 | // NewGroup creates a new option group with a given name and underlying data |
| 64 | // container. The data container is a pointer to a struct. The fields of the |
| 65 | // struct represent the command line options (using field tags) and their values |
| 66 | // will be set when their corresponding options appear in the command line |
| 67 | // arguments. |
| 68 | func NewGroup(name string, data interface{}) *Group { |
| 69 | ret := &Group{ |
| 70 | Commander: Commander{ |
| 71 | Commands: make(map[string]*Group), |
| 72 | }, |
| 73 | |
| 74 | Name: name, |
| 75 | Names: make(map[string]*Option), |
| 76 | IniNames: make(map[string]*Option), |
| 77 | LongNames: make(map[string]*Option), |
| 78 | ShortNames: make(map[rune]*Option), |
| 79 | IsCommand: false, |
| 80 | data: data, |
| 81 | } |
| 82 | |
| 83 | ret.Error = ret.scan() |
| 84 | return ret |
| 85 | } |