Skip to content

FilmCraft: An open-source, nonlinear video editor written by scratch in pure Rust

#710: --version works after global options

vangel76bc5965e42b52
diff --git a/apps/filmcraft-cli/src/args.rs b/apps/filmcraft-cli/src/args.rs
index bac68febb..af531c50c 100644
--- a/apps/filmcraft-cli/src/args.rs
+++ b/apps/filmcraft-cli/src/args.rs
@@ -60,6 +60,12 @@ impl Args {
self.opts.iter().any(|(n, _)| n == k)
}
+ /// A version query: `--version` anywhere, or `-V` in the subcommand position, so global
+ /// options may come first (`--demo --version`, `--compact -V`).
+ pub fn wants_version(&self) -> bool {
+ self.flag("--version") || self.pos(0) == Some("-V")
+ }
+
/// Command params from positionals `from..`: one JSON object, or `key=value` pairs
/// (dotted keys nest: `color.r=1`).
pub fn params_from(&self, from: usize) -> Result<Value, String> {
@@ -135,6 +141,16 @@ mod tests {
assert_eq!(a.params_from(2).unwrap(), json!({"a": {"b": [1]}}));
}
+ #[test]
+ fn version_after_global_options() {
+ for s in ["--version", "-V", "--demo --version", "--compact -V", "--data-dir d --version", "export --version"] {
+ assert!(args(s).wants_version(), "{s}");
+ }
+ for s in ["version", "--data-dir -V", "exec x -V", "-- --version"] {
+ assert!(!args(s).wants_version(), "{s}");
+ }
+ }
+
#[test]
fn double_dash_ends_options() {
let a = args("import -- --weird-name.mov a.mov");
diff --git a/apps/filmcraft-cli/src/main.rs b/apps/filmcraft-cli/src/main.rs
index b051d6cbf..2d54c4923 100644
--- a/apps/filmcraft-cli/src/main.rs
+++ b/apps/filmcraft-cli/src/main.rs
@@ -252,12 +252,6 @@ async fn main() {
}
async fn cli() {
- if matches!(std::env::args().nth(1).as_deref(), Some("--version" | "-V")) {
- outln!("filmcraft-cli {}", env!("CARGO_PKG_VERSION"));
- return;
- }
- register_hardware_decoders();
- register_gpu_frame_renderer();
let a = Args::parse(std::env::args().skip(1));
// `--help` anywhere (`filmcraft-cli --help`, `filmcraft-cli export --help`) prints the reference:
// the parser takes it as a flag option, so it never reaches the subcommand match.
@@ -265,6 +259,13 @@ async fn cli() {
out!("{HELP}");
return;
}
+ // `--version` anywhere and `-V` as the subcommand, also after global options (`--demo --version`, #710).
+ if a.wants_version() {
+ outln!("filmcraft-cli {}", env!("CARGO_PKG_VERSION"));
+ return;
+ }
+ register_hardware_decoders();
+ register_gpu_frame_renderer();
let Some(cmd) = a.pos(0) else { usage("missing subcommand") };
match cmd {
"help" | "-h" => out!("{HELP}"),
diff --git a/apps/filmcraft-cli/tests/cli.rs b/apps/filmcraft-cli/tests/cli.rs
index 2736806e5..8552cc640 100644
--- a/apps/filmcraft-cli/tests/cli.rs
+++ b/apps/filmcraft-cli/tests/cli.rs
@@ -24,6 +24,16 @@ fn help_and_usage_errors() {
assert_eq!(cli(&["exec", "file.newBin", "notkv"]).status.code(), Some(2));
}
+#[test]
+fn version_after_global_options() {
+ let want = format!("filmcraft-cli {}\n", env!("CARGO_PKG_VERSION"));
+ for args in [&["--version"][..], &["-V"], &["version"], &["--demo", "--version"], &["--compact", "-V"], &["--demo", "version"]] {
+ let o = cli(args);
+ assert!(o.status.success(), "{args:?}: {}", String::from_utf8_lossy(&o.stderr));
+ assert_eq!(String::from_utf8_lossy(&o.stdout), want, "{args:?}");
+ }
+}
+
#[test]
fn long_help_matches_help_command() {
let reference = cli(&["help"]);