Skip to content

Commit c1c5acc

Browse files
committed
Add non-interactive DCG documentation for AI agents
- Document --answers JSON method for complete automation - Document --answer sequential flags for simple generators - Add --dry-run technique to discover required answers - Include complete examples for all common generators - Add answer keys reference table for quick lookup - Document best practices for AI-assisted development - Add troubleshooting section for common issues Based on official Drush and DCG documentation.
1 parent c298d06 commit c1c5acc

File tree

1 file changed

+170
-0
lines changed

1 file changed

+170
-0
lines changed

.claude/skills/drupal-expert/SKILL.md

Lines changed: 170 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -535,6 +535,176 @@ docker compose exec php drush field:create node article
535535
ddev exec drush generate controller
536536
```
537537

538+
### Non-Interactive Mode for Automation and AI Agents
539+
540+
**CRITICAL: Drush generators are interactive by default. Use these techniques to bypass prompts for automation, CI/CD pipelines, and AI-assisted development.**
541+
542+
#### Method 1: `--answers` with JSON (Recommended)
543+
544+
Pass all answers as a JSON object. This is the most reliable method for complete automation:
545+
546+
```bash
547+
# Generate a complete module non-interactively
548+
drush generate module --answers='{
549+
"name": "My Custom Module",
550+
"machine_name": "my_custom_module",
551+
"description": "A custom module for specific functionality",
552+
"package": "Custom",
553+
"dependencies": "",
554+
"install_file": "no",
555+
"libraries": "no",
556+
"permissions": "no",
557+
"event_subscriber": "no",
558+
"block_plugin": "no",
559+
"controller": "no",
560+
"settings_form": "no"
561+
}'
562+
563+
# Generate a controller non-interactively
564+
drush generate controller --answers='{
565+
"module": "my_custom_module",
566+
"class": "MyController",
567+
"services": ["entity_type.manager", "current_user"]
568+
}'
569+
570+
# Generate a form non-interactively
571+
drush generate form-simple --answers='{
572+
"module": "my_custom_module",
573+
"class": "ContactForm",
574+
"form_id": "my_custom_module_contact",
575+
"route": "yes",
576+
"route_path": "/contact-us",
577+
"route_title": "Contact Us",
578+
"route_permission": "access content",
579+
"link": "no"
580+
}'
581+
```
582+
583+
#### Method 2: Sequential `--answer` Flags
584+
585+
For simpler generators, use multiple `--answer` (or `-a`) flags in order:
586+
587+
```bash
588+
# Answers are consumed in order of the prompts
589+
drush generate controller --answer="my_module" --answer="PageController" --answer=""
590+
591+
# Short form
592+
drush gen controller -a my_module -a PageController -a ""
593+
```
594+
595+
#### Method 3: Discover Required Answers
596+
597+
Use `--dry-run` with verbose output to discover all prompts and their expected values:
598+
599+
```bash
600+
# Preview generation and see all prompts
601+
drush generate module -vvv --dry-run
602+
603+
# This shows you exactly what answers are needed
604+
# Then re-run with --answers JSON
605+
```
606+
607+
#### Method 4: Auto-Accept Defaults
608+
609+
Use `-y` or `--yes` to accept all default values (useful when defaults are acceptable):
610+
611+
```bash
612+
# Accept all defaults
613+
drush generate module -y
614+
615+
# Combine with some answers to override specific defaults
616+
drush generate module --answer="My Module" -y
617+
```
618+
619+
#### Complete Non-Interactive Examples
620+
621+
**Generate a block plugin:**
622+
```bash
623+
drush generate plugin:block --answers='{
624+
"module": "my_custom_module",
625+
"plugin_id": "my_custom_block",
626+
"admin_label": "My Custom Block",
627+
"category": "Custom",
628+
"class": "MyCustomBlock",
629+
"services": ["entity_type.manager"],
630+
"configurable": "no",
631+
"access": "no"
632+
}'
633+
```
634+
635+
**Generate a service:**
636+
```bash
637+
drush generate service --answers='{
638+
"module": "my_custom_module",
639+
"service_name": "my_custom_module.helper",
640+
"class": "HelperService",
641+
"services": ["database", "logger.factory"]
642+
}'
643+
```
644+
645+
**Generate an event subscriber:**
646+
```bash
647+
drush generate event-subscriber --answers='{
648+
"module": "my_custom_module",
649+
"class": "MyEventSubscriber",
650+
"event": "kernel.request"
651+
}'
652+
```
653+
654+
**Generate a Drush command:**
655+
```bash
656+
drush generate drush:command-file --answers='{
657+
"module": "my_custom_module",
658+
"class": "MyCommands",
659+
"services": ["entity_type.manager"]
660+
}'
661+
```
662+
663+
#### Common Answer Keys Reference
664+
665+
| Generator | Common Answer Keys |
666+
|-----------|-------------------|
667+
| `module` | `name`, `machine_name`, `description`, `package`, `dependencies`, `install_file`, `libraries`, `permissions`, `event_subscriber`, `block_plugin`, `controller`, `settings_form` |
668+
| `controller` | `module`, `class`, `services` |
669+
| `form-simple` | `module`, `class`, `form_id`, `route`, `route_path`, `route_title`, `route_permission`, `link` |
670+
| `form-config` | `module`, `class`, `form_id`, `route`, `route_path`, `route_title` |
671+
| `plugin:block` | `module`, `plugin_id`, `admin_label`, `category`, `class`, `services`, `configurable`, `access` |
672+
| `service` | `module`, `service_name`, `class`, `services` |
673+
| `event-subscriber` | `module`, `class`, `event` |
674+
675+
#### Best Practices for AI-Assisted Development
676+
677+
1. **Always use `--answers` JSON** - Most reliable for deterministic generation
678+
2. **Validate with `--dry-run` first** - Preview output before writing files
679+
3. **Escape quotes properly** - Use single quotes around JSON, double quotes inside
680+
4. **Chain with config export** - Always export config after field creation:
681+
```bash
682+
drush field:create node article --field-name=field_subtitle && drush cex -y
683+
```
684+
5. **Document your commands** - Store generation commands in project README for reproducibility
685+
686+
#### Troubleshooting
687+
688+
**"Missing required answer" error:**
689+
```bash
690+
# Use -vvv to see which answer is missing
691+
drush generate module -vvv --answers='{"name": "Test"}'
692+
```
693+
694+
**JSON parsing errors:**
695+
```bash
696+
# Ensure proper escaping - use single quotes outside, double inside
697+
drush generate module --answers='{"name": "Test Module"}' # Correct
698+
drush generate module --answers="{"name": "Test Module"}" # Wrong - shell interprets braces
699+
```
700+
701+
**Interactive prompt still appears:**
702+
```bash
703+
# Some prompts may not have defaults - provide all required answers
704+
# Use --dry-run first to identify all prompts
705+
drush generate module -vvv --dry-run 2>&1 | grep -E "^\s*\?"
706+
```
707+
538708
## Essential Drush Commands
539709

540710
```bash

0 commit comments

Comments
 (0)