Repository navigation
Add command requirements (requires) - #940
Merged
Merged
Conversation
Thor::Requirement is the value object and the engine behind the requires macro: a command name plus optional args:/options: mappings, validated when declared, with the name resolution, invocation, error wrapping, cycle detection, and the graph walk behind validate_requirements!. Thor::RequirementError < Thor::Error is raised when a requirement cannot be satisfied. Thor::Command gains a trailing requirements member that defaults to an empty list, so existing callers of Command.new are unaffected.
requires declares one or more commands that must run before the next command. Requirements run through invoke from Invocation#invoke_command, so they share its once-per-invocation rule, compose recursively, and run depth-first in declaration order. Options declared by both commands auto-pass by name; args:/options: mappings (static or callable) map the current invocation into a single required command. A malformed command line raises its usage error before any requirement runs, and a command whose requirement failed is not recorded as run. A missing command, a cycle, or a Thor::Error from a required command raises Thor::RequirementError with the original error as cause. Other exceptions propagate unchanged. validate_requirements! resolves every reachable requirement and reports unresolved names and cycles, so a test suite can assert them. help COMMAND lists the command's requirements.
Make the mapping helpers private, use Thor's type-check idiom and error wording, and match the suite's describe style. Split the arity example that chained two raising calls, so both cases run. Add examples for a splat parameter, two classes sharing a namespace, the default namespace, and an explicit options: key the required command does not declare.
paulspringett
marked this pull request as ready for review
October 19, 2026 12:09
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.
🌈
Closes #939
Why
A Thor command can call
invoketo run another command, but there's no declarative way to say a command needs others to have run first, with what inputs. The relationship doesn't show inhelp, and a failure doesn't name which prerequisite failed.Shopify maintains a large set of Rake tasks for local development. Many build on each other (set something up, then seed it, then use it), and we want to move them to Thor for its typed options and
help. Requirements are the piece Thor is missing for that. I've discussed this with @rafaelfranca already and we're aligned on the direction.What
A
requiresclass macro declares that a command needs other commands to have run first, with optionalargs:/options:mappings. AThor::Requirementvalue object owns name resolution and satisfaction;Thor::Invocation#invoke_commandgets a one-line hook that calls it before the command body runs. A failure raises the newThor::RequirementError, with the original error ascause.Thor.validate_requirements!resolves every requirement on a class, for use in a test.help COMMANDgets aRequirements:section listing the direct names.Requirements run through
invoke, so Thor's existing once-per-run dedup applies: a command already run in this invocation is skipped by name, whatever the mapped args. Callinginvokedirectly in a command body is still the way to run a command again with different inputs.The arity check on the command's own arguments runs before its requirements, so a usage error doesn't run any prerequisites first.
Options declared by both commands auto-pass by name. An explicit
options:mapping merges on top of the auto-passed set.Compatibility
No behaviour change for classes that don't use
requires.Thor::Command.newgains one optional trailing argument._shared_configurationgains one new key, for cycle detection.Notes for reviewers
requireshas nomethod_requiresform, unlikemethod_option/optionandmethod_exclusive/exclusive. Should we add this?invokerule. A command whose requirement failed is not recorded, so a laterinvokeretries it.