📦 RequirePackageWithOptions Add a package dependency with custom options
🔍
RequirePackageWithOptionsadds a new dependency package to the project and supports passing extra options (e.g.--dev,--prefer-source,--no-update), equivalent to runningcomposer require [options] packageName:version.
📋 Signature
go
func (c *Composer) RequirePackageWithOptions(packageName string, version string, options map[string]string) error📥 Parameters
| Parameter | Type | Description |
|---|---|---|
packageName | string | The name of the package to add, e.g. symfony/console |
version | string | Version constraint, e.g. ^5.0; pass an empty string "" to use the latest version |
options | map[string]string | Extra options map; keys are option names and values are option values; an empty string "" value denotes a valueless switch flag |
📤 Return value
| Return value | Type | Description |
|---|---|---|
| First return value | error | Returned when an error occurs during addition; nil on success |
📝 Example
go
package main
import (
"log"
"github.com/scagogogo/composer-skills/pkg/composer"
)
func main() {
comp, err := composer.NewComposer()
if err != nil {
log.Fatalf("failed to create Composer instance: %v", err)
}
// Add a development dependency and specify multiple options
options := map[string]string{
"dev": "",
"prefer-source": "",
"no-update": "",
}
if err := comp.RequirePackageWithOptions("phpunit/phpunit", "^9.0", options); err != nil {
log.Fatalf("add dependency failed: %v", err)
}
log.Println("Dependency addition complete")
}🚀 Advanced
- 📋 If no extra options are needed, use the simpler
RequirePackage(packageName, version, dev); thedevboolean is equivalent to adding"dev": ""tooptions. - 🧪 Combined with
"dry-run": "", you can preview the addition result without modifyingcomposer.json, equivalent toRequireDryRunbut with more flexible options. - ⚙️ Common options:
dev(development dependency),prefer-source/prefer-dist(installation source),no-update(only modify json without resolving dependencies),ignore-platform-reqs(ignore platform requirements). - 🔗 Under the hood,
buildOptionsArgs(options)expands the map into command-line arguments, concatenatespackage:version, and then callsc.Run("require", ...); map iteration order does not affect the determinism of the final command construction.