diff options
| author | nsfisis <nsfisis@gmail.com> | 2026-06-02 02:44:16 +0900 |
|---|---|---|
| committer | nsfisis <nsfisis@gmail.com> | 2026-06-02 02:44:16 +0900 |
| commit | f8633e1647e42700ee20c8b77353ef992800dc76 (patch) | |
| tree | d594d42ec9bb165b2e49bd32366a47ac29a0370a /crates | |
| parent | 20dbcf11b86cb03c451ba1d5cd9efe17b68fa66d (diff) | |
| download | php-shirabe-f8633e1647e42700ee20c8b77353ef992800dc76.tar.gz php-shirabe-f8633e1647e42700ee20c8b77353ef992800dc76.tar.zst php-shirabe-f8633e1647e42700ee20c8b77353ef992800dc76.zip | |
feat(cli): parse the Composer CLI with bpaf combinatorsfeat/cli-parse
Add a cli module that reproduces every command's argument/option set
from the PHP configure() definitions using bpaf combinators (no derive).
Output is per-command typed structs plus a top-level Command enum; the
bridge into Symfony Console Input is deferred. Verbosity is unified into
the global option and bump-after-update models VALUE_OPTIONAL as a
three-state enum. Known limitation (bundled subcommand short flags) is
recorded as a TODO in the module.
Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
Diffstat (limited to 'crates')
| -rw-r--r-- | crates/shirabe/Cargo.toml | 1 | ||||
| -rw-r--r-- | crates/shirabe/src/cli.rs | 2511 | ||||
| -rw-r--r-- | crates/shirabe/src/lib.rs | 1 | ||||
| -rw-r--r-- | crates/shirabe/src/main.rs | 7 |
4 files changed, 2519 insertions, 1 deletions
diff --git a/crates/shirabe/Cargo.toml b/crates/shirabe/Cargo.toml index aed4c1c..70de6c3 100644 --- a/crates/shirabe/Cargo.toml +++ b/crates/shirabe/Cargo.toml @@ -11,6 +11,7 @@ shirabe-semver.workspace = true anyhow.workspace = true async-trait.workspace = true base64.workspace = true +bpaf.workspace = true chrono.workspace = true indexmap.workspace = true md5.workspace = true diff --git a/crates/shirabe/src/cli.rs b/crates/shirabe/src/cli.rs new file mode 100644 index 0000000..2c494a8 --- /dev/null +++ b/crates/shirabe/src/cli.rs @@ -0,0 +1,2511 @@ +//! Command-line parsing for the Composer CLI, built with bpaf combinators. +//! +//! TODO(phase-c): bpaf does not expand bundled short flags for subcommand-level +//! options. `disambiguate_short` only sees the top-level short flag set, so a +//! bundle like `-il` (= `-i -l`) declared inside a subcommand is not split: +//! `dump-autoload -oa` errors with "`-oa` is not expected", and for commands +//! with an optional positional (e.g. `show -il pkg`) the bundle is silently +//! absorbed as the positional, dropping the real argument. The separated form +//! (`-i -l`) and long form (`--installed --latest`) work correctly. Symfony +//! expands bundles, so this is a compatibility gap affecting every command. +//! Options to fix: pre-split bundles before handing args to bpaf, or drop +//! bpaf subcommands and dispatch each command as its own top-level parser via +//! `run_inner` (so each parser knows its own short flags at disambiguation). + +use bpaf::{OptionParser, Parser, construct, long, positional, pure}; + +fn named(name: &'static str, short_c: Option<char>) -> bpaf::parsers::NamedArg { + match short_c { + Some(c) => long(name).short(c), + None => long(name), + } +} + +/// VALUE_NONE option. +fn flag(name: &'static str, short_c: Option<char>, help: &'static str) -> impl Parser<bool> { + named(name, short_c).help(help).switch() +} + +/// VALUE_REQUIRED option without a default. +fn value( + name: &'static str, + short_c: Option<char>, + help: &'static str, +) -> impl Parser<Option<String>> { + named(name, short_c) + .help(help) + .argument::<String>("VALUE") + .optional() +} + +/// VALUE_REQUIRED option with a string default. +fn value_default( + name: &'static str, + short_c: Option<char>, + help: &'static str, + default: &'static str, +) -> impl Parser<String> { + named(name, short_c) + .help(help) + .argument::<String>("VALUE") + .fallback(default.to_string()) +} + +/// VALUE_REQUIRED | VALUE_IS_ARRAY option. +fn value_many( + name: &'static str, + short_c: Option<char>, + help: &'static str, +) -> impl Parser<Vec<String>> { + named(name, short_c) + .help(help) + .argument::<String>("VALUE") + .many() +} + +/// REQUIRED positional argument. +fn pos_req(name: &'static str, help: &'static str) -> impl Parser<String> { + positional::<String>(name).help(help) +} + +/// OPTIONAL positional argument. +fn pos_opt(name: &'static str, help: &'static str) -> impl Parser<Option<String>> { + positional::<String>(name).help(help).optional() +} + +/// IS_ARRAY (optionally OPTIONAL) positional argument. +fn pos_many(name: &'static str, help: &'static str) -> impl Parser<Vec<String>> { + positional::<String>(name).help(help).many() +} + +/// IS_ARRAY | REQUIRED positional argument. +fn pos_some(name: &'static str, help: &'static str) -> impl Parser<Vec<String>> { + positional::<String>(name) + .help(help) + .some("at least one value is required") +} + +#[derive(Debug, Clone)] +pub struct GlobalOptions { + pub profile: bool, + pub no_plugins: bool, + pub no_scripts: bool, + pub working_dir: Option<String>, + pub no_cache: bool, + pub quiet: bool, + pub verbose: usize, + pub version: bool, + pub ansi: Option<bool>, + pub no_interaction: bool, +} + +fn global_options() -> impl Parser<GlobalOptions> { + let profile = flag( + "profile", + None, + "Display timing and memory usage information", + ); + let no_plugins = flag("no-plugins", None, "Whether to disable plugins."); + let no_scripts = flag( + "no-scripts", + None, + "Skips the execution of all scripts defined in composer.json file.", + ); + let working_dir = value( + "working-dir", + Some('d'), + "If specified, use the given directory as working directory.", + ); + let no_cache = flag("no-cache", None, "Prevent use of the cache"); + let quiet = flag("quiet", Some('q'), "Do not output any message"); + // Symfony declares verbosity as -v|-vv|-vvv; bpaf counts repeated -v occurrences. + let verbose = long("verbose") + .short('v') + .help("Increase the verbosity of messages: 1 for normal output, 2 for more verbose output and 3 for debug") + .req_flag(()) + .count(); + let version = flag("version", Some('V'), "Display this application version"); + let ansi = { + let yes = long("ansi") + .help("Force (or disable --no-ansi) ANSI output") + .req_flag(true); + let no = long("no-ansi").help("Disable ANSI output").req_flag(false); + construct!([yes, no]).optional() + }; + let no_interaction = flag( + "no-interaction", + Some('n'), + "Do not ask any interactive question", + ); + construct!(GlobalOptions { + profile, + no_plugins, + no_scripts, + working_dir, + no_cache, + quiet, + verbose, + version, + ansi, + no_interaction, + }) +} + +#[derive(Debug, Clone)] +pub enum Command { + About, + Archive(ArchiveArgs), + Audit(AuditArgs), + Bump(BumpArgs), + CheckPlatformReqs(CheckPlatformReqsArgs), + ClearCache, + Config(ConfigArgs), + CreateProject(CreateProjectArgs), + Depends(DependsArgs), + Diagnose, + DumpAutoload(DumpAutoloadArgs), + Exec(ExecArgs), + Fund(FundArgs), + Global(GlobalArgs), + Browse(BrowseArgs), + Init(InitArgs), + Install(InstallArgs), + Licenses(LicensesArgs), + Outdated(OutdatedArgs), + Prohibits(ProhibitsArgs), + Reinstall(ReinstallArgs), + Remove(RemoveArgs), + Repository(RepositoryArgs), + Require(RequireArgs), + RunScript(RunScriptArgs), + Search(SearchArgs), + SelfUpdate(SelfUpdateArgs), + Show(ShowArgs), + Status, + Suggests(SuggestsArgs), + Update(UpdateArgs), + Validate(ValidateArgs), +} + +// about +fn about_opts() -> OptionParser<Command> { + pure(Command::About) + .to_options() + .descr("Shows a short information about Composer") +} + +// archive +#[derive(Debug, Clone)] +pub struct ArchiveArgs { + pub format: Option<String>, + pub dir: Option<String>, + pub file: Option<String>, + pub ignore_filters: bool, + pub package: Option<String>, + pub version: Option<String>, +} + +fn archive_opts() -> OptionParser<Command> { + let format = value( + "format", + Some('f'), + "Format of the resulting archive: tar, tar.gz, tar.bz2 or zip (default tar)", + ); + let dir = value("dir", None, "Write the archive to this directory"); + let file = value( + "file", + None, + "Write the archive with the given file name. Note that the format will be appended.", + ); + let ignore_filters = flag("ignore-filters", None, "Ignore filters when saving package"); + let package = pos_opt( + "package", + "The package to archive instead of the current project", + ); + let version = pos_opt( + "version", + "A version constraint to find the package to archive", + ); + construct!(ArchiveArgs { + format, + dir, + file, + ignore_filters, + package, + version, + }) + .map(Command::Archive) + .to_options() + .descr("Creates an archive of this composer package") +} + +// audit +#[derive(Debug, Clone)] +pub struct AuditArgs { + pub no_dev: bool, + pub format: String, + pub locked: bool, + pub abandoned: Option<String>, + pub ignore_severity: Vec<String>, + pub ignore_unreachable: bool, +} + +fn audit_opts() -> OptionParser<Command> { + let no_dev = flag("no-dev", None, "Disables auditing of require-dev packages."); + let format = value_default( + "format", + Some('f'), + "Output format. Must be \"table\", \"plain\", \"json\", or \"summary\".", + "table", + ); + let locked = flag( + "locked", + None, + "Audit based on the lock file instead of the installed packages.", + ); + let abandoned = value( + "abandoned", + None, + "Behavior on abandoned packages. Must be \"ignore\", \"report\", or \"fail\".", + ); + let ignore_severity = value_many( + "ignore-severity", + None, + "Ignore advisories of a certain severity level.", + ); + let ignore_unreachable = flag( + "ignore-unreachable", + None, + "Ignore repositories that are unreachable or return a non-200 status code.", + ); + construct!(AuditArgs { + no_dev, + format, + locked, + abandoned, + ignore_severity, + ignore_unreachable, + }) + .map(Command::Audit) + .to_options() + .descr("Checks for security vulnerability advisories for installed packages") +} + +// bump +#[derive(Debug, Clone)] +pub struct BumpArgs { + pub dev_only: bool, + pub no_dev_only: bool, + pub dry_run: bool, + pub packages: Vec<String>, +} + +fn bump_opts() -> OptionParser<Command> { + let dev_only = flag( + "dev-only", + Some('D'), + "Only bump requirements in \"require-dev\".", + ); + let no_dev_only = flag( + "no-dev-only", + Some('R'), + "Only bump requirements in \"require\".", + ); + let dry_run = flag( + "dry-run", + None, + "Outputs the packages to bump, but will not execute anything.", + ); + let packages = pos_many( + "packages", + "Optional package name(s) to restrict which packages are bumped.", + ); + construct!(BumpArgs { + dev_only, + no_dev_only, + dry_run, + packages, + }) + .map(Command::Bump) + .to_options() + .descr("Increases the lower limit of your composer.json requirements to the currently installed versions") +} + +// check-platform-reqs +#[derive(Debug, Clone)] +pub struct CheckPlatformReqsArgs { + pub no_dev: bool, + pub lock: bool, + pub format: String, +} + +fn check_platform_reqs_opts() -> OptionParser<Command> { + let no_dev = flag( + "no-dev", + None, + "Disables checking of require-dev packages requirements.", + ); + let lock = flag( + "lock", + None, + "Checks requirements only from the lock file, not from installed packages.", + ); + let format = value_default( + "format", + Some('f'), + "Format of the output: text or json", + "text", + ); + construct!(CheckPlatformReqsArgs { + no_dev, + lock, + format, + }) + .map(Command::CheckPlatformReqs) + .to_options() + .descr("Check that platform requirements are satisfied") +} + +// clear-cache +fn clear_cache_opts() -> OptionParser<Command> { + pure(Command::ClearCache) + .to_options() + .descr("Clears composer's internal package cache") +} + +// config +#[derive(Debug, Clone)] +pub struct ConfigArgs { + pub global: bool, + pub editor: bool, + pub auth: bool, + pub unset: bool, + pub list: bool, + pub file: Option<String>, + pub absolute: bool, + pub json: bool, + pub merge: bool, + pub append: bool, + pub source: bool, + pub setting_key: Option<String>, + pub setting_value: Vec<String>, +} + +fn config_opts() -> OptionParser<Command> { + let global = flag( + "global", + Some('g'), + "Apply command to the global config file", + ); + let editor = flag("editor", Some('e'), "Open editor"); + let auth = flag( + "auth", + Some('a'), + "Affect auth config file (only used for --editor)", + ); + let unset = flag("unset", None, "Unset the given setting-key"); + let list = flag("list", Some('l'), "List configuration settings"); + let file = value( + "file", + Some('f'), + "If you want to choose a different composer.json or config.json", + ); + let absolute = flag( + "absolute", + None, + "Returns absolute paths when fetching *-dir config values instead of relative", + ); + let json = flag( + "json", + Some('j'), + "JSON decode the setting value, to be used with extra.* keys", + ); + let merge = flag( + "merge", + Some('m'), + "Merge the setting value with the current value, to be used with extra.* or audit.ignore[-abandoned] keys in combination with --json", + ); + let append = flag( + "append", + None, + "When adding a repository, append it (lowest priority) to the existing ones instead of prepending it (highest priority)", + ); + let source = flag( + "source", + None, + "Display where the config value is loaded from", + ); + let setting_key = pos_opt("setting-key", "Setting key"); + let setting_value = pos_many("setting-value", "Setting value"); + construct!(ConfigArgs { + global, + editor, + auth, + unset, + list, + file, + absolute, + json, + merge, + append, + source, + setting_key, + setting_value, + }) + .map(Command::Config) + .to_options() + .descr("Sets config options") +} + +// create-project +#[derive(Debug, Clone)] +pub struct CreateProjectArgs { + pub stability: Option<String>, + pub prefer_source: bool, + pub prefer_dist: bool, + pub prefer_install: Option<String>, + pub repository: Vec<String>, + pub repository_url: Option<String>, + pub add_repository: bool, + pub dev: bool, + pub no_dev: bool, + pub no_custom_installers: bool, + pub no_scripts: bool, + pub no_progress: bool, + pub no_secure_http: bool, + pub keep_vcs: bool, + pub remove_vcs: bool, + pub no_install: bool, + pub no_audit: bool, + pub audit_format: String, + pub no_security_blocking: bool, + pub ignore_platform_req: Vec<String>, + pub ignore_platform_reqs: bool, + pub ask: bool, + pub package: Option<String>, + pub directory: Option<String>, + pub version: Option<String>, +} + +fn create_project_opts() -> OptionParser<Command> { + let stability = value( + "stability", + Some('s'), + "Minimum-stability allowed (unless a version is specified).", + ); + let prefer_source = flag( + "prefer-source", + None, + "Forces installation from package sources when possible, including VCS information.", + ); + let prefer_dist = flag( + "prefer-dist", + None, + "Forces installation from package dist (default behavior).", + ); + let prefer_install = value( + "prefer-install", + None, + "Forces installation from package dist|source|auto (auto chooses source for dev versions, dist for the rest).", + ); + let repository = value_many( + "repository", + None, + "Add custom repositories to look the package up, either by URL or using JSON arrays", + ); + let repository_url = value( + "repository-url", + None, + "DEPRECATED: Use --repository instead.", + ); + let add_repository = flag( + "add-repository", + None, + "Add the custom repository in the composer.json. If a lock file is present it will be deleted and an update will be run instead of install.", + ); + let dev = flag( + "dev", + None, + "Enables installation of require-dev packages (enabled by default, only present for BC).", + ); + let no_dev = flag( + "no-dev", + None, + "Disables installation of require-dev packages.", + ); + let no_custom_installers = flag( + "no-custom-installers", + None, + "DEPRECATED: Use no-plugins instead.", + ); + let no_scripts = flag( + "no-scripts", + None, + "Whether to prevent execution of all defined scripts in the root package.", + ); + let no_progress = flag("no-progress", None, "Do not output download progress."); + let no_secure_http = flag( + "no-secure-http", + None, + "Disable the secure-http config option temporarily while installing the root package. Use at your own risk. Using this flag is a bad idea.", + ); + let keep_vcs = flag( + "keep-vcs", + None, + "Whether to prevent deleting the vcs folder.", + ); + let remove_vcs = flag( + "remove-vcs", + None, + "Whether to force deletion of the vcs folder without prompting.", + ); + let no_install = flag( + "no-install", + None, + "Whether to skip installation of the package dependencies.", + ); + let no_audit = flag( + "no-audit", + None, + "Whether to skip auditing of the installed package dependencies (can also be set via the COMPOSER_NO_AUDIT=1 env var).", + ); + let audit_format = value_default( + "audit-format", + None, + "Audit output format. Must be \"table\", \"plain\", \"json\" or \"summary\".", + "summary", + ); + let no_security_blocking = flag( + "no-security-blocking", + None, + "Allows installing packages with security advisories or that are abandoned (can also be set via the COMPOSER_NO_SECURITY_BLOCKING=1 env var).", + ); + let ignore_platform_req = value_many( + "ignore-platform-req", + None, + "Ignore a specific platform requirement (php & ext- packages).", + ); + let ignore_platform_reqs = flag( + "ignore-platform-reqs", + None, + "Ignore all platform requirements (php & ext- packages).", + ); + let ask = flag("ask", None, "Whether to ask for project directory."); + let package = pos_opt("package", "Package name to be installed"); + let directory = pos_opt("directory", "Directory where the files should be created"); + let version = pos_opt("version", "Version, will default to latest"); + construct!(CreateProjectArgs { + stability, + prefer_source, + prefer_dist, + prefer_install, + repository, + repository_url, + add_repository, + dev, + no_dev, + no_custom_installers, + no_scripts, + no_progress, + no_secure_http, + keep_vcs, + remove_vcs, + no_install, + no_audit, + audit_format, + no_security_blocking, + ignore_platform_req, + ignore_platform_reqs, + ask, + package, + directory, + version, + }) + .map(Command::CreateProject) + .to_options() + .descr("Creates new project from a package into given directory") +} + +// depends (why) +#[derive(Debug, Clone)] +pub struct DependsArgs { + pub recursive: bool, + pub tree: bool, + pub locked: bool, + pub package: String, +} + +fn depends_opts() -> OptionParser<Command> { + let recursive = flag( + "recursive", + Some('r'), + "Recursively resolves up to the root package", + ); + let tree = flag("tree", Some('t'), "Prints the results as a nested tree"); + let locked = flag( + "locked", + None, + "Read dependency information from composer.lock", + ); + let package = pos_req("package", "Package to inspect"); + construct!(DependsArgs { + recursive, + tree, + locked, + package, + }) + .map(Command::Depends) + .to_options() + .descr("Shows which packages cause the given package to be installed") +} + +// diagnose +fn diagnose_opts() -> OptionParser<Command> { + pure(Command::Diagnose) + .to_options() + .descr("Diagnoses the system to identify common errors") +} + +// dump-autoload +#[derive(Debug, Clone)] +pub struct DumpAutoloadArgs { + pub optimize: bool, + pub classmap_authoritative: bool, + pub apcu: bool, + pub apcu_prefix: Option<String>, + pub dry_run: bool, + pub dev: bool, + pub no_dev: bool, + pub ignore_platform_req: Vec<String>, + pub ignore_platform_reqs: bool, + pub strict_psr: bool, + pub strict_ambiguous: bool, +} + +fn dump_autoload_opts() -> OptionParser<Command> { + let optimize = flag( + "optimize", + Some('o'), + "Optimizes PSR0 and PSR4 packages to be loaded with classmaps too, good for production.", + ); + let classmap_authoritative = flag( + "classmap-authoritative", + Some('a'), + "Autoload classes from the classmap only. Implicitly enables `--optimize`.", + ); + let apcu = flag("apcu", None, "Use APCu to cache found/not-found classes."); + let apcu_prefix = value( + "apcu-prefix", + None, + "Use a custom prefix for the APCu autoloader cache. Implicitly enables --apcu", + ); + let dry_run = flag( + "dry-run", + None, + "Outputs the operations but will not execute anything.", + ); + let dev = flag( + "dev", + None, + "Enables autoload-dev rules. Composer will by default infer this automatically according to the last install or update --no-dev state.", + ); + let no_dev = flag( + "no-dev", + None, + "Disables autoload-dev rules. Composer will by default infer this automatically according to the last install or update --no-dev state.", + ); + let ignore_platform_req = value_many( + "ignore-platform-req", + None, + "Ignore a specific platform requirement (php & ext- packages).", + ); + let ignore_platform_reqs = flag( + "ignore-platform-reqs", + None, + "Ignore all platform requirements (php & ext- packages).", + ); + let strict_psr = flag( + "strict-psr", + None, + "Return a failed status code (1) if PSR-4 or PSR-0 mapping errors are present. Requires --optimize to work.", + ); + let strict_ambiguous = flag( + "strict-ambiguous", + None, + "Return a failed status code (2) if the same class is found in multiple files. Requires --optimize to work.", + ); + construct!(DumpAutoloadArgs { + optimize, + classmap_authoritative, + apcu, + apcu_prefix, + dry_run, + dev, + no_dev, + ignore_platform_req, + ignore_platform_reqs, + strict_psr, + strict_ambiguous, + }) + .map(Command::DumpAutoload) + .to_options() + .descr("Dumps the autoloader") +} + +// exec +#[derive(Debug, Clone)] +pub struct ExecArgs { + pub list: bool, + pub binary: Option<String>, + pub args: Vec<String>, +} + +fn exec_opts() -> OptionParser<Command> { + let list = flag("list", Some('l'), ""); + let binary = pos_opt("binary", "The binary to run, e.g. phpunit"); + let args = pos_many( + "args", + "Arguments to pass to the binary. Use -- to separate from composer arguments", + ); + construct!(ExecArgs { list, binary, args }) + .map(Command::Exec) + .to_options() + .descr("Executes a vendored binary/script") +} + +// fund +#[derive(Debug, Clone)] +pub struct FundArgs { + pub format: String, +} + +fn fund_opts() -> OptionParser<Command> { + let format = value_default( + "format", + Some('f'), + "Format of the output: text or json", + "text", + ); + construct!(FundArgs { format }) + .map(Command::Fund) + .to_options() + .descr("Discover how to help fund the maintenance of your dependencies") +} + +// global +#[derive(Debug, Clone)] +pub struct GlobalArgs { + pub command_name: String, + pub args: Vec<String>, +} + +fn global_opts() -> OptionParser<Command> { + let command_name = pos_req("command-name", ""); + let args = pos_many("args", ""); + construct!(GlobalArgs { command_name, args }) + .map(Command::Global) + .to_options() + .descr("Allows running commands in the global composer dir ($COMPOSER_HOME)") +} + +// browse (home) +#[derive(Debug, Clone)] +pub struct BrowseArgs { + pub homepage: bool, + pub show: bool, + pub packages: Vec<String>, +} + +fn browse_opts() -> OptionParser<Command> { + let homepage = flag( + "homepage", + Some('H'), + "Open the homepage instead of the repository URL.", + ); + let show = flag( + "show", + Some('s'), + "Only show the homepage or repository URL.", + ); + let packages = pos_many("packages", "Package(s) to browse to."); + construct!(BrowseArgs { + homepage, + show, + packages, + }) + .map(Command::Browse) + .to_options() + .descr("Opens the package's repository URL or homepage in your browser") +} + +// init +#[derive(Debug, Clone)] +pub struct InitArgs { + pub name: Option<String>, + pub description: Option<String>, + pub author: Option<String>, + pub r#type: Option<String>, + pub homepage: Option<String>, + pub require: Vec<String>, + pub require_dev: Vec<String>, + pub stability: Option<String>, + pub license: Option<String>, + pub repository: Vec<String>, + pub autoload: Option<String>, +} + +fn init_opts() -> OptionParser<Command> { + let name = value("name", None, "Name of the package"); + let description = value("description", None, "Description of package"); + let author = value("author", None, "Author name of package"); + let r#type = value( + "type", + None, + "Type of package (e.g. library, project, metapackage, composer-plugin)", + ); + let homepage = value("homepage", None, "Homepage of package"); + let require = value_many( + "require", + None, + "Package to require with a version constraint, e.g. foo/bar:1.0.0 or foo/bar=1.0.0 or \"foo/bar 1.0.0\"", + ); + let require_dev = value_many( + "require-dev", + None, + "Package to require for development with a version constraint, e.g. foo/bar:1.0.0 or foo/bar=1.0.0 or \"foo/bar 1.0.0\"", + ); + let stability = value( + "stability", + Some('s'), + "Minimum stability (empty or one of: stable, RC, beta, alpha, dev)", + ); + let license = value("license", Some('l'), "License of package"); + let repository = value_many( + "repository", + None, + "Add custom repositories, either by URL or using JSON arrays", + ); + let autoload = value( + "autoload", + Some('a'), + "Add PSR-4 autoload mapping. Maps your package's namespace to the provided directory. (Expects a relative path, e.g. src/)", + ); + construct!(InitArgs { + name, + description, + author, + r#type, + homepage, + require, + require_dev, + stability, + license, + repository, + autoload, + }) + .map(Command::Init) + .to_options() + .descr("Creates a basic composer.json file in current directory") +} + +// install (i) +#[derive(Debug, Clone)] +pub struct InstallArgs { + pub prefer_source: bool, + pub prefer_dist: bool, + pub prefer_install: Option<String>, + pub dry_run: bool, + pub download_only: bool, + pub dev: bool, + pub no_suggest: bool, + pub no_dev: bool, + pub no_security_blocking: bool, + pub no_autoloader: bool, + pub no_progress: bool, + pub no_install: bool, + pub audit: bool, + pub audit_format: String, + // TODO(phase-c): `verbose` is unified into the global verbosity option (see + // GlobalOptions). Kept commented out because dropping the command-local + // definition changes `--help` output and must be reconciled with Symfony's + // merged InputDefinition. + // pub verbose: bool, + pub optimize_autoloader: bool, + pub classmap_authoritative: bool, + pub apcu_autoloader: bool, + pub apcu_autoloader_prefix: Option<String>, + pub ignore_platform_req: Vec<String>, + pub ignore_platform_reqs: bool, + pub packages: Vec<String>, +} + +fn install_opts() -> OptionParser<Command> { + let prefer_source = flag( + "prefer-source", + None, + "Forces installation from package sources when possible, including VCS information.", + ); + let prefer_dist = flag( + "prefer-dist", + None, + "Forces installation from package dist (default behavior).", + ); + let prefer_install = value( + "prefer-install", + None, + "Forces installation from package dist|source|auto (auto chooses source for dev versions, dist for the rest).", + ); + let dry_run = flag( + "dry-run", + None, + "Outputs the operations but will not execute anything (implicitly enables --verbose).", + ); + let download_only = flag( + "download-only", + None, + "Download only, do not install packages.", + ); + let dev = flag( + "dev", + None, + "DEPRECATED: Enables installation of require-dev packages (enabled by default, only present for BC).", + ); + let no_suggest = flag( + "no-suggest", + None, + "DEPRECATED: This flag does not exist anymore.", + ); + let no_dev = flag( + "no-dev", + None, + "Disables installation of require-dev packages.", + ); + let no_security_blocking = flag( + "no-security-blocking", + None, + "Allows installing packages with security advisories or that are abandoned (can also be set via the COMPOSER_NO_SECURITY_BLOCKING=1 env var). Only applies when no lock file is present.", + ); + let no_autoloader = flag("no-autoloader", None, "Skips autoloader generation"); + let no_progress = flag("no-progress", None, "Do not output download progress."); + let no_install = flag( + "no-install", + None, + "Do not use, only defined here to catch misuse of the install command.", + ); + let audit = flag( + "audit", + None, + "Run an audit after installation is complete.", + ); + let audit_format = value_default( + "audit-format", + None, + "Audit output format. Must be \"table\", \"plain\", \"json\", or \"summary\".", + "summary", + ); + // TODO(phase-c): `verbose` (-v|-vv|-vvv) is unified into the global verbosity + // option; kept commented out because dropping it changes `--help` output and + // must be reconciled with Symfony's merged InputDefinition. + // let verbose = flag( + // "verbose", + // Some('v'), + // "Shows more details including new commits pulled in when updating packages.", + // ); + let optimize_autoloader = flag( + "optimize-autoloader", + Some('o'), + "Optimize autoloader during autoloader dump", + ); + let classmap_authoritative = flag( + "classmap-authoritative", + Some('a'), + "Autoload classes from the classmap only. Implicitly enables `--optimize-autoloader`.", + ); + let apcu_autoloader = flag( + "apcu-autoloader", + None, + "Use APCu to cache found/not-found classes.", + ); + let apcu_autoloader_prefix = value( + "apcu-autoloader-prefix", + None, + "Use a custom prefix for the APCu autoloader cache. Implicitly enables --apcu-autoloader", + ); + let ignore_platform_req = value_many( + "ignore-platform-req", + None, + "Ignore a specific platform requirement (php & ext- packages).", + ); + let ignore_platform_reqs = flag( + "ignore-platform-reqs", + None, + "Ignore all platform requirements (php & ext- packages).", + ); + let packages = pos_many( + "packages", + "Should not be provided, use composer require instead to add a given package to composer.json.", + ); + construct!(InstallArgs { + prefer_source, + prefer_dist, + prefer_install, + dry_run, + download_only, + dev, + no_suggest, + no_dev, + no_security_blocking, + no_autoloader, + no_progress, + no_install, + audit, + audit_format, + // verbose, + optimize_autoloader, + classmap_authoritative, + apcu_autoloader, + apcu_autoloader_prefix, + ignore_platform_req, + ignore_platform_reqs, + packages, + }) + .map(Command::Install) + .to_options() + .descr("Installs the project dependencies from the composer.lock file if present, or falls back on the composer.json") +} + +// licenses +#[derive(Debug, Clone)] +pub struct LicensesArgs { + pub format: String, + pub no_dev: bool, + pub locked: bool, +} + +fn licenses_opts() -> OptionParser<Command> { + let format = value_default( + "format", + Some('f'), + "Format of the output: text, json or summary", + "text", + ); + let no_dev = flag("no-dev", None, "Disables search in require-dev packages."); + let locked = flag( + "locked", + None, + "Shows licenses from the lock file instead of installed packages.", + ); + construct!(LicensesArgs { + format, + no_dev, + locked, + }) + .map(Command::Licenses) + .to_options() + .descr("Shows information about licenses of dependencies") +} + +// outdated +#[derive(Debug, Clone)] +pub struct OutdatedArgs { + pub outdated: bool, + pub all: bool, + pub locked: bool, + pub direct: bool, + pub strict: bool, + pub major_only: bool, + pub minor_only: bool, + pub patch_only: bool, + pub sort_by_age: bool, + pub format: String, + pub ignore: Vec<String>, + pub no_dev: bool, + pub ignore_platform_req: Vec<String>, + pub ignore_platform_reqs: bool, + pub package: Option<String>, +} + +fn outdated_opts() -> OptionParser<Command> { + let outdated = flag( + "outdated", + Some('o'), + "Show only packages that are outdated (this is the default, but present here for compat with `show`", + ); + let all = flag( + "all", + Some('a'), + "Show all installed packages with their latest versions", + ); + let locked = flag( + "locked", + None, + "Shows updates for packages from the lock file, regardless of what is currently in vendor dir", + ); + let direct = flag( + "direct", + Some('D'), + "Shows only packages that are directly required by the root package", + ); + let strict = flag( + "strict", + None, + "Return a non-zero exit code when there are outdated packages", + ); + let major_only = flag( + "major-only", + Some('M'), + "Show only packages that have major SemVer-compatible updates.", + ); + let minor_only = flag( + "minor-only", + Some('m'), + "Show only packages that have minor SemVer-compatible updates.", + ); + let patch_only = flag( + "patch-only", + Some('p'), + "Show only packages that have patch SemVer-compatible updates.", + ); + let sort_by_age = flag( + "sort-by-age", + Some('A'), + "Displays the installed version's age, and sorts packages oldest first.", + ); + let format = value_default( + "format", + Some('f'), + "Format of the output: text or json", + "text", + ); + let ignore = value_many( + "ignore", + None, + "Ignore specified package(s). Can contain wildcards (*). Use it if you don't want to be informed about new versions of some packages.", + ); + let no_dev = flag("no-dev", None, "Disables search in require-dev packages."); + let ignore_platform_req = value_many( + "ignore-platform-req", + None, + "Ignore a specific platform requirement (php & ext- packages). Use with the --outdated option", + ); + let ignore_platform_reqs = flag( + "ignore-platform-reqs", + None, + "Ignore all platform requirements (php & ext- packages). Use with the --outdated option", + ); + let package = pos_opt( + "package", + "Package to inspect. Or a name including a wildcard (*) to filter lists of packages instead.", + ); + construct!(OutdatedArgs { + outdated, + all, + locked, + direct, + strict, + major_only, + minor_only, + patch_only, + sort_by_age, + format, + ignore, + no_dev, + ignore_platform_req, + ignore_platform_reqs, + package, + }) + .map(Command::Outdated) + .to_options() + .descr("Shows a list of installed packages that have updates available, including their latest version") +} + +// prohibits (why-not) +#[derive(Debug, Clone)] +pub struct ProhibitsArgs { + pub recursive: bool, + pub tree: bool, + pub locked: bool, + pub package: String, + pub version: String, +} + +fn prohibits_opts() -> OptionParser<Command> { + let recursive = flag( + "recursive", + Some('r'), + "Recursively resolves up to the root package", + ); + let tree = flag("tree", Some('t'), "Prints the results as a nested tree"); + let locked = flag( + "locked", + None, + "Read dependency information from composer.lock", + ); + let package = pos_req("package", "Package to inspect"); + let version = pos_req( + "version", + "Version constraint, which version you expected to be installed", + ); + construct!(ProhibitsArgs { + recursive, + tree, + locked, + package, + version, + }) + .map(Command::Prohibits) + .to_options() + .descr("Shows which packages prevent the given package from being installed") +} + +// reinstall +#[derive(Debug, Clone)] +pub struct ReinstallArgs { + pub prefer_source: bool, + pub prefer_dist: bool, + pub prefer_install: Option<String>, + pub no_autoloader: bool, + pub no_progress: bool, + pub optimize_autoloader: bool, + pub classmap_authoritative: bool, + pub apcu_autoloader: bool, + pub apcu_autoloader_prefix: Option<String>, + pub ignore_platform_req: Vec<String>, + pub ignore_platform_reqs: bool, + pub r#type: Vec<String>, + pub packages: Vec<String>, +} + +fn reinstall_opts() -> OptionParser<Command> { + let prefer_source = flag( + "prefer-source", + None, + "Forces installation from package sources when possible, including VCS information.", + ); + let prefer_dist = flag( + "prefer-dist", + None, + "Forces installation from package dist (default behavior).", + ); + let prefer_install = value( + "prefer-install", + None, + "Forces installation from package dist|source|auto (auto chooses source for dev versions, dist for the rest).", + ); + let no_autoloader = flag("no-autoloader", None, "Skips autoloader generation"); + let no_progress = flag("no-progress", None, "Do not output download progress."); + let optimize_autoloader = flag( + "optimize-autoloader", + Some('o'), + "Optimize autoloader during autoloader dump", + ); + let classmap_authoritative = flag( + "classmap-authoritative", + Some('a'), + "Autoload classes from the classmap only. Implicitly enables `--optimize-autoloader`.", + ); + let apcu_autoloader = flag( + "apcu-autoloader", + None, + "Use APCu to cache found/not-found classes.", + ); + let apcu_autoloader_prefix = value( + "apcu-autoloader-prefix", + None, + "Use a custom prefix for the APCu autoloader cache. Implicitly enables --apcu-autoloader", + ); + let ignore_platform_req = value_many( + "ignore-platform-req", + None, + "Ignore a specific platform requirement (php & ext- packages).", + ); + let ignore_platform_reqs = flag( + "ignore-platform-reqs", + None, + "Ignore all platform requirements (php & ext- packages).", + ); + let r#type = value_many("type", None, "Filter packages to reinstall by type(s)"); + let packages = pos_many( + "packages", + "List of package names to reinstall, can include a wildcard (*) to match any substring.", + ); + construct!(ReinstallArgs { + prefer_source, + prefer_dist, + prefer_install, + no_autoloader, + no_progress, + optimize_autoloader, + classmap_authoritative, + apcu_autoloader, + apcu_autoloader_prefix, + ignore_platform_req, + ignore_platform_reqs, + r#type, + packages, + }) + .map(Command::Reinstall) + .to_options() + .descr("Uninstalls and reinstalls the given package names") +} + +// remove (rm, uninstall) +#[derive(Debug, Clone)] +pub struct RemoveArgs { + pub dev: bool, + pub dry_run: bool, + pub no_progress: bool, + pub no_update: bool, + pub no_install: bool, + pub no_audit: bool, + pub audit_format: String, + pub no_security_blocking: bool, + pub update_no_dev: bool, + pub update_with_dependencies: bool, + pub update_with_all_dependencies: bool, + pub with_all_dependencies: bool, + pub no_update_with_dependencies: bool, + pub minimal_changes: bool, + pub unused: bool, + pub ignore_platform_req: Vec<String>, + pub ignore_platform_reqs: bool, + pub optimize_autoloader: bool, + pub classmap_authoritative: bool, + pub apcu_autoloader: bool, + pub apcu_autoloader_prefix: Option<String>, + pub packages: Vec<String>, +} + +fn remove_opts() -> OptionParser<Command> { + let dev = flag( + "dev", + None, + "Removes a package from the require-dev section.", + ); + let dry_run = flag( + "dry-run", + None, + "Outputs the operations but will not execute anything (implicitly enables --verbose).", + ); + let no_progress = flag("no-progress", None, "Do not output download progress."); + let no_update = flag( + "no-update", + None, + "Disables the automatic update of the dependencies (implies --no-install).", + ); + let no_install = flag( + "no-install", + None, + "Skip the install step after updating the composer.lock file.", + ); + let no_audit = flag( + "no-audit", + None, + "Skip the audit step after updating the composer.lock file (can also be set via the COMPOSER_NO_AUDIT=1 env var).", + ); + let audit_format = value_default( + "audit-format", + None, + "Audit output format. Must be \"table\", \"plain\", \"json\", or \"summary\".", + "summary", + ); + let no_security_blocking = flag( + "no-security-blocking", + None, + "Allows installing packages with security advisories or that are abandoned (can also be set via the COMPOSER_NO_SECURITY_BLOCKING=1 env var).", + ); + let update_no_dev = flag( + "update-no-dev", + None, + "Run the dependency update with the --no-dev option.", + ); + let update_with_dependencies = flag( + "update-with-dependencies", + Some('w'), + "Allows inherited dependencies to be updated with explicit dependencies (can also be set via the COMPOSER_WITH_DEPENDENCIES=1 env var). (Deprecated, is now default behavior)", + ); + let update_with_all_dependencies = flag( + "update-with-all-dependencies", + Some('W'), + "Allows all inherited dependencies to be updated, including those that are root requirements (can also be set via the COMPOSER_WITH_ALL_DEPENDENCIES=1 env var).", + ); + let with_all_dependencies = flag( + "with-all-dependencies", + None, + "Alias for --update-with-all-dependencies", + ); + let no_update_with_dependencies = flag( + "no-update-with-dependencies", + None, + "Does not allow inherited dependencies to be updated with explicit dependencies.", + ); + let minimal_changes = flag( + "minimal-changes", + Some('m'), + "During an update with -w/-W, only perform absolutely necessary changes to transitive dependencies (can also be set via the COMPOSER_MINIMAL_CHANGES=1 env var).", + ); + let unused = flag( + "unused", + None, + "Remove all packages which are locked but not required by any other package.", + ); + let ignore_platform_req = value_many( + "ignore-platform-req", + None, + "Ignore a specific platform requirement (php & ext- packages).", + ); + let ignore_platform_reqs = flag( + "ignore-platform-reqs", + None, + "Ignore all platform requirements (php & ext- packages).", + ); + let optimize_autoloader = flag( + "optimize-autoloader", + Some('o'), + "Optimize autoloader during autoloader dump", + ); + let classmap_authoritative = flag( + "classmap-authoritative", + Some('a'), + "Autoload classes from the classmap only. Implicitly enables `--optimize-autoloader`.", + ); + let apcu_autoloader = flag( + "apcu-autoloader", + None, + "Use APCu to cache found/not-found classes.", + ); + let apcu_autoloader_prefix = value( + "apcu-autoloader-prefix", + None, + "Use a custom prefix for the APCu autoloader cache. Implicitly enables --apcu-autoloader", + ); + let packages = pos_many("packages", "Packages that should be removed."); + construct!(RemoveArgs { + dev, + dry_run, + no_progress, + no_update, + no_install, + no_audit, + audit_format, + no_security_blocking, + update_no_dev, + update_with_dependencies, + update_with_all_dependencies, + with_all_dependencies, + no_update_with_dependencies, + minimal_changes, + unused, + ignore_platform_req, + ignore_platform_reqs, + optimize_autoloader, + classmap_authoritative, + apcu_autoloader, + apcu_autoloader_prefix, + packages, + }) + .map(Command::Remove) + .to_options() + .descr("Removes a package from the require or require-dev") +} + +// repository (repo) +#[derive(Debug, Clone)] +pub struct RepositoryArgs { + pub global: bool, + pub file: Option<String>, + pub append: bool, + pub before: Option<String>, + pub after: Option<String>, + pub action: String, + pub name: Option<String>, + pub arg1: Option<String>, + pub arg2: Option<String>, +} + +fn repository_opts() -> OptionParser<Command> { + let global = flag( + "global", + Some('g'), + "Apply command to the global config file", + ); + let file = value( + "file", + Some('f'), + "If you want to choose a different composer.json or config.json", + ); + let append = flag( + "append", + None, + "When adding a repository, append it (lower priority) instead of prepending it", + ); + let before = value( + "before", + None, + "When adding a repository, insert it before the given repository name", + ); + let after = value( + "after", + None, + "When adding a repository, insert it after the given repository name", + ); + let action = positional::<String>("action") + .help("Action to perform: list, add, remove, set-url, get-url, enable, disable") + .fallback("list".to_string()); + let name = pos_opt( + "name", + "Repository name (or special name packagist.org for enable/disable)", + ); + let arg1 = pos_opt( + "arg1", + "Type for add, or new URL for set-url, or JSON config for add", + ); + let arg2 = pos_opt("arg2", "URL for add (if not using JSON)"); + construct!(RepositoryArgs { + global, + file, + append, + before, + after, + action, + name, + arg1, + arg2, + }) + .map(Command::Repository) + .to_options() + .descr("Manages repositories") +} + +// require (r) +#[derive(Debug, Clone)] +pub struct RequireArgs { + pub dev: bool, + pub dry_run: bool, + pub prefer_source: bool, + pub prefer_dist: bool, + pub prefer_install: Option<String>, + pub fixed: bool, + pub no_suggest: bool, + pub no_progress: bool, + pub no_update: bool, + pub no_install: bool, + pub no_audit: bool, + pub audit_format: String, + pub no_security_blocking: bool, + pub update_no_dev: bool, + pub update_with_dependencies: bool, + pub update_with_all_dependencies: bool, + pub with_dependencies: bool, + pub with_all_dependencies: bool, + pub ignore_platform_req: Vec<String>, + pub ignore_platform_reqs: bool, + pub prefer_stable: bool, + pub prefer_lowest: bool, + pub minimal_changes: bool, + pub sort_packages: bool, + pub optimize_autoloader: bool, + pub classmap_authoritative: bool, + pub apcu_autoloader: bool, + pub apcu_autoloader_prefix: Option<String>, + pub packages: Vec<String>, +} + +fn require_opts() -> OptionParser<Command> { + let dev = flag("dev", None, "Add requirement to require-dev."); + let dry_run = flag( + "dry-run", + None, + "Outputs the operations but will not execute anything (implicitly enables --verbose).", + ); + let prefer_source = flag( + "prefer-source", + None, + "Forces installation from package sources when possible, including VCS information.", + ); + let prefer_dist = flag( + "prefer-dist", + None, + "Forces installation from package dist (default behavior).", + ); + let prefer_install = value( + "prefer-install", + None, + "Forces installation from package dist|source|auto (auto chooses source for dev versions, dist for the rest).", + ); + let fixed = flag("fixed", None, "Write fixed version to the composer.json."); + let no_suggest = flag( + "no-suggest", + None, + "DEPRECATED: This flag does not exist anymore.", + ); + let no_progress = flag("no-progress", None, "Do not output download progress."); + let no_update = flag( + "no-update", + None, + "Disables the automatic update of the dependencies (implies --no-install).", + ); + let no_install = flag( + "no-install", + None, + "Skip the install step after updating the composer.lock file.", + ); + let no_audit = flag( + "no-audit", + None, + "Skip the audit step after updating the composer.lock file (can also be set via the COMPOSER_NO_AUDIT=1 env var).", + ); + let audit_format = value_default( + "audit-format", + None, + "Audit output format. Must be \"table\", \"plain\", \"json\", or \"summary\".", + "summary", + ); + let no_security_blocking = flag( + "no-security-blocking", + None, + "Allows installing packages with security advisories or that are abandoned (can also be set via the COMPOSER_NO_SECURITY_BLOCKING=1 env var).", + ); + let update_no_dev = flag( + "update-no-dev", + None, + "Run the dependency update with the --no-dev option.", + ); + let update_with_dependencies = flag( + "update-with-dependencies", + Some('w'), + "Allows inherited dependencies to be updated, except those that are root requirements (can also be set via the COMPOSER_WITH_DEPENDENCIES=1 env var).", + ); + let update_with_all_dependencies = flag( + "update-with-all-dependencies", + Some('W'), + "Allows all inherited dependencies to be updated, including those that are root requirements (can also be set via the COMPOSER_WITH_ALL_DEPENDENCIES=1 env var).", + ); + let with_dependencies = flag( + "with-dependencies", + None, + "Alias for --update-with-dependencies", + ); + let with_all_dependencies = flag( + "with-all-dependencies", + None, + "Alias for --update-with-all-dependencies", + ); + let ignore_platform_req = value_many( + "ignore-platform-req", + None, + "Ignore a specific platform requirement (php & ext- packages).", + ); + let ignore_platform_reqs = flag( + "ignore-platform-reqs", + None, + "Ignore all platform requirements (php & ext- packages).", + ); + let prefer_stable = flag( + "prefer-stable", + None, + "Prefer stable versions of dependencies (can also be set via the COMPOSER_PREFER_STABLE=1 env var).", + ); + let prefer_lowest = flag( + "prefer-lowest", + None, + "Prefer lowest versions of dependencies (can also be set via the COMPOSER_PREFER_LOWEST=1 env var).", + ); + let minimal_changes = flag( + "minimal-changes", + Some('m'), + "During an update with -w/-W, only perform absolutely necessary changes to transitive dependencies (can also be set via the COMPOSER_MINIMAL_CHANGES=1 env var).", + ); + let sort_packages = flag( + "sort-packages", + None, + "Sorts packages when adding/updating a new dependency", + ); + let optimize_autoloader = flag( + "optimize-autoloader", + Some('o'), + "Optimize autoloader during autoloader dump", + ); + let classmap_authoritative = flag( + "classmap-authoritative", + Some('a'), + "Autoload classes from the classmap only. Implicitly enables `--optimize-autoloader`.", + ); + let apcu_autoloader = flag( + "apcu-autoloader", + None, + "Use APCu to cache found/not-found classes.", + ); + let apcu_autoloader_prefix = value( + "apcu-autoloader-prefix", + None, + "Use a custom prefix for the APCu autoloader cache. Implicitly enables --apcu-autoloader", + ); + let packages = pos_many( + "packages", + "Optional package name can also include a version constraint, e.g. foo/bar or foo/bar:1.0.0 or foo/bar=1.0.0 or \"foo/bar 1.0.0\"", + ); + construct!(RequireArgs { + dev, + dry_run, + prefer_source, + prefer_dist, + prefer_install, + fixed, + no_suggest, + no_progress, + no_update, + no_install, + no_audit, + audit_format, + no_security_blocking, + update_no_dev, + update_with_dependencies, + update_with_all_dependencies, + with_dependencies, + with_all_dependencies, + ignore_platform_req, + ignore_platform_reqs, + prefer_stable, + prefer_lowest, + minimal_changes, + sort_packages, + optimize_autoloader, + classmap_authoritative, + apcu_autoloader, + apcu_autoloader_prefix, + packages, + }) + .map(Command::Require) + .to_options() + .descr("Adds required packages to your composer.json and installs them") +} + +// run-script (run) +#[derive(Debug, Clone)] +pub struct RunScriptArgs { + pub timeout: Option<String>, + pub dev: bool, + pub no_dev: bool, + pub list: bool, + pub script: Option<String>, + pub args: Vec<String>, +} + +fn run_script_opts() -> OptionParser<Command> { + let timeout = value( + "timeout", + None, + "Sets script timeout in seconds, or 0 for never.", + ); + let dev = flag("dev", None, "Sets the dev mode."); + let no_dev = flag("no-dev", None, "Disables the dev mode."); + let list = flag("list", Some('l'), "List scripts."); + let script = pos_opt("script", "Script name to run."); + let args = pos_many("args", ""); + construct!(RunScriptArgs { + timeout, + dev, + no_dev, + list, + script, + args, + }) + .map(Command::RunScript) + .to_options() + .descr("Runs the scripts defined in composer.json") +} + +// search +#[derive(Debug, Clone)] +pub struct SearchArgs { + pub only_name: bool, + pub only_vendor: bool, + pub r#type: Option<String>, + pub format: String, + pub tokens: Vec<String>, +} + +fn search_opts() -> OptionParser<Command> { + let only_name = flag("only-name", Some('N'), "Search only in package names"); + let only_vendor = flag( + "only-vendor", + Some('O'), + "Search only for vendor / organization names, returns only \"vendor\" as result", + ); + let r#type = value("type", Some('t'), "Search for a specific package type"); + let format = value_default( + "format", + Some('f'), + "Format of the output: text or json", + "text", + ); + let tokens = pos_some("tokens", "tokens to search for"); + construct!(SearchArgs { + only_name, + only_vendor, + r#type, + format, + tokens, + }) + .map(Command::Search) + .to_options() + .descr("Searches for packages") +} + +// self-update (selfupdate) +#[derive(Debug, Clone)] +pub struct SelfUpdateArgs { + pub rollback: bool, + pub clean_backups: bool, + pub no_progress: bool, + pub update_keys: bool, + pub stable: bool, + pub preview: bool, + pub snapshot: bool, + pub v1: bool, + pub v2: bool, + pub v2_2: bool, + pub set_channel_only: bool, + pub version: Option<String>, +} + +fn self_update_opts() -> OptionParser<Command> { + let rollback = flag( + "rollback", + Some('r'), + "Revert to an older installation of composer", + ); + let clean_backups = flag( + "clean-backups", + None, + "Delete old backups during an update. This makes the current version of composer the only backup available after the update", + ); + let no_progress = flag("no-progress", None, "Do not output download progress."); + let update_keys = flag("update-keys", None, "Prompt user for a key update"); + let stable = flag("stable", None, "Force an update to the stable channel"); + let preview = flag("preview", None, "Force an update to the preview channel"); + let snapshot = flag("snapshot", None, "Force an update to the snapshot channel"); + let v1 = flag( + "1", + None, + "Force an update to the stable channel, but only use 1.x versions", + ); + let v2 = flag( + "2", + None, + "Force an update to the stable channel, but only use 2.x versions", + ); + let v2_2 = flag( + "2.2", + None, + "Force an update to the stable channel, but only use 2.2.x LTS versions", + ); + let set_channel_only = flag( + "set-channel-only", + None, + "Only store the channel as the default one and then exit", + ); + let version = pos_opt("version", "The version to update to"); + construct!(SelfUpdateArgs { + rollback, + clean_backups, + no_progress, + update_keys, + stable, + preview, + snapshot, + v1, + v2, + v2_2, + set_channel_only, + version, + }) + .map(Command::SelfUpdate) + .to_options() + .descr("Updates composer.phar to the latest version") +} + +// show (info) +#[derive(Debug, Clone)] +pub struct ShowArgs { + pub all: bool, + pub locked: bool, + pub installed: bool, + pub platform: bool, + pub available: bool, + pub self_: bool, + pub name_only: bool, + pub path: bool, + pub tree: bool, + pub latest: bool, + pub outdated: bool, + pub ignore: Vec<String>, + pub major_only: bool, + pub minor_only: bool, + pub patch_only: bool, + pub sort_by_age: bool, + pub direct: bool, + pub strict: bool, + pub format: String, + pub no_dev: bool, + pub ignore_platform_req: Vec<String>, + pub ignore_platform_reqs: bool, + pub package: Option<String>, + pub version: Option<String>, +} + +fn show_opts() -> OptionParser<Command> { + let all = flag("all", None, "List all packages"); + let locked = flag("locked", None, "List all locked packages"); + let installed = flag( + "installed", + Some('i'), + "List installed packages only (enabled by default, only present for BC).", + ); + let platform = flag("platform", Some('p'), "List platform packages only"); + let available = flag("available", Some('a'), "List available packages only"); + let self_ = flag("self", Some('s'), "Show the root package information"); + let name_only = flag("name-only", Some('N'), "List package names only"); + let path = flag("path", Some('P'), "Show package paths"); + let tree = flag("tree", Some('t'), "List the dependencies as a tree"); + let latest = flag("latest", Some('l'), "Show the latest version"); + let outdated = flag( + "outdated", + Some('o'), + "Show the latest version but only for packages that are outdated", + ); + let ignore = value_many( + "ignore", + None, + "Ignore specified package(s). Can contain wildcards (*). Use it with the --outdated option if you don't want to be informed about new versions of some packages.", + ); + let major_only = flag( + "major-only", + Some('M'), + "Show only packages that have major SemVer-compatible updates. Use with the --latest or --outdated option.", + ); + let minor_only = flag( + "minor-only", + Some('m'), + "Show only packages that have minor SemVer-compatible updates. Use with the --latest or --outdated option.", + ); + let patch_only = flag( + "patch-only", + None, + "Show only packages that have patch SemVer-compatible updates. Use with the --latest or --outdated option.", + ); + let sort_by_age = flag( + "sort-by-age", + Some('A'), + "Displays the installed version's age, and sorts packages oldest first. Use with the --latest or --outdated option.", + ); + let direct = flag( + "direct", + Some('D'), + "Shows only packages that are directly required by the root package", + ); + let strict = flag( + "strict", + None, + "Return a non-zero exit code when there are outdated packages", + ); + let format = value_default( + "format", + Some('f'), + "Format of the output: text or json", + "text", + ); + let no_dev = flag("no-dev", None, "Disables search in require-dev packages."); + let ignore_platform_req = value_many( + "ignore-platform-req", + None, + "Ignore a specific platform requirement (php & ext- packages). Use with the --outdated option", + ); + let ignore_platform_reqs = flag( + "ignore-platform-reqs", + None, + "Ignore all platform requirements (php & ext- packages). Use with the --outdated option", + ); + let package = pos_opt( + "package", + "Package to inspect. Or a name including a wildcard (*) to filter lists of packages instead.", + ); + let version = pos_opt("version", "Version or version constraint to inspect"); + construct!(ShowArgs { + all, + locked, + installed, + platform, + available, + self_, + name_only, + path, + tree, + latest, + outdated, + ignore, + major_only, + minor_only, + patch_only, + sort_by_age, + direct, + strict, + format, + no_dev, + ignore_platform_req, + ignore_platform_reqs, + package, + version, + }) + .map(Command::Show) + .to_options() + .descr("Shows information about packages") +} + +// status +// TODO(phase-c): `verbose` is unified into the global verbosity option (see +// GlobalOptions). Kept commented out because dropping the command-local +// definition changes `--help` output and must be reconciled with Symfony's +// merged InputDefinition. Once restored, `Command::Status` should carry a +// `StatusArgs` payload again. +// #[derive(Debug, Clone)] +// pub struct StatusArgs { +// pub verbose: bool, +// } + +fn status_opts() -> OptionParser<Command> { + // let verbose = flag( + // "verbose", + // Some('v'), + // "Show modified files for each directory that contains changes.", + // ); + pure(Command::Status) + .to_options() + .descr("Shows a list of locally modified packages") +} + +// suggests +#[derive(Debug, Clone)] +pub struct SuggestsArgs { + pub by_package: bool, + pub by_suggestion: bool, + pub all: bool, + pub list: bool, + pub no_dev: bool, + pub packages: Vec<String>, +} + +fn suggests_opts() -> OptionParser<Command> { + let by_package = flag( + "by-package", + None, + "Groups output by suggesting package (default)", + ); + let by_suggestion = flag("by-suggestion", None, "Groups output by suggested package"); + let all = flag( + "all", + Some('a'), + "Show suggestions from all dependencies, including transitive ones", + ); + let list = flag("list", None, "Show only list of suggested package names"); + let no_dev = flag( + "no-dev", + None, + "Exclude suggestions from require-dev packages", + ); + let packages = pos_many( + "packages", + "Packages that you want to list suggestions from.", + ); + construct!(SuggestsArgs { + by_package, + by_suggestion, + all, + list, + no_dev, + packages, + }) + .map(Command::Suggests) + .to_options() + .descr("Shows package suggestions") +} + +// update (u, upgrade) +/// VALUE_OPTIONAL representation for `--bump-after-update`: the flag may be +/// absent, present without a value, or present with a value (only the `=value` +/// form provides a value, matching Symfony's optional-value semantics). +#[derive(Debug, Clone)] +pub enum BumpAfterUpdate { + Absent, + Present, + Value(String), +} + +#[derive(Debug, Clone)] +pub struct UpdateArgs { + pub with: Vec<String>, + pub prefer_source: bool, + pub prefer_dist: bool, + pub prefer_install: Option<String>, + pub dry_run: bool, + pub dev: bool, + pub no_dev: bool, + pub lock: bool, + pub no_install: bool, + pub no_audit: bool, + pub audit_format: String, + pub no_security_blocking: bool, + pub no_autoloader: bool, + pub no_suggest: bool, + pub no_progress: bool, + pub with_dependencies: bool, + pub with_all_dependencies: bool, + pub optimize_autoloader: bool, + pub classmap_authoritative: bool, + pub apcu_autoloader: bool, + pub apcu_autoloader_prefix: Option<String>, + pub ignore_platform_req: Vec<String>, + pub ignore_platform_reqs: bool, + pub prefer_stable: bool, + pub prefer_lowest: bool, + pub minimal_changes: bool, + pub patch_only: bool, + pub interactive: bool, + pub root_reqs: bool, + pub bump_after_update: BumpAfterUpdate, + pub packages: Vec<String>, +} + +fn update_opts() -> OptionParser<Command> { + let with = value_many( + "with", + None, + "Temporary version constraint to add, e.g. foo/bar:1.0.0 or foo/bar=1.0.0", + ); + let prefer_source = flag( + "prefer-source", + None, + "Forces installation from package sources when possible, including VCS information.", + ); + let prefer_dist = flag( + "prefer-dist", + None, + "Forces installation from package dist (default behavior).", + ); + let prefer_install = value( + "prefer-install", + None, + "Forces installation from package dist|source|auto (auto chooses source for dev versions, dist for the rest).", + ); + let dry_run = flag( + "dry-run", + None, + "Outputs the operations but will not execute anything (implicitly enables --verbose).", + ); + let dev = flag( + "dev", + None, + "DEPRECATED: Enables installation of require-dev packages (enabled by default, only present for BC).", + ); + let no_dev = flag( + "no-dev", + None, + "Disables installation of require-dev packages.", + ); + let lock = flag( + "lock", + None, + "Overwrites the lock file hash to suppress warning about the lock file being out of date without updating package versions. Package metadata like mirrors and URLs are updated if they changed.", + ); + let no_install = flag( + "no-install", + None, + "Skip the install step after updating the composer.lock file.", + ); + let no_audit = flag( + "no-audit", + None, + "Skip the audit step after updating the composer.lock file (can also be set via the COMPOSER_NO_AUDIT=1 env var).", + ); + let audit_format = value_default( + "audit-format", + None, + "Audit output format. Must be \"table\", \"plain\", \"json\", or \"summary\".", + "summary", + ); + let no_security_blocking = flag( + "no-security-blocking", + None, + "Allows installing packages with security advisories or that are abandoned (can also be set via the COMPOSER_NO_SECURITY_BLOCKING=1 env var).", + ); + let no_autoloader = flag("no-autoloader", None, "Skips autoloader generation"); + let no_suggest = flag( + "no-suggest", + None, + "DEPRECATED: This flag does not exist anymore.", + ); + let no_progress = flag("no-progress", None, "Do not output download progress."); + let with_dependencies = flag( + "with-dependencies", + Some('w'), + "Update also dependencies of packages in the argument list, except those which are root requirements (can also be set via the COMPOSER_WITH_DEPENDENCIES=1 env var).", + ); + let with_all_dependencies = flag( + "with-all-dependencies", + Some('W'), + "Update also dependencies of packages in the argument list, including those which are root requirements (can also be set via the COMPOSER_WITH_ALL_DEPENDENCIES=1 env var).", + ); + // TODO(phase-c): `verbose` (-v|-vv|-vvv) is unified into the global verbosity + // option; kept commented out because dropping it changes `--help` output and + // must be reconciled with Symfony's merged InputDefinition. + // let verbose = flag( + // "verbose", + // Some('v'), + // "Shows more details including new commits pulled in when updating packages.", + // ); + let optimize_autoloader = flag( + "optimize-autoloader", + Some('o'), + "Optimize autoloader during autoloader dump.", + ); + let classmap_authoritative = flag( + "classmap-authoritative", + Some('a'), + "Autoload classes from the classmap only. Implicitly enables `--optimize-autoloader`.", + ); + let apcu_autoloader = flag( + "apcu-autoloader", + None, + "Use APCu to cache found/not-found classes.", + ); + let apcu_autoloader_prefix = value( + "apcu-autoloader-prefix", + None, + "Use a custom prefix for the APCu autoloader cache. Implicitly enables --apcu-autoloader", + ); + let ignore_platform_req = value_many( + "ignore-platform-req", + None, + "Ignore a specific platform requirement (php & ext- packages).", + ); + let ignore_platform_reqs = flag( + "ignore-platform-reqs", + None, + "Ignore all platform requirements (php & ext- packages).", + ); + let prefer_stable = flag( + "prefer-stable", + None, + "Prefer stable versions of dependencies (can also be set via the COMPOSER_PREFER_STABLE=1 env var).", + ); + let prefer_lowest = flag( + "prefer-lowest", + None, + "Prefer lowest versions of dependencies (can also be set via the COMPOSER_PREFER_LOWEST=1 env var).", + ); + let minimal_changes = flag( + "minimal-changes", + Some('m'), + "Only perform absolutely necessary changes to dependencies. If packages cannot be kept at their currently locked version they are updated. For partial updates the allow-listed packages are always updated fully. (can also be set via the COMPOSER_MINIMAL_CHANGES=1 env var).", + ); + let patch_only = flag( + "patch-only", + None, + "Only allow patch version updates for currently installed dependencies.", + ); + let interactive = flag( + "interactive", + Some('i'), + "Interactive interface with autocompletion to select the packages to update.", + ); + let root_reqs = flag( + "root-reqs", + None, + "Restricts the update to your first degree dependencies.", + ); + // VALUE_OPTIONAL with default `false`: only the `--bump-after-update=value` + // form supplies a value; the bare flag means "present without value". + let bump_after_update = { + let with_value = long("bump-after-update") + .help("Runs bump after performing the update.") + .argument::<String>("MODE") + .adjacent() + .map(BumpAfterUpdate::Value); + let bare = long("bump-after-update").req_flag(BumpAfterUpdate::Present); + construct!([with_value, bare]).fallback(BumpAfterUpdate::Absent) + }; + let packages = pos_many( + "packages", + "Packages that should be updated, if not provided all packages are.", + ); + construct!(UpdateArgs { + with, + prefer_source, + prefer_dist, + prefer_install, + dry_run, + dev, + no_dev, + lock, + no_install, + no_audit, + audit_format, + no_security_blocking, + no_autoloader, + no_suggest, + no_progress, + with_dependencies, + with_all_dependencies, + optimize_autoloader, + classmap_authoritative, + apcu_autoloader, + apcu_autoloader_prefix, + ignore_platform_req, + ignore_platform_reqs, + prefer_stable, + prefer_lowest, + minimal_changes, + patch_only, + interactive, + root_reqs, + bump_after_update, + packages, + }) + .map(Command::Update) + .to_options() + .descr("Updates your dependencies to the latest version according to composer.json, and updates the composer.lock file") +} + +// validate +#[derive(Debug, Clone)] +pub struct ValidateArgs { + pub no_check_all: bool, + pub check_lock: bool, + pub no_check_lock: bool, + pub no_check_publish: bool, + pub no_check_version: bool, + pub with_dependencies: bool, + pub strict: bool, + pub file: Option<String>, +} + +fn validate_opts() -> OptionParser<Command> { + let no_check_all = flag( + "no-check-all", + None, + "Do not validate requires for overly strict/loose constraints", + ); + let check_lock = flag( + "check-lock", + None, + "Check if lock file is up to date (even when config.lock is false)", + ); + let no_check_lock = flag( + "no-check-lock", + None, + "Do not check if lock file is up to date", + ); + let no_check_publish = flag("no-check-publish", None, "Do not check for publish errors"); + let no_check_version = flag( + "no-check-version", + None, + "Do not report a warning if the version field is present", + ); + let with_dependencies = flag( + "with-dependencies", + Some('A'), + "Also validate the composer.json of all installed dependencies", + ); + let strict = flag( + "strict", + None, + "Return a non-zero exit code for warnings as well as errors", + ); + let file = pos_opt("file", "path to composer.json file"); + construct!(ValidateArgs { + no_check_all, + check_lock, + no_check_lock, + no_check_publish, + no_check_version, + with_dependencies, + strict, + file, + }) + .map(Command::Validate) + .to_options() + .descr("Validates a composer.json and composer.lock") +} + +// TODO(phase-c): ScriptAliasCommand is constructed dynamically from composer.json +// `scripts`; it cannot be a static subcommand and must be registered at runtime +// once composer.json is loaded. + +fn sub( + name: &'static str, + aliases: &[&'static str], + make: fn() -> OptionParser<Command>, +) -> Box<dyn Parser<Command>> { + let mut acc: Box<dyn Parser<Command>> = bpaf::command(name, make()).boxed(); + for a in aliases { + acc = acc.or_else(bpaf::command(*a, make())).boxed(); + } + acc +} + +fn commands() -> Box<dyn Parser<Command>> { + let mut subs: Vec<Box<dyn Parser<Command>>> = vec![ + sub("about", &[], about_opts), + sub("archive", &[], archive_opts), + sub("audit", &[], audit_opts), + sub("bump", &[], bump_opts), + sub("check-platform-reqs", &[], check_platform_reqs_opts), + sub("clear-cache", &["clearcache", "cc"], clear_cache_opts), + sub("config", &[], config_opts), + sub("create-project", &[], create_project_opts), + sub("depends", &["why"], depends_opts), + sub("diagnose", &[], diagnose_opts), + sub("dump-autoload", &["dumpautoload"], dump_autoload_opts), + sub("exec", &[], exec_opts), + sub("fund", &[], fund_opts), + sub("global", &[], global_opts), + sub("browse", &["home"], browse_opts), + sub("init", &[], init_opts), + sub("install", &["i"], install_opts), + sub("licenses", &[], licenses_opts), + sub("outdated", &[], outdated_opts), + sub("prohibits", &["why-not"], prohibits_opts), + sub("reinstall", &[], reinstall_opts), + sub("remove", &["rm", "uninstall"], remove_opts), + sub("repository", &["repo"], repository_opts), + sub("require", &["r"], require_opts), + sub("run-script", &["run"], run_script_opts), + sub("search", &[], search_opts), + sub("self-update", &["selfupdate"], self_update_opts), + sub("show", &["info"], show_opts), + sub("status", &[], status_opts), + sub("suggests", &[], suggests_opts), + sub("update", &["u", "upgrade"], update_opts), + sub("validate", &[], validate_opts), + ]; + let mut acc = subs.remove(0); + for s in subs { + acc = acc.or_else(s).boxed(); + } + acc +} + +#[derive(Debug, Clone)] +pub struct Cli { + pub global: GlobalOptions, + pub command: Command, +} + +pub fn cli() -> OptionParser<Cli> { + let global = global_options(); + let command = commands(); + construct!(Cli { global, command }) + .to_options() + .descr("Composer (Shirabe)") +} diff --git a/crates/shirabe/src/lib.rs b/crates/shirabe/src/lib.rs index 9fb9f5f..78ea009 100644 --- a/crates/shirabe/src/lib.rs +++ b/crates/shirabe/src/lib.rs @@ -1,6 +1,7 @@ pub mod advisory; pub mod autoload; pub mod cache; +pub mod cli; pub mod command; pub mod compiler; pub mod composer; diff --git a/crates/shirabe/src/main.rs b/crates/shirabe/src/main.rs index 0672e51..597c30a 100644 --- a/crates/shirabe/src/main.rs +++ b/crates/shirabe/src/main.rs @@ -1,3 +1,8 @@ +use shirabe::cli; + fn main() { - println!("Hello, World!"); + let parsed = cli::cli().run(); + // TODO(phase-b): bridge the parsed CLI into Symfony Console's Input and + // dispatch through Application::run. For now we only parse. + let _ = parsed; } |
