Skip to content

feat: add RegisterIntEnum for integer-based enum flags#116

Merged
leodido merged 3 commits into
mainfrom
ld/register-int-enum
Apr 19, 2026
Merged

feat: add RegisterIntEnum for integer-based enum flags#116
leodido merged 3 commits into
mainfrom
ld/register-int-enum

Conversation

@leodido
Copy link
Copy Markdown
Owner

@leodido leodido commented Apr 19, 2026

Description

RegisterIntEnum[E ~int | ~int8 | ~int16 | ~int32 | ~int64] registers an integer-based enum type for automatic flag handling via enumflag/v2. Mirrors RegisterEnum[E ~string] (#115) but targets signed integer enum types.

After registration, struct fields of type E work without flagcustom:"true" or manual Define/Decode/Complete hook methods.

Unsigned integer types are not supported (documented in the doc comment).

Usage

type Priority int

const (
    PriorityLow    Priority = 0
    PriorityMedium Priority = 1
    PriorityHigh   Priority = 2
)

func init() {
    structcli.RegisterIntEnum[Priority](map[Priority][]string{
        PriorityLow:    {"low"},
        PriorityMedium: {"medium", "med"},
        PriorityHigh:   {"high"},
    })
}

type Options struct {
    Priority Priority `flag:"priority" flagdescr:"task priority" default:"low"`
}

Features

  • Case-insensitive matching and alias support (via enumflag/v2)
  • Automatic {val1,val2,...} description enhancement (sorted by integer value)
  • Automatic shell completion for all EnumValuer flags
  • JSON Schema enum annotation
  • Works with flagenv:"true", flagenv:"only", flagrequired:"true", default:"..."
  • Panics on duplicate registration, empty values map, or alias collisions

Commits

  1. feat(internal/hooks): DefineIntEnumHookFunc and StringToIntEnumHookFunc — define/decode hook factories for integer enums using enumflag/v2. Widens enumHelpText constraint to cover all integer widths.
  2. feat: Public RegisterIntEnum + integration tests.
  3. fix: Address review findings — fix broken doc comments (2.1), add alias collision panic to StringToIntEnumHookFunc (2.2), add env-var/env-only tests (2.3), validate empty values (2.7), document ordering/unsigned exclusion (3.1/3.3), fix enumHelpText int64 truncation on 32-bit (3.2).

Stacked on #115 — review that PR first.

How to test

go test -run "TestRegisterIntEnum|TestDefineIntEnum|TestStringToIntEnum" -v . ./internal/hooks/...
go test ./...
@leodido leodido added the enhancement New feature or request label Apr 19, 2026
@leodido leodido force-pushed the ld/register-int-enum branch from 2b45f54 to e26e099 Compare April 19, 2026 18:18
@leodido leodido marked this pull request as ready for review April 19, 2026 19:11
@leodido leodido changed the title feat: add RegisterIntEnum for integer-based enum flags feat: add RegisterIntEnum for integer-based enum flags Apr 19, 2026
@leodido leodido self-assigned this Apr 19, 2026
@leodido leodido force-pushed the ld/register-int-enum branch from e26e099 to f90cb75 Compare April 19, 2026 20:15
@leodido leodido changed the base branch from ld/register-enum to main April 19, 2026 20:35
leodido and others added 3 commits April 19, 2026 20:35
Add DefineIntEnumHookFunc and StringToIntEnumHookFunc for integer-based
enum types using enumflag/v2. Widen enumHelpText constraint to cover
all integer widths (~int through ~int64).

Include unit tests for both hook factories.

Co-authored-by: Ona <no-reply@ona.com>
RegisterIntEnum[E] eliminates boilerplate for integer enum types by
auto-registering define, decode, and completion hooks via enumflag/v2.

Mirrors RegisterEnum[E ~string] but targets ~int through ~int64.

Include integration tests covering define+unmarshal, aliases,
case-insensitivity, validation, defaults, auto-completion, JSON schema,
duplicate detection, and description enhancement.

Co-authored-by: Ona <no-reply@ona.com>
- Fix broken doc comments: reorder RegisterEnum before RegisterIntEnum
  so each has its own doc block (2.1)
- Add alias collision panic to StringToIntEnumHookFunc (2.2)
- Add env-var and env-only integration tests for RegisterIntEnum (2.3)
- Validate empty/nil values map in RegisterIntEnum (2.7)
- Document int enum ordering by integer value (3.1)
- Document unsigned type exclusion (3.3)
- Fix enumHelpText int truncation on 32-bit: use int64 (3.2)

Co-authored-by: Ona <no-reply@ona.com>
@leodido leodido force-pushed the ld/register-int-enum branch from f90cb75 to de06cbd Compare April 19, 2026 20:35
@leodido leodido merged commit 882f3e6 into main Apr 19, 2026
5 checks passed
@leodido leodido deleted the ld/register-int-enum branch April 19, 2026 20:38
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

enhancement New feature or request

1 participant