Make read-manifest accept consistent manifest-path arguments
authorCarol (Nichols || Goulding) <carol.nichols@gmail.com>
Sat, 29 Aug 2015 17:03:05 +0000 (13:03 -0400)
committerCarol (Nichols || Goulding) <carol.nichols@gmail.com>
Tue, 1 Sep 2015 01:26:29 +0000 (21:26 -0400)
And also preserve the existing behavior of accepting an absolute path to
a directory containing a Cargo.toml for backwards compatibility.

src/bin/read_manifest.rs

index 591d9f42ff0ca4eec066ef784fadb635e50ce13f..9c584fa2c3319d9e7b6eb954d195b77c61a75620 100644 (file)
@@ -1,31 +1,51 @@
-use std::path::Path;
+use std::env;
 use std::error::Error;
+use std::path::PathBuf;
 
 use cargo::core::{Package, Source};
 use cargo::util::{CliResult, CliError, Config};
+use cargo::util::important_paths::{find_root_manifest_for_cwd};
 use cargo::sources::{PathSource};
 
 #[derive(RustcDecodable)]
 struct Options {
-    flag_manifest_path: String,
+    flag_manifest_path: Option<String>,
     flag_color: Option<String>,
 }
 
 pub const USAGE: &'static str = "
 Usage:
-    cargo read-manifest [options] --manifest-path=PATH
+    cargo read-manifest [options]
     cargo read-manifest -h | --help
 
 Options:
     -h, --help               Print this message
     -v, --verbose            Use verbose output
+    --manifest-path PATH     Path to the manifest to compile
     --color WHEN             Coloring: auto, always, never
 ";
 
 pub fn execute(options: Options, config: &Config) -> CliResult<Option<Package>> {
+    debug!("executing; cmd=cargo-read-manifest; args={:?}",
+           env::args().collect::<Vec<_>>());
     try!(config.shell().set_color_config(options.flag_color.as_ref().map(|s| &s[..])));
-    let path = Path::new(&options.flag_manifest_path);
-    let mut source = try!(PathSource::for_path(&path, config).map_err(|e| {
+
+    // Accept paths to directories containing Cargo.toml for backwards compatibility.
+    let root = match options.flag_manifest_path {
+        Some(path) => {
+            let mut path = PathBuf::from(path);
+            if !path.ends_with("Cargo.toml") {
+                path.push("Cargo.toml");
+            }
+            Some(path.display().to_string())
+        },
+        None => None,
+    };
+    let root = try!(find_root_manifest_for_cwd(root));
+
+    debug!("read-manifest; manifest-path={}", root.display());
+
+    let mut source = try!(PathSource::for_path(root.parent().unwrap(), config).map_err(|e| {
         CliError::new(e.description(), 1)
     }));