Add support for positional and repeated arguments - #7
Merged
eliothedeman merged 4 commits intoNov 29, 2025
Merged
Conversation
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
deleted the
claude/add-positional-repeated-args-01AM62XVd4zLdyJQwBeKHWR4
branch
November 29, 2025 23:11
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Implemented two new struct tags:
positional: Marks a field as a positional argument instead of a flagrepeated: Marks a field (must be a slice) to accept multiple valuesFeatures: