Skip to content

Add support for positional and repeated arguments - #7

Merged
eliothedeman merged 4 commits into
masterfrom
claude/add-positional-repeated-args-01AM62XVd4zLdyJQwBeKHWR4
Nov 29, 2025
Merged

Add support for positional and repeated arguments#7
eliothedeman merged 4 commits into
masterfrom
claude/add-positional-repeated-args-01AM62XVd4zLdyJQwBeKHWR4

Conversation

@eliothedeman

Copy link
Copy Markdown
Owner

Implemented two new struct tags:

  • positional: Marks a field as a positional argument instead of a flag
  • repeated: Marks a field (must be a slice) to accept multiple values

Features:

  • Positional arguments are parsed from command-line args in order
  • Repeated positional args (slices with both tags) consume all remaining args
  • Repeated flags (slices with repeated tag) can be specified multiple times
  • Support for all basic types: string, int/uint variants, float32/64, bool
  • Automatic type parsing and validation with clear error messages
  • Full test coverage for both positional and repeated arguments

Implemented two new struct tags:
- `positional`: Marks a field as a positional argument instead of a flag
- `repeated`: Marks a field (must be a slice) to accept multiple values

Features:
- Positional arguments are parsed from command-line args in order
- Repeated positional args (slices with both tags) consume all remaining args
- Repeated flags (slices with repeated tag) can be specified multiple times
- Support for all basic types: string, int/uint variants, float32/64, bool
- Automatic type parsing and validation with clear error messages
- Full test coverage for both positional and repeated arguments
Updated the positional argument implementation to use explicit position
numbers (e.g., position:"1", position:"2") instead of relying on field
order. This provides more clarity and flexibility.

Changes:
- Renamed tag from 'positional' to 'position'
- Changed option.Positional from bool to option.Position as int
- Parse position value from tag (must be positive integer)
- Sort positional options by their position number
- Added validation for invalid position values
- Added test for out-of-order field definitions with correct positions

This allows fields to be defined in any order in the struct while
maintaining correct positional argument parsing based on the position
tag value.
Removed the need for the `repeated:""` tag - slice types are now
automatically treated as repeated/variadic arguments.

Changes:
- Slices are detected by checking reflect.Kind() == reflect.Slice
- Works for both flags and positional arguments
- Removed `repeated:""` tag from tests (no longer needed)
- Simplified the API - just use a slice type to get repeated behavior

Examples:
```go
// Repeated flag (old way with tag)
Files []string `repeated:""`

// Repeated flag (new way - automatic)
Files []string

// Repeated positional (old way)
Files []string `position:"1" repeated:""`

// Repeated positional (new way)
Files []string `position:"1"`
```
Changed the tag name from 'position' to 'arg' to better distinguish
between positional arguments and named options/flags.

Terminology:
- Named options: --file, -f (flags with names)
- Positional args: arg:"1", arg:"2" (arguments by position)

Changes:
- Renamed positionTag to argTag
- Renamed option.Position to option.Arg
- Updated all references in code and tests
- Updated comments to reflect new terminology

Usage:
```go
type CopyCmd struct {
    Source string `arg:"1"`  // First positional argument
    Target string `arg:"2"`  // Second positional argument
}
```
@eliothedeman
eliothedeman merged commit 7f42d46 into master Nov 29, 2025
1 check passed
@eliothedeman
eliothedeman deleted the claude/add-positional-repeated-args-01AM62XVd4zLdyJQwBeKHWR4 branch November 29, 2025 23:11
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants