Source File
flag.go
Belonging Package
flag
package flag
import (
)
type boolValue bool
func ( bool, *bool) *boolValue {
* =
return (*boolValue)()
}
func ( *boolValue) ( string) error {
, := strconv.ParseBool()
if != nil {
= errParse
}
* = boolValue()
return
}
func ( *boolValue) () interface{} { return bool(*) }
func ( *boolValue) () string { return strconv.FormatBool(bool(*)) }
func ( *boolValue) () bool { return true }
type intValue int
func ( int, *int) *intValue {
* =
return (*intValue)()
}
func ( *intValue) ( string) error {
, := strconv.ParseInt(, 0, strconv.IntSize)
if != nil {
= numError()
}
* = intValue()
return
}
func ( *intValue) () interface{} { return int(*) }
func ( *intValue) () string { return strconv.Itoa(int(*)) }
type int64Value int64
func ( int64, *int64) *int64Value {
* =
return (*int64Value)()
}
func ( *int64Value) ( string) error {
, := strconv.ParseInt(, 0, 64)
if != nil {
= numError()
}
* = int64Value()
return
}
func ( *int64Value) () interface{} { return int64(*) }
func ( *int64Value) () string { return strconv.FormatInt(int64(*), 10) }
type uintValue uint
func ( uint, *uint) *uintValue {
* =
return (*uintValue)()
}
func ( *uintValue) ( string) error {
, := strconv.ParseUint(, 0, strconv.IntSize)
if != nil {
= numError()
}
* = uintValue()
return
}
func ( *uintValue) () interface{} { return uint(*) }
func ( *uintValue) () string { return strconv.FormatUint(uint64(*), 10) }
type uint64Value uint64
func ( uint64, *uint64) *uint64Value {
* =
return (*uint64Value)()
}
func ( *uint64Value) ( string) error {
, := strconv.ParseUint(, 0, 64)
if != nil {
= numError()
}
* = uint64Value()
return
}
func ( *uint64Value) () interface{} { return uint64(*) }
func ( *uint64Value) () string { return strconv.FormatUint(uint64(*), 10) }
type stringValue string
func ( string, *string) *stringValue {
* =
return (*stringValue)()
}
func ( *stringValue) ( string) error {
* = stringValue()
return nil
}
func ( *stringValue) () interface{} { return string(*) }
func ( *stringValue) () string { return string(*) }
type float64Value float64
func ( float64, *float64) *float64Value {
* =
return (*float64Value)()
}
func ( *float64Value) ( string) error {
, := strconv.ParseFloat(, 64)
if != nil {
= numError()
}
* = float64Value()
return
}
func ( *float64Value) () interface{} { return float64(*) }
func ( *float64Value) () string { return strconv.FormatFloat(float64(*), 'g', -1, 64) }
type durationValue time.Duration
func ( time.Duration, *time.Duration) *durationValue {
* =
return (*durationValue)()
}
func ( *durationValue) ( string) error {
, := time.ParseDuration()
if != nil {
= errParse
}
* = durationValue()
return
}
func ( *durationValue) () interface{} { return time.Duration(*) }
func ( *durationValue) () string { return (*time.Duration)().String() }
type funcValue func(string) error
func ( funcValue) ( string) error { return () }
func ( funcValue) () string { return "" }
type ErrorHandling int
const (
ContinueOnError ErrorHandling = iota // Return a descriptive error.
ExitOnError // Call os.Exit(2) or for -h/-help Exit(0).
PanicOnError // Call panic with a descriptive error.
)
func ( *FlagSet) () ErrorHandling {
return .errorHandling
}
func ( func(*Flag)) {
CommandLine.VisitAll()
}
func ( func(*Flag)) {
CommandLine.Visit()
}
func ( string) *Flag {
return CommandLine.formal[]
}
func (, string) error {
return CommandLine.Set(, )
}
= "value"
switch .Value.(type) {
case boolFlag:
= ""
case *durationValue:
= "duration"
case *float64Value:
= "float"
case *intValue, *int64Value:
= "int"
case *stringValue:
= "string"
case *uintValue, *uint64Value:
= "uint"
}
return
}
if len() <= 4 { // space, space, '-', 'x'.
+= "\t"
+= "\n \t"
}
+= strings.ReplaceAll(, "\n", "\n \t")
if !isZeroValue(, .DefValue) {
func () {
CommandLine.PrintDefaults()
}
var Usage = func() {
fmt.Fprintf(CommandLine.Output(), "Usage of %s:\n", os.Args[0])
PrintDefaults()
}
func () int { return len(CommandLine.actual) }
func ( int) string {
return CommandLine.Arg()
}
func () int { return len(CommandLine.args) }
func () []string { return CommandLine.args }
func ( *bool, string, bool, string) {
CommandLine.Var(newBoolValue(, ), , )
}
func ( *int, string, int, string) {
CommandLine.Var(newIntValue(, ), , )
}
func ( *int64, string, int64, string) {
CommandLine.Var(newInt64Value(, ), , )
}
func ( *uint, string, uint, string) {
CommandLine.Var(newUintValue(, ), , )
}
func ( *uint64, string, uint64, string) {
CommandLine.Var(newUint64Value(, ), , )
}
func ( *string, string, string, string) {
CommandLine.Var(newStringValue(, ), , )
}
func ( *float64, string, float64, string) {
CommandLine.Var(newFloat64Value(, ), , )
}
func ( *time.Duration, string, time.Duration, string) {
CommandLine.Var(newDurationValue(, ), , )
}
func (, string, func(string) error) {
CommandLine.Func(, , )
}
:= &Flag{, , , .String()}
, := .formal[]
if {
var string
if .name == "" {
= fmt.Sprintf("flag redefined: %s", )
} else {
= fmt.Sprintf("%s flag redefined: %s", .name, )
}
fmt.Fprintln(.Output(), )
panic() // Happens only if flags are declared with identical names
}
if .formal == nil {
.formal = make(map[string]*Flag)
}
.formal[] =
}
func ( Value, string, string) {
CommandLine.Var(, , )
}
func ( *FlagSet) () {
if .Usage == nil {
.defaultUsage()
} else {
.Usage()
}
}
func ( *FlagSet) () (bool, error) {
if len(.args) == 0 {
return false, nil
}
:= .args[0]
if len() < 2 || [0] != '-' {
return false, nil
}
:= 1
if [1] == '-' {
++
if len() == 2 { // "--" terminates the flags
.args = .args[1:]
return false, nil
}
}
:= [:]
if len() == 0 || [0] == '-' || [0] == '=' {
return false, .failf("bad flag syntax: %s", )
}
.args = .args[1:]
:= false
:= ""
for := 1; < len(); ++ { // equals cannot be first
if [] == '=' {
= [+1:]
= true
= [0:]
break
}
}
:= .formal
, := [] // BUG
if ! {
if == "help" || == "h" { // special case for nice help message.
.usage()
return false, ErrHelp
}
return false, .failf("flag provided but not defined: -%s", )
}
if , := .Value.(boolFlag); && .IsBoolFlag() { // special case: doesn't need an arg
if {
if := .Set(); != nil {
return false, .failf("invalid boolean value %q for -%s: %v", , , )
}
} else {
if := .Set("true"); != nil {
return false, .failf("invalid boolean flag %s: %v", , )
}
}
func ( *FlagSet) ( []string) error {
.parsed = true
.args =
for {
, := .parseOne()
if {
continue
}
if == nil {
break
}
switch .errorHandling {
case ContinueOnError:
return
case ExitOnError:
if == ErrHelp {
os.Exit(0)
}
os.Exit(2)
case PanicOnError:
panic()
}
}
return nil
}
CommandLine.Parse(os.Args[1:])
}
func () bool {
return CommandLine.Parsed()
}
var CommandLine = NewFlagSet(os.Args[0], ExitOnError)
CommandLine.Usage = commandLineUsage
}
func () {
Usage()
}
func ( string, ErrorHandling) *FlagSet {
:= &FlagSet{
name: ,
errorHandling: ,
}
.Usage = .defaultUsage
return
}
func ( *FlagSet) ( string, ErrorHandling) {
.name =
.errorHandling =
![]() |
The pages are generated with Golds v0.3.2-preview. (GOOS=darwin GOARCH=amd64) Golds is a Go 101 project developed by Tapir Liu. PR and bug reports are welcome and can be submitted to the issue list. Please follow @Go100and1 (reachable from the left QR code) to get the latest news of Golds. |