/usr/share/gocode/src/github.com/jessevdk/go-flags/parser.go is in golang-go-flags-dev 0.0~git20131216-1.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 | // Copyright 2012 Jesse van den Kieboom. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
package flags
import (
"os"
"path"
)
// A Parser provides command line option parsing. It can contain several
// option groups each with their own set of options.
type Parser struct {
*Command
Usage string
Options Options
internalError error
}
// Parser options
type Options uint
const (
// No options
None Options = 0
// Add a default Help Options group to the parser containing -h and
// --help options. When either -h or --help is specified on the command
// line, a pretty formatted help message will be printed to os.Stderr.
// The parser will return ErrHelp.
HelpFlag = 1 << iota
// Pass all arguments after a double dash, --, as remaining command line
// arguments (i.e. they will not be parsed for flags)
PassDoubleDash
// Ignore any unknown options and pass them as remaining command line
// arguments
IgnoreUnknown
// Print any errors which occured during parsing to os.Stderr
PrintErrors
// Pass all arguments after the first non option. This is equivalent
// to strict POSIX processing
PassAfterNonOption
// A convenient default set of options
Default = HelpFlag | PrintErrors | PassDoubleDash
)
// Parse is a convenience function to parse command line options with default
// settings. The provided data is a pointer to a struct representing the
// default option group (named "Application Options"). For more control, use
// flags.NewParser.
func Parse(data interface{}) ([]string, error) {
return NewParser(data, Default).Parse()
}
// ParseArgs is a convenience function to parse command line options with default
// settings. The provided data is a pointer to a struct representing the
// default option group (named "Application Options"). The args argument is
// the list of command line arguments to parse. If you just want to parse the
// default program command line arguments (i.e. os.Args), then use flags.Parse
// instead. For more control, use flags.NewParser.
func ParseArgs(data interface{}, args []string) ([]string, error) {
return NewParser(data, Default).ParseArgs(args)
}
// NewParser creates a new parser. It uses os.Args[0] as the application
// name and then calls Parser.NewNamedParser (see Parser.NewNamedParser for
// more details). The provided data is a pointer to a struct representing the
// default option group (named "Application Options"), or nil if the default
// group should not be added. The options parameter specifies a set of options
// for the parser.
func NewParser(data interface{}, options Options) *Parser {
ret := NewNamedParser(path.Base(os.Args[0]), options)
if data != nil {
_, ret.internalError = ret.AddGroup("Application Options", "", data)
}
return ret
}
// NewNamedParser creates a new parser. The appname is used to display the
// executable name in the builtin help message. Option groups and commands can
// be added to this parser by using AddGroup and AddCommand.
func NewNamedParser(appname string, options Options) *Parser {
return &Parser{
Command: newCommand(appname, "", "", nil),
Options: options,
}
}
// Parse parses the command line arguments from os.Args using Parser.ParseArgs.
// For more detailed information see ParseArgs.
func (p *Parser) Parse() ([]string, error) {
return p.ParseArgs(os.Args[1:])
}
// ParseArgs parses the command line arguments according to the option groups that
// were added to the parser. On successful parsing of the arguments, the
// remaining, non-option, arguments (if any) are returned. The returned error
// indicates a parsing error and can be used with PrintError to display
// contextual information on where the error occurred exactly.
//
// When the common help group has been added (AddHelp) and either -h or --help
// was specified in the command line arguments, a help message will be
// automatically printed. Furthermore, the special error type ErrHelp is returned.
// It is up to the caller to exit the program if so desired.
func (p *Parser) ParseArgs(args []string) ([]string, error) {
if p.internalError != nil {
return nil, p.internalError
}
p.eachCommand(func(c *Command) {
p.eachGroup(func(g *Group) {
g.storeDefaults()
})
}, true)
// Add builtin help group to all commands if necessary
if (p.Options & HelpFlag) != None {
p.addHelpGroups(p.showBuiltinHelp)
}
s := &parseState{
args: args,
retargs: make([]string, 0, len(args)),
command: p.Command,
lookup: p.makeLookup(),
}
for !s.eof() {
arg := s.pop()
// When PassDoubleDash is set and we encounter a --, then
// simply append all the rest as arguments and break out
if (p.Options&PassDoubleDash) != None && arg == "--" {
s.retargs = append(s.retargs, s.args...)
break
}
if !argumentIsOption(arg) {
// Note: this also sets s.err, so we can just check for
// nil here and use s.err later
if p.parseNonOption(s) != nil {
break
}
continue
}
var err error
var option *Option
prefix, optname, islong := stripOptionPrefix(arg)
optname, argument := splitOption(prefix, optname, islong)
if islong {
option, err = p.parseLong(s, optname, argument)
} else {
option, err = p.parseShort(s, optname, argument)
}
if err != nil {
ignoreUnknown := (p.Options & IgnoreUnknown) != None
parseErr := wrapError(err)
if !(parseErr.Type == ErrUnknownFlag && ignoreUnknown) {
s.err = parseErr
break
}
if ignoreUnknown {
s.retargs = append(s.retargs, arg)
}
} else {
delete(s.lookup.required, option)
}
}
if s.err == nil {
s.checkRequired()
}
if s.err != nil {
return nil, p.printError(s.err)
}
if len(s.command.commands) != 0 {
return nil, p.printError(s.estimateCommand())
} else if cmd, ok := s.command.data.(Commander); ok {
return nil, p.printError(cmd.Execute(s.retargs))
}
return s.retargs, nil
}
|