Skip to content

Refactor: Migrate to clap for CLI argument parsing#3

Merged
devark28 merged 11 commits intomasterfrom
refactor/use-crap-in-cli
Nov 14, 2025
Merged

Refactor: Migrate to clap for CLI argument parsing#3
devark28 merged 11 commits intomasterfrom
refactor/use-crap-in-cli

Conversation

@devark28
Copy link
Owner

@devark28 devark28 commented Nov 13, 2025

Overview

This PR replaces the custom CLI argument parsing implementation with clap, a robust and well-maintained argument parsing library. This change improves maintainability, user experience, and reduces the codebase complexity.

Key Changes

1. CLI Parsing Migration

  • Removed custom argument parsing logic including:

    • CliCmd trait and all command-specific parsing implementations
    • try_parse_cmd! macro for command matching
    • Individual command structs (FormatCmd, LintCmd, ListCmd, PickCmd, HelpCmd, VersionCmd)
  • Added clap-based argument parsing:

    • New Args struct with derive macros for declarative CLI definition
    • ArgCommands enum for subcommand handling
    • Automatic help generation and validation

2. Improved User Experience

  • Enhanced help text with detailed usage examples and input mode explanations
  • Support for --version / -v flag (in addition to version subcommand)
  • Better error messages through clap's built-in validation
  • Consistent command interface with clear subcommand structure

3. Code Simplification

  • Removed ~400 lines of custom parsing logic
  • Consolidated command handling into a single, maintainable module (cli/args.rs)
  • Simplified Cli::init() method with cleaner input resolution logic
  • Removed unnecessary command-specific modules and their implementations

4. Module Restructuring

  • Moved engine command implementations from engine/commands/ to engine/ root
    • engine/commands/format.rsengine/format.rs
    • engine/commands/list.rsengine/list.rs
    • engine/commands/pick.rsengine/pick.rs
    • engine/commands/version.rsengine/version.rs
  • Removed engine/commands/help.rs (now handled by clap)

5. Code Cleanup

  • Removed unused methods and error variants:
    • AccessErrors::VariableNotFound
    • CliErrors::UnknownCommand and CliErrors::FailedToParseArgs
    • ParsingErrors::BrokenValidator
    • NamingErrors unused warnings
    • Various unused helper methods in Parser, Document, Block, and Line
  • Cleaned up redundant test code using matches! macro

Dependencies

  • Added clap = { version = "4.0", features = ["derive"] } to Cargo.toml

Behavioral Changes

  • Help is now shown via envmn --help or envmn help (clap standard)
  • Version can be shown via envmn --version / -v or envmn version
  • Error messages are now more descriptive and consistent with clap conventions
  • Default file (.env) behavior remains unchanged

Testing

  • All existing tests pass
  • Manual testing confirms:
    • All subcommands work as expected
    • Stdin piping continues to work correctly
    • File input resolution follows the documented priority (piped input > explicit file > default .env)

Migration Notes

This is a refactoring PR with no breaking changes to the public CLI interface. All existing commands and workflows continue to work as before, but with improved error messages and help text.

- Replace complex custom argument parsing with clap derive macros
- Remove custom command types and macros in favor of clap's patterns
- Maintain same command interface (lint, format, list, pick)
- Add auto-generated help and standard CLI flags
- Simplify codebase by removing ~200 lines of custom parsing logic
- Move version from subcommand to --version/-v flag
- Add detailed help with input modes and usage examples
- Make subcommands required to trigger help when no args provided
- Remove custom help command in favor of clap's built-in help system
This reverts commit 6bd3335.

# Conflicts:
#	src/parser/engine/commands/mod.rs
#	src/parser/engine/help.rs
@devark28 devark28 changed the title Refactor/use crap in cli Refactor: Migrate to clap for CLI argument parsing Nov 14, 2025
- Add comprehensive integration test suite covering all CLI commands
- Fix stdin input priority over file arguments in CLI parsing
- Add tempfile dev dependency for test file management
- Configure IntelliJ test source folder for tests directory
- Extract shared test utilities to tests/common/mod.rs
- Split monolithic cli_integration.rs into command-specific files:
  - version_tests.rs: version and help commands
  - format_tests.rs: format command with file/stdin
  - list_tests.rs: list command
  - pick_tests.rs: pick command and error cases
  - lint_tests.rs: lint command
- Improve test naming consistency (remove "test_" prefix)
- Better error assertion in pick_nonexistent_block test
@devark28 devark28 force-pushed the refactor/use-crap-in-cli branch from a5a5c18 to fb1278e Compare November 14, 2025 17:18
@devark28 devark28 closed this Nov 14, 2025
@devark28 devark28 reopened this Nov 14, 2025
@devark28 devark28 merged commit fecb8fb into master Nov 14, 2025
1 check passed
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.

1 participant