feat: implement complete Modern API for Horde/Argv#8
Open
ralflang wants to merge 1 commit intoFRAMEWORK_6_0from
Open
feat: implement complete Modern API for Horde/Argv#8ralflang wants to merge 1 commit intoFRAMEWORK_6_0from
ralflang wants to merge 1 commit intoFRAMEWORK_6_0from
Conversation
Implement immutable help formatter for Modern API following Principle #5 and #7. Provides professional, terminal-width-aware help text formatting with full support for options, groups, and descriptions. Core Components: - HelpFormatter: Immutable help text generator - Integration with ImmutableParser via formatHelp() - Builder pattern for customization - Terminal width auto-detection Features: - Usage line formatting with %prog substitution - Description and epilog support - Option formatting with alignment and wrapping - Option groups with descriptions - Default value display (e.g., "(default: 8080)") - Choice list display (e.g., "(choices: 'json', 'xml', 'csv')") - Metavar support for argument placeholders - Short/long option ordering control - Text wrapping for terminal width - Configurable indentation Formatter Configuration: - width: Terminal width (0 = auto-detect, default: 80) - indent: Indentation increment (default: 2) - maxHelpPosition: Column before wrapping help text (default: 24) - shortFirst: Show short option first in listings (default: true) Formatting Rules: - Options aligned at configurable column position - Help text wraps to terminal width - Default values and choices automatically appended - Groups separated with headers - Consistent spacing and indentation Parser Integration: - formatHelp(): Generate help text for parser - Accepts optional custom formatter - Uses builder-provided formatter if available - Falls back to default formatter Example Usage: $formatter = HelpFormatter::create() ->withWidth(100) ->withIndent(4) ->build(); $parser = ParserBuilder::create() ->withUsage('%prog [options] <file>') ->withDescription('Process files') ->addOption($verboseOption) ->build(); echo $parser->formatHelp($formatter); Output Example: Usage: myapp [options] <file> Process files Options: -v, --verbose Increase verbosity -p, --port=PORT Server port (default: 8080) -f, --format=FORMAT Output format (choices: 'json', 'xml', 'csv') (default: json) Test Coverage: - 28 tests added (234 total Modern API tests) - 73 assertions added (403 total assertions) - All tests passing (100%) - Tests cover all formatting scenarios - Integration tests with ImmutableParser Formatter Characteristics: - Readonly class (Principle #7) - Immutable builder pattern (Principle #5) - No side effects, pure output generation - Thread-safe and cacheable - Fast formatting (no reflection) Text Wrapping Algorithm: - Word-based wrapping (no mid-word breaks) - Respects indentation for wrapped lines - Handles long option strings gracefully - Falls back to next-line help for long options Terminal Width Detection: - Auto-detects via tput cols if available - Falls back to 80 columns default - Can be explicitly set via withWidth() - Zero width disables wrapping Principles Demonstrated: - Principle #5: Modern API is Immutable - formatter fully immutable - Principle #7: Immutable Throughout - readonly class, builder for config - Principle #6: Explicit Access - clear method calls, no magic - Clean separation between formatting logic and parser logic Next Steps: Modern API is now feature-complete with parsing and help formatting
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.
Summary
Complete implementation of Modern API for Horde/Argv following 8 guiding principles established through design analysis. This provides a fully immutable, type-safe, and explicit API for command-line argument parsing in PHP 8.2+.
What's Included
Week 1: Enums and Config Objects
OptionAction,OptionType,ConflictHandlerParserConfig,OptionConfig,OptionGroupConfigInvalidOptionException,ValueValidationExceptionWeek 2: Result Objects, Exceptions, and Validators
ParseResult,OptionValuesAmbiguousOptionException,MissingValueException,ConflictingOptionExceptionValidatorsutility with 12 composable validatorsWeek 3: Immutable Builders
OptionBuilder: 14 setters, 3 convenience methods (flag, repeatable, counter)GroupBuilder: Group management with title and descriptionParserBuilder: Complete parser configurationWeek 4: ImmutableParser (Core Engine)
-v,-abc(bundling),-nVALUE(attached)--verbose,--name VALUE,--name=VALUEtoBuilder()Week 5: Help Formatting System
HelpFormatter: Immutable help text generatorTest Coverage
Test Breakdown
Design Principles
All 8 guiding principles fully implemented and verified:
Example Usage
Help Output
Features
Parsing Capabilities
-abc=-a -b -c)-nVALUEor--name=VALUE)--verbmatches--verbose)--) option terminatorType System
Actions
Store- Store single valueStoreTrue/StoreFalse- Boolean flagsAppend- Collect multiple valuesCount- Increment counter (e.g.,-vvv= 3)StoreConst/AppendConst- Store predefined constantsCallback- Custom processing functionValidation & Transformation
all(),any()Help Generation
Error Handling
InvalidOptionException- Unknown/invalid optionsAmbiguousOptionException- Partial match ambiguityMissingValueException- Required value not providedConflictingOptionException- Duplicate optionsValueValidationException- Type conversion/validation failuresArchitecture
Immutability
Performance
Design Patterns
toBuilder())Breaking Changes
None - Modern API is in separate namespace:
Horde\Argv\Modern\Horde_Argv_*(unchanged)Both APIs can coexist. Legacy API remains stable and supported.
Migration Path
Users can adopt Modern API gradually:
Documentation
Commits
chore: add test autoload configuration and upgrade guidedocs: mark under-development stub classes as internalfeat(modern): implement Week 1 - Enums and Config objectsfeat(modern): implement Week 2 - Result objects, exceptions, and validatorstest(modern): add comprehensive unit tests for Weeks 1-2feat(Modern): add immutable builders (Week 3)feat(Modern): implement ImmutableParser with full parsing engine (Week 4)feat(Modern): implement help formatting system (Week 5)Checklist
Future Enhancements
Possible future additions (not in this PR):
Status
Modern API is production-ready and feature-complete for command-line argument parsing in PHP 8.2+ applications!