-
-
Notifications
You must be signed in to change notification settings - Fork 49
feat(spec): allow a mount at the top level #806
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from 2 commits
7e04c4c
d67252d
9eb998b
1e79a08
29e81b5
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
|
@@ -592,8 +592,31 @@ fn parse_partial_with_env( | |||||||||||||||||||||||||||||||||||||||||||||||||||||||
| // Track whether we've already applied the default_subcommand to prevent | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| // multiple switches (e.g., if default is "run" and there's a task named "run") | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| let mut used_default_subcommand = false; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| // Whether the command in scope has had its own mounts run. A mount on the root | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| // is the case that needs this: a subcommand's mounts are run when the parser | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| // descends into it, but nothing descends into the root. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| let mut mounts_resolved = false; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| while idx < input.len() { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| // Only for a word that could name a command, and only when it matches | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| // nothing already declared. A CLI that declares its commands and mounts more | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| // does not spawn a process for every invocation, and a flag — `--help`, or | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| // anything unrecognized — never triggers discovery at all, which it would | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| // otherwise do simply by not being a subcommand. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| if !mounts_resolved | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| && !out.cmd.mounts.is_empty() | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| && !input[idx].starts_with('-') | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| && out.cmd.find_subcommand(&input[idx]).is_none() | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| mounts_resolved = true; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| let mut mounted = out.cmd.clone(); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| mounted.mount(&mount_prefix_words(&prefix_flags))?; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
greptile-apps[bot] marked this conversation as resolved.
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| merge_subcommand_flags(&mut out.available_flags, gather_flags(&mounted), false); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Comment on lines
+651
to
+662
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 🎯 Functional Correctness | 🟠 Major | ⚡ Quick win Run only overriding mounts before default fallback.
When a default exists, execute only mounts with Proposed fix let mut mounted = out.cmd.clone();
+if spec.default_subcommand.is_some() {
+ mounted.mounts.retain(|mount| mount.overrides_default);
+}
mounted.mount(&mount_prefix_words(&prefix_flags))?;📝 Committable suggestion
Suggested change
🤖 Prompt for AI Agents |
||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| if let Some(last) = out.cmds.last_mut() { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| *last = mounted.clone(); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| out.cmd = mounted; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
coderabbitai[bot] marked this conversation as resolved.
cursor[bot] marked this conversation as resolved.
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| if let Some(subcommand) = out.cmd.find_subcommand(&input[idx]) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| let mut subcommand = subcommand.clone(); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| // Pass prefix words (global flags before this subcommand) to mount | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
@@ -610,6 +633,8 @@ fn parse_partial_with_env( | |||||||||||||||||||||||||||||||||||||||||||||||||||||||
| input.remove(idx); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| out.cmds.push(subcommand.clone()); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| out.cmd = subcommand.clone(); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| // A descent already ran the new command's mounts, above. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| mounts_resolved = true; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
cursor[bot] marked this conversation as resolved.
coderabbitai[bot] marked this conversation as resolved.
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| prefix_flags.clear(); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| // Continue from current position (don't reset to 0) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| // After remove(), idx now points to the next element | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
@@ -670,6 +695,9 @@ fn parse_partial_with_env( | |||||||||||||||||||||||||||||||||||||||||||||||||||||||
| out.cmds.push(subcommand.clone()); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| out.cmd = subcommand.clone(); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| prefix_flags.clear(); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| // This descent ran the new command's mounts, so lazy | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| // discovery must not run them a second time. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| mounts_resolved = true; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| used_default_subcommand = true; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| // Continue the loop to check if this word is a subcommand of the | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| // default subcommand (e.g., a task name added via mount). | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
@@ -1524,6 +1552,76 @@ arg "[input]" | |||||||||||||||||||||||||||||||||||||||||||||||||||||||
| assert_eq!(first_string_value(&parsed), "input.txt"); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| #[cfg(unix)] | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| #[test] | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| fn a_mount_on_the_root_discovers_subcommands() { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| // The root is a command like any other, so it can find its own subcommands | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| // by running something. Uses `echo` rather than a fixture because resolving | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| // a mount is what is being tested. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| let spec: Spec = r#" | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| name "ex" | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| bin "ex" | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| cmd "declared" | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| mount run="echo 'cmd \"discovered\"'" | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| "# | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| .parse() | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| .unwrap(); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| let out = parse(&spec, &["ex".to_string(), "discovered".to_string()]).unwrap(); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| assert_eq!(out.cmd.name, "discovered"); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| #[cfg(unix)] | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| #[test] | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| fn a_flag_does_not_run_the_mount() { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| // A flag matches no subcommand, which would have been enough to trigger | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| // discovery — so `ex --help` spawned a process. The mount fails if it runs, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| // so parsing at all is the proof that it did not. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| let spec: Spec = r#" | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| name "ex" | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| bin "ex" | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| flag "--verbose" | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| cmd "declared" | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| mount run="exit 1" | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| "# | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| .parse() | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| .unwrap(); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| let out = parse(&spec, &["ex".to_string(), "--verbose".to_string()]).unwrap(); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| assert_eq!(out.cmd.name, "ex"); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| #[cfg(unix)] | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| #[test] | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| fn a_declared_subcommand_does_not_run_the_mount() { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| // The mount would fail if it ran, so this parsing at all is the proof that | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| // discovery is skipped when the word is already known. Worth pinning: a root | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| // mount that resolved eagerly would spawn a process on every invocation. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| let spec: Spec = r#" | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| name "ex" | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| bin "ex" | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| cmd "declared" | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| mount run="exit 1" | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| "# | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| .parse() | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| .unwrap(); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| let out = parse(&spec, &["ex".to_string(), "declared".to_string()]).unwrap(); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| assert_eq!(out.cmd.name, "declared"); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| #[test] | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| fn a_root_mount_survives_being_written_out() { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| let spec: Spec = "name \"ex\"\nbin \"ex\"\nmount run=\"ex plugins --usage\"\n" | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| .parse() | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| .unwrap(); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| assert_eq!(spec.cmd.mounts.len(), 1); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| let reparsed: Spec = spec.to_string().parse().unwrap(); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| assert_eq!(reparsed.cmd.mounts.len(), 1, "written:\n{spec}"); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| assert_eq!(reparsed.cmd.mounts[0].run, "ex plugins --usage"); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| #[test] | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| fn test_mount_prefix_applies_flag_overrides() { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| let stdin = Arc::new( | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||
Uh oh!
There was an error while loading. Please reload this page.