---
source: lib/src/go/mod.rs
expression: out
---
// Code generated by `usage generate go`. DO NOT EDIT.
//
// Binding tables for `ex`, read by
// [github.com/jdx/usage/go/argv]. Regenerate rather than editing: the spec is
// the definition, and a hand-edit here is a difference no reviewer can see.
//
// These are package-level variables holding plain data, so the linker lays them
// out and nothing runs before main.

package ex

import "github.com/jdx/usage/go/argv"

// Keys identify a table entry without a string comparison: switch on the Key an
// event carries rather than on its Name, which is there for diagnostics.
const (
	CmdRoot                 uint64 = 1
	CmdMacosDefaults        uint64 = 2
	FlagMacosDefaultsApply  uint64 = 3
	CmdMacos                uint64 = 4
	CmdMacosDefaults2       uint64 = 5
	FlagMacosDefaultsApply2 uint64 = 6
)

// Root is the command tree for `ex`. Pass it to argv.New.
var Root = &argv.Command{
	Name:        "ex",
	Key:         CmdRoot,
	Subcommands: []*argv.Command{cmdMacosDefaults, cmdMacos},
}

// macos-defaults
var cmdMacosDefaults = &argv.Command{
	Name: "macos-defaults",
	Key:  CmdMacosDefaults,
	Flags: []*argv.Flag{
		{Key: FlagMacosDefaultsApply, Name: "apply", Longs: []string{"apply"}},
	},
}

// macos
var cmdMacos = &argv.Command{
	Name:        "macos",
	Key:         CmdMacos,
	Subcommands: []*argv.Command{cmdMacosDefaults2},
}

// macos defaults
var cmdMacosDefaults2 = &argv.Command{
	Name: "defaults",
	Key:  CmdMacosDefaults2,
	Flags: []*argv.Flag{
		{Key: FlagMacosDefaultsApply2, Name: "apply", Longs: []string{"apply"}},
	},
}

// Meta is the cold table, read only by the rules that are decided once the
// last token has been read: required, choices, the env-then-default fallback,
// the var bounds, and the four that compare one entry against another. A parse
// never touches it.
//
// Indexed by key, so entry Key sits at Meta[Key-1]. A command's slot is empty:
// commands take keys too, and have no cold half.
var Meta = argv.Metadata{
	{},
	{},
	{Key: FlagMacosDefaultsApply, Name: "apply", Flag: true, RequiresIfBoolean: true, Spelling: "--apply"},
	{},
	{},
	{Key: FlagMacosDefaultsApply2, Name: "apply", Flag: true, RequiresIfBoolean: true, Spelling: "--apply"},
}

// HelpText is the third table, read only when a page is rendered. Neither the
// parser nor the post-binding rules touch it, and a CLI that never prints help
// does not carry it: Go's linker drops an unreferenced table whole.
//
// Indexed by key, like the others.
var HelpText = argv.HelpTable{
	{Key: CmdRoot},
	{Key: CmdMacosDefaults},
	{Key: FlagMacosDefaultsApply},
	{Key: CmdMacos},
	{Key: CmdMacosDefaults2},
	{Key: FlagMacosDefaultsApply2},
}

// HelpMeta is what a page needs from the spec's root rather than from any one
// command: the header, and the text that brackets every page.
var HelpMeta = argv.HelpSpec{Name: "ex", Bin: "ex"}

// Cli is the whole command line.
type Cli struct {
	MacosDefaults *MacosDefaultsCmd // CmdMacosDefaults
	Macos         *MacosCmd         // CmdMacos
}

// MacosDefaultsCmd is `macos-defaults`.
type MacosDefaultsCmd struct {
	Apply bool // FlagMacosDefaultsApply
}

// MacosCmd is `macos`.
type MacosCmd struct {
	Defaults *MacosDefaults2Cmd // CmdMacosDefaults2
}

// MacosDefaults2Cmd is `macos defaults`.
type MacosDefaults2Cmd struct {
	Apply bool // FlagMacosDefaultsApply2
}

// Parse binds a command line and fills the structs above.
//
// The rules decided once the last token has been read run here too, so a
// missing required flag or a value outside its choices comes back rather than
// reaching your code. A returned error is an *argv.Error; render it with
// argv.Render.
//
// Help and version arrive as errors, because a parse that stops to print a page
// has produced no value. Check the code before treating one as a failure.
func Parse(args []string) (*Cli, error) {
	out := &Cli{}
	var cmdMacosDefaultsV *MacosDefaultsCmd
	var cmdMacosV *MacosCmd
	var cmdMacosDefaults2V *MacosDefaults2Cmd

	// Collected by key, so the post-binding rules can judge what arrived
	// before any of it is handed back.
	given := map[uint64][]string{}
	seen := map[uint64]int{}
	chain := []*argv.Command{Root}

	p := argv.New(Root, args)
	for p.Next() {
		ev := p.Event()
		switch ev.Kind {
		case argv.KindCommand:
			chain = append(chain, ev.Command)
			switch ev.Command.Key {
			case CmdMacosDefaults:
				cmdMacosDefaultsV = &MacosDefaultsCmd{}
				out.MacosDefaults = cmdMacosDefaultsV
			case CmdMacos:
				cmdMacosV = &MacosCmd{}
				out.Macos = cmdMacosV
			case CmdMacosDefaults2:
				cmdMacosDefaults2V = &MacosDefaults2Cmd{}
				cmdMacosV.Defaults = cmdMacosDefaults2V
			}
		case argv.KindFlag:
			seen[ev.Flag.Key]++
			if ev.Flag.BoolValue {
				// Boolean binding is last-one-wins. Replace an earlier attached value even
				// when the last occurrence is bare, so relationship polarity follows the field.
				if ev.HasValue {
					given[ev.Flag.Key] = []string{ev.Value}
				} else {
					given[ev.Flag.Key] = []string{}
				}
			} else if ev.HasValue {
				given[ev.Flag.Key] = append(given[ev.Flag.Key], argv.SplitValue(ev.Value, ev.Flag.Delimiter, true)...)
			} else if given[ev.Flag.Key] == nil {
				// Given without a value is still given, and nil would read as
				// absent when the fallbacks are applied.
				given[ev.Flag.Key] = []string{}
			}
			switch ev.Flag.Key {
			case FlagMacosDefaultsApply:
				cmdMacosDefaultsV.Apply = !ev.Negated
			case FlagMacosDefaultsApply2:
				cmdMacosDefaults2V.Apply = !ev.Negated
			}
		case argv.KindArg:
			seen[ev.Arg.Key]++
			values := argv.SplitValue(ev.Value, ev.Arg.Delimiter, ev.Delimit)
			given[ev.Arg.Key] = append(given[ev.Arg.Key], values...)
			switch ev.Arg.Key {
			}
		}
	}
	if err := p.Err(); err != nil {
		return nil, err
	}
	if p.Command().ArgRequiredElseHelp && p.CommandStart() == len(args) {
		return nil, &argv.Error{Code: argv.CodeHelp, Cmd: p.Command()}
	}

	// Only the commands the words actually selected are judged: a required
	// flag on a command nobody ran is not missing.
	var scope []uint64
	requirements := map[uint64]bool{}
	for i, cmd := range chain {
		checkRequirements := i == len(chain)-1 || !cmd.SubcommandNegatesReqs
		for _, f := range cmd.Flags {
			scope = append(scope, f.Key)
			requirements[f.Key] = checkRequirements
		}
		for _, a := range cmd.Args {
			scope = append(scope, a.Key)
			requirements[a.Key] = checkRequirements
		}
	}
	sources := map[uint64]argv.Source{}
	filled := map[uint64][]string{}
	for _, key := range scope {
		values, source := argv.Fill(Meta.Lookup(key), given[key], argv.LookupEnv)
		filled[key] = values
		sources[key] = source
	}
	argv.ApplyDefaultIf(Meta, scope, filled, sources, nil)
	for _, key := range scope {
		values, source := filled[key], sources[key]
		entryMeta := Meta.Lookup(key)
		if entryMeta != nil && !requirements[key] {
			copy := *entryMeta
			copy.Required = false
			entryMeta = &copy
		}
		if err := argv.Check(entryMeta, values, seen[key]); err != nil {
			return nil, err
		}
		// What the environment or a default supplied has to reach the field
		// too. A front door that enforces a default and then hands back the
		// zero value is worse than one that has no defaults at all.
		//
		// Written here rather than in a function of its own because a
		// subcommand's struct is reachable only from inside this one: the
		// variable holding it is local, and only the keys of commands the
		// words selected are in scope, so it is never nil when its key is.
		if source == argv.FromEnv || source == argv.FromDefault {
			switch key {
			case FlagMacosDefaultsApply:
				if source == argv.FromEnv {
					cmdMacosDefaultsV.Apply = argv.EnvTruth(values[0])
				} else {
					cmdMacosDefaultsV.Apply = values[0] == "true"
				}
			case FlagMacosDefaultsApply2:
				if source == argv.FromEnv {
					cmdMacosDefaults2V.Apply = argv.EnvTruth(values[0])
				} else {
					cmdMacosDefaults2V.Apply = values[0] == "true"
				}
			}
		}
	}
	if err := argv.CheckRelationshipsWithValuesAndRequirements(Meta, scope, func(k uint64) argv.Source {
		return sources[k]
	}, nil, func(k uint64) bool { return requirements[k] }); err != nil {
		return nil, err
	}
	return out, nil
}
