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 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353
|
// Package flaggy is a input flag parsing package that supports recursive
// subcommands, positional values, and any-position flags without
// unnecessary complexeties.
//
// For a getting started tutorial and full feature list, check out the
// readme at https://github.com/integrii/flaggy.
package flaggy // import "github.com/integrii/flaggy"
import (
"fmt"
"log"
"net"
"os"
"strconv"
"strings"
"time"
)
// strings used for builtin help and version flags both short and long
const versionFlagLongName = "version"
const helpFlagLongName = "help"
const helpFlagShortName = "h"
// defaultVersion is applied to parsers when they are created
const defaultVersion = "0.0.0"
// DebugMode indicates that debug output should be enabled
var DebugMode bool
// DefaultHelpTemplate is the help template that will be used
// for newly created subcommands and commands
var DefaultHelpTemplate = defaultHelpTemplate
// DefaultParser is the default parser that is used with the package-level public
// functions
var DefaultParser *Parser
// TrailingArguments holds trailing arguments in the main parser after parsing
// has been run.
var TrailingArguments []string
func init() {
// set the default help template
// allow usage like flaggy.StringVar by enabling a default Parser
ResetParser()
}
// ResetParser resets the default parser to a fresh instance. Uses the
// name of the binary executing as the program name by default.
func ResetParser() {
if len(os.Args) > 0 {
chunks := strings.Split(os.Args[0], "/")
DefaultParser = NewParser(chunks[len(chunks)-1])
} else {
DefaultParser = NewParser("default")
}
}
// Parse parses flags as requested in the default package parser
func Parse() {
err := DefaultParser.Parse()
TrailingArguments = DefaultParser.TrailingArguments
if err != nil {
log.Panicln("Error from argument parser:", err)
}
}
// ParseArgs parses the passed args as if they were the arguments to the
// running binary. Targets the default main parser for the package.
func ParseArgs(args []string) {
err := DefaultParser.ParseArgs(args)
TrailingArguments = DefaultParser.TrailingArguments
if err != nil {
log.Panicln("Error from argument parser:", err)
}
}
// String adds a new string flag
func String(assignmentVar *string, shortName string, longName string, description string) {
DefaultParser.add(assignmentVar, shortName, longName, description)
}
// StringSlice adds a new slice of strings flag
// Specify the flag multiple times to fill the slice
func StringSlice(assignmentVar *[]string, shortName string, longName string, description string) {
DefaultParser.add(assignmentVar, shortName, longName, description)
}
// Bool adds a new bool flag
func Bool(assignmentVar *bool, shortName string, longName string, description string) {
DefaultParser.add(assignmentVar, shortName, longName, description)
}
// BoolSlice adds a new slice of bools flag
// Specify the flag multiple times to fill the slice
func BoolSlice(assignmentVar *[]bool, shortName string, longName string, description string) {
DefaultParser.add(assignmentVar, shortName, longName, description)
}
// ByteSlice adds a new slice of bytes flag
// Specify the flag multiple times to fill the slice. Takes hex as input.
func ByteSlice(assignmentVar *[]byte, shortName string, longName string, description string) {
DefaultParser.add(assignmentVar, shortName, longName, description)
}
// Duration adds a new time.Duration flag.
// Input format is described in time.ParseDuration().
// Example values: 1h, 1h50m, 32s
func Duration(assignmentVar *time.Duration, shortName string, longName string, description string) {
DefaultParser.add(assignmentVar, shortName, longName, description)
}
// DurationSlice adds a new time.Duration flag.
// Input format is described in time.ParseDuration().
// Example values: 1h, 1h50m, 32s
// Specify the flag multiple times to fill the slice.
func DurationSlice(assignmentVar *[]time.Duration, shortName string, longName string, description string) {
DefaultParser.add(assignmentVar, shortName, longName, description)
}
// Float32 adds a new float32 flag.
func Float32(assignmentVar *float32, shortName string, longName string, description string) {
DefaultParser.add(assignmentVar, shortName, longName, description)
}
// Float32Slice adds a new float32 flag.
// Specify the flag multiple times to fill the slice.
func Float32Slice(assignmentVar *[]float32, shortName string, longName string, description string) {
DefaultParser.add(assignmentVar, shortName, longName, description)
}
// Float64 adds a new float64 flag.
func Float64(assignmentVar *float64, shortName string, longName string, description string) {
DefaultParser.add(assignmentVar, shortName, longName, description)
}
// Float64Slice adds a new float64 flag.
// Specify the flag multiple times to fill the slice.
func Float64Slice(assignmentVar *[]float64, shortName string, longName string, description string) {
DefaultParser.add(assignmentVar, shortName, longName, description)
}
// Int adds a new int flag
func Int(assignmentVar *int, shortName string, longName string, description string) {
DefaultParser.add(assignmentVar, shortName, longName, description)
}
// IntSlice adds a new int slice flag.
// Specify the flag multiple times to fill the slice.
func IntSlice(assignmentVar *[]int, shortName string, longName string, description string) {
DefaultParser.add(assignmentVar, shortName, longName, description)
}
// UInt adds a new uint flag
func UInt(assignmentVar *uint, shortName string, longName string, description string) {
DefaultParser.add(assignmentVar, shortName, longName, description)
}
// UIntSlice adds a new uint slice flag.
// Specify the flag multiple times to fill the slice.
func UIntSlice(assignmentVar *[]uint, shortName string, longName string, description string) {
DefaultParser.add(assignmentVar, shortName, longName, description)
}
// UInt64 adds a new uint64 flag
func UInt64(assignmentVar *uint64, shortName string, longName string, description string) {
DefaultParser.add(assignmentVar, shortName, longName, description)
}
// UInt64Slice adds a new uint64 slice flag.
// Specify the flag multiple times to fill the slice.
func UInt64Slice(assignmentVar *[]uint64, shortName string, longName string, description string) {
DefaultParser.add(assignmentVar, shortName, longName, description)
}
// UInt32 adds a new uint32 flag
func UInt32(assignmentVar *uint32, shortName string, longName string, description string) {
DefaultParser.add(assignmentVar, shortName, longName, description)
}
// UInt32Slice adds a new uint32 slice flag.
// Specify the flag multiple times to fill the slice.
func UInt32Slice(assignmentVar *[]uint32, shortName string, longName string, description string) {
DefaultParser.add(assignmentVar, shortName, longName, description)
}
// UInt16 adds a new uint16 flag
func UInt16(assignmentVar *uint16, shortName string, longName string, description string) {
DefaultParser.add(assignmentVar, shortName, longName, description)
}
// UInt16Slice adds a new uint16 slice flag.
// Specify the flag multiple times to fill the slice.
func UInt16Slice(assignmentVar *[]uint16, shortName string, longName string, description string) {
DefaultParser.add(assignmentVar, shortName, longName, description)
}
// UInt8 adds a new uint8 flag
func UInt8(assignmentVar *uint8, shortName string, longName string, description string) {
DefaultParser.add(assignmentVar, shortName, longName, description)
}
// UInt8Slice adds a new uint8 slice flag.
// Specify the flag multiple times to fill the slice.
func UInt8Slice(assignmentVar *[]uint8, shortName string, longName string, description string) {
DefaultParser.add(assignmentVar, shortName, longName, description)
}
// Int64 adds a new int64 flag
func Int64(assignmentVar *int64, shortName string, longName string, description string) {
DefaultParser.add(assignmentVar, shortName, longName, description)
}
// Int64Slice adds a new int64 slice flag.
// Specify the flag multiple times to fill the slice.
func Int64Slice(assignmentVar *[]int64, shortName string, longName string, description string) {
DefaultParser.add(assignmentVar, shortName, longName, description)
}
// Int32 adds a new int32 flag
func Int32(assignmentVar *int32, shortName string, longName string, description string) {
DefaultParser.add(assignmentVar, shortName, longName, description)
}
// Int32Slice adds a new int32 slice flag.
// Specify the flag multiple times to fill the slice.
func Int32Slice(assignmentVar *[]int32, shortName string, longName string, description string) {
DefaultParser.add(assignmentVar, shortName, longName, description)
}
// Int16 adds a new int16 flag
func Int16(assignmentVar *int16, shortName string, longName string, description string) {
DefaultParser.add(assignmentVar, shortName, longName, description)
}
// Int16Slice adds a new int16 slice flag.
// Specify the flag multiple times to fill the slice.
func Int16Slice(assignmentVar *[]int16, shortName string, longName string, description string) {
DefaultParser.add(assignmentVar, shortName, longName, description)
}
// Int8 adds a new int8 flag
func Int8(assignmentVar *int8, shortName string, longName string, description string) {
DefaultParser.add(assignmentVar, shortName, longName, description)
}
// Int8Slice adds a new int8 slice flag.
// Specify the flag multiple times to fill the slice.
func Int8Slice(assignmentVar *[]int8, shortName string, longName string, description string) {
DefaultParser.add(assignmentVar, shortName, longName, description)
}
// IP adds a new net.IP flag.
func IP(assignmentVar *net.IP, shortName string, longName string, description string) {
DefaultParser.add(assignmentVar, shortName, longName, description)
}
// IPSlice adds a new int8 slice flag.
// Specify the flag multiple times to fill the slice.
func IPSlice(assignmentVar *[]net.IP, shortName string, longName string, description string) {
DefaultParser.add(assignmentVar, shortName, longName, description)
}
// HardwareAddr adds a new net.HardwareAddr flag.
func HardwareAddr(assignmentVar *net.HardwareAddr, shortName string, longName string, description string) {
DefaultParser.add(assignmentVar, shortName, longName, description)
}
// HardwareAddrSlice adds a new net.HardwareAddr slice flag.
// Specify the flag multiple times to fill the slice.
func HardwareAddrSlice(assignmentVar *[]net.HardwareAddr, shortName string, longName string, description string) {
DefaultParser.add(assignmentVar, shortName, longName, description)
}
// IPMask adds a new net.IPMask flag. IPv4 Only.
func IPMask(assignmentVar *net.IPMask, shortName string, longName string, description string) {
DefaultParser.add(assignmentVar, shortName, longName, description)
}
// IPMaskSlice adds a new net.HardwareAddr slice flag. IPv4 only.
// Specify the flag multiple times to fill the slice.
func IPMaskSlice(assignmentVar *[]net.IPMask, shortName string, longName string, description string) {
DefaultParser.add(assignmentVar, shortName, longName, description)
}
// AttachSubcommand adds a subcommand for parsing
func AttachSubcommand(subcommand *Subcommand, relativePosition int) {
DefaultParser.AttachSubcommand(subcommand, relativePosition)
}
// ShowHelp shows parser help
func ShowHelp(message string) {
DefaultParser.ShowHelpWithMessage(message)
}
// SetDescription sets the description of the default package command parser
func SetDescription(description string) {
DefaultParser.Description = description
}
// SetVersion sets the version of the default package command parser
func SetVersion(version string) {
DefaultParser.Version = version
}
// SetName sets the name of the default package command parser
func SetName(name string) {
DefaultParser.Name = name
}
// ShowHelpAndExit shows parser help and exits with status code 2
func ShowHelpAndExit(message string) {
ShowHelp(message)
exitOrPanic(2)
}
// PanicInsteadOfExit is used when running tests
var PanicInsteadOfExit bool
// exitOrPanic panics instead of calling os.Exit so that tests can catch
// more failures
func exitOrPanic(code int) {
if PanicInsteadOfExit {
panic("Panic instead of exit with code: " + strconv.Itoa(code))
}
os.Exit(code)
}
// ShowHelpOnUnexpectedEnable enables the ShowHelpOnUnexpected behavior on the
// default parser. This causes unknown inputs to error out.
func ShowHelpOnUnexpectedEnable() {
DefaultParser.ShowHelpOnUnexpected = true
}
// ShowHelpOnUnexpectedDisable disables the ShowHelpOnUnexpected behavior on the
// default parser. This causes unknown inputs to error out.
func ShowHelpOnUnexpectedDisable() {
DefaultParser.ShowHelpOnUnexpected = false
}
// AddPositionalValue adds a positional value to the main parser at the global
// context
func AddPositionalValue(assignmentVar *string, name string, relativePosition int, required bool, description string) {
DefaultParser.AddPositionalValue(assignmentVar, name, relativePosition, required, description)
}
// debugPrint prints if debugging is enabled
func debugPrint(i ...interface{}) {
if DebugMode {
fmt.Println(i...)
}
}
|