package command

import (
	
	
	
	
)

type Flag struct {
	Longhand    string
	Shorthand   string
	Description string

	NameAsShorthand bool
	Repeatable      bool
	Optarg          bool
	Value           bool
	Hidden          bool
	Required        bool
	Persistent      bool

	Nargs   int
	Default string
}

func ( Flag) () string {
	if .Longhand != "" {
		return .Longhand
	}
	return .Shorthand
}

func ( Flag) () string {
	var  string

	if .Shorthand != "" {
		 += "-" + .Shorthand
		if .Longhand != "" {
			 += ", "
		}
	}

	if .Longhand != "" {
		switch {
		case .NameAsShorthand:
			 += "-" + .Shorthand
		default:
			 += "--" + .Longhand
		}
	}

	switch {
	case .Optarg:
		 += "?"
	case .Value:
		 += "="
	}

	if .Repeatable {
		 += "*"
	}

	if .Required {
		 += "!"
	}

	if .Hidden {
		 += "&"
	}

	return 
}

func parseFlag(,  string) (*Flag, error) {
	 := regexp.MustCompile(`^(?P<shorthand>-[^-][^ =*?&!]*)?(, )?(?P<longhand>-[-]?[^ =*?&!]*)?(?P<modifier>[=*?&!]*)$`)
	if !.MatchString() {
		return nil, fmt.Errorf("flag syntax invalid: %v", )
	}

	 := findNamedMatches(, )

	 := &Flag{}
	.Longhand = strings.TrimLeft(["longhand"], "-")
	.Shorthand = strings.TrimPrefix(["shorthand"], "-")
	.NameAsShorthand = (["longhand"] != "" && !strings.HasPrefix(["longhand"], "--"))
	.Description = 
	.Repeatable = strings.Contains(["modifier"], "*")
	.Optarg = strings.Contains(["modifier"], "?")
	.Value = .Optarg || strings.Contains(["modifier"], "=")
	.Hidden = strings.Contains(["modifier"], "&")
	.Required = strings.Contains(["modifier"], "!")
	if ["nargs"] != "" {
		var  error
		if .Nargs,  = strconv.Atoi(["nargs"]);  != nil {
			return nil, 
		}
	}

	if .Longhand == "" && .Shorthand == "" {
		return nil, fmt.Errorf("malformed flag: '%v'", )
	}
	return , nil
}

func findNamedMatches( *regexp.Regexp,  string) map[string]string {
	 := .FindStringSubmatch()

	 := map[string]string{}
	for ,  := range  {
		[.SubexpNames()[]] = 
	}
	return 
}