Adds initial usage support
authorYehuda Katz <wycats@gmail.com>
Sat, 21 Jun 2014 23:15:31 +0000 (16:15 -0700)
committerYehuda Katz <wycats@gmail.com>
Sat, 21 Jun 2014 23:15:31 +0000 (16:15 -0700)
libs/hammer.rs
src/bin/cargo-compile.rs
src/bin/cargo-git-checkout.rs
src/bin/cargo-read-manifest.rs
src/cargo/lib.rs

index 9af78f5b1fdfd09580758ec3b559c7704e6a5382..6d26b74a66ce16f9d146a407a2384aa6ef431f7c 160000 (submodule)
@@ -1 +1 @@
-Subproject commit 9af78f5b1fdfd09580758ec3b559c7704e6a5382
+Subproject commit 6d26b74a66ce16f9d146a407a2384aa6ef431f7c
index 2c46d177893a2dd154c9bf4ef7e99f4b4c2efc0b..c4cff306e634db1086fa9b3bcf44cee185aee552 100644 (file)
@@ -2,14 +2,16 @@
 #![feature(phase)]
 
 extern crate cargo;
+
+#[phase(plugin, link)]
 extern crate hammer;
-extern crate serialize;
 
 #[phase(plugin, link)]
 extern crate log;
 
+extern crate serialize;
+
 use std::os;
-use hammer::FlagConfig;
 use cargo::{execute_main_without_stdin};
 use cargo::ops;
 use cargo::util::{CliResult, CliError};
@@ -20,7 +22,7 @@ pub struct Options {
     manifest_path: Option<String>
 }
 
-impl FlagConfig for Options {}
+hammer_config!(Options)
 
 fn main() {
     execute_main_without_stdin(execute);
index f72b0d56b67b651ebf28e803c2f09a7cbbdb4dc8..a9a7698747846e76103b8834345df80618277483 100644 (file)
@@ -1,11 +1,13 @@
 #![crate_id="cargo-git-checkout"]
+#![feature(phase)]
 
 extern crate cargo;
 extern crate serialize;
-extern crate hammer;
 extern crate url;
 
-use hammer::FlagConfig;
+#[phase(plugin, link)]
+extern crate hammer;
+
 use cargo::{execute_main_without_stdin};
 use cargo::core::source::{Source,SourceId};
 use cargo::sources::git::{GitSource};
@@ -15,11 +17,10 @@ use url::Url;
 #[deriving(PartialEq,Clone,Decodable)]
 struct Options {
     url: String,
-    reference: String,
-    verbose: bool
+    reference: String
 }
 
-impl FlagConfig for Options {}
+hammer_config!(Options)
 
 fn main() {
     execute_main_without_stdin(execute);
index af9c904e93375f5ab90f3ab6133253260a7bcd4a..9772a1275fe0f42cf2fe273b5750e22fab79b927 100644 (file)
@@ -1,10 +1,12 @@
 #![crate_id="cargo-read-manifest"]
+#![feature(phase)]
 
 extern crate cargo;
 extern crate serialize;
+
+#[phase(plugin, link)]
 extern crate hammer;
 
-use hammer::FlagConfig;
 use cargo::{execute_main_without_stdin};
 use cargo::core::{Package, Source, SourceId};
 use cargo::util::{CliResult, CliError};
@@ -15,7 +17,7 @@ struct Options {
     manifest_path: String
 }
 
-impl FlagConfig for Options {}
+hammer_config!(Options)
 
 fn main() {
     execute_main_without_stdin(execute);
index 96651618ab798c0e563499df222028f25d54cff3..f2ed95c1b8883183087c459e52e87e8d3afbf478 100644 (file)
@@ -8,9 +8,11 @@ extern crate term;
 extern crate url;
 extern crate serialize;
 extern crate semver;
-extern crate hammer;
 extern crate toml = "github.com/mneumann/rust-toml#toml";
 
+#[phase(plugin, link)]
+extern crate hammer;
+
 #[phase(plugin, link)]
 extern crate log;
 
@@ -19,7 +21,7 @@ extern crate hamcrest;
 
 use serialize::{Decoder, Encoder, Decodable, Encodable, json};
 use std::io;
-use hammer::{FlagDecoder, FlagConfig, HammerError, FlagConfiguration};
+use hammer::{FlagDecoder, FlagConfig, UsageDecoder, HammerError, FlagConfiguration};
 
 pub use util::{CargoError, CliError, CliResult, human};
 
@@ -56,28 +58,50 @@ pub mod ops;
 pub mod sources;
 pub mod util;
 
-trait RepresentsFlags : FlagConfig + Decodable<FlagDecoder, HammerError> {}
-impl<T: FlagConfig + Decodable<FlagDecoder, HammerError>> RepresentsFlags for T {}
+trait FlagParse : FlagConfig {
+    fn decode_flags(d: &mut FlagDecoder) -> Result<Self, HammerError>;
+}
+
+trait FlagUsage : FlagConfig {
+    fn decode_usage(d: &mut UsageDecoder) -> Result<Self, HammerError>;
+}
+
+//impl<T: FlagConfig + Decodable<FlagDecoder, HammerError>> FlagParse for T {}
+//impl<T: FlagConfig + Decodable<UsageDecoder, HammerError>> FlagUsage for T {}
+
+impl<T: FlagConfig + Decodable<FlagDecoder, HammerError>> FlagParse for T {
+    fn decode_flags(d: &mut FlagDecoder) -> Result<T, HammerError> {
+        Decodable::decode(d)
+    }
+}
+
+impl<T: FlagConfig + Decodable<UsageDecoder, HammerError>> FlagUsage for T {
+    fn decode_usage(d: &mut UsageDecoder) -> Result<T, HammerError> {
+        Decodable::decode(d)
+    }
+}
+
+trait RepresentsFlags : FlagParse + Decodable<UsageDecoder, HammerError> {}
+impl<T: FlagParse + Decodable<UsageDecoder, HammerError>> RepresentsFlags for T {}
 
 trait RepresentsJSON : Decodable<json::Decoder, json::DecoderError> {}
-impl <T: Decodable<json::Decoder, json::DecoderError>> RepresentsJSON for T {}
+impl<T: Decodable<json::Decoder, json::DecoderError>> RepresentsJSON for T {}
 
 #[deriving(Decodable)]
 pub struct NoFlags;
 
-impl FlagConfig for NoFlags {}
+hammer_config!(NoFlags)
 
 #[deriving(Decodable)]
 pub struct GlobalFlags {
     verbose: bool,
+    help: bool,
     rest: Vec<String>
 }
 
-impl FlagConfig for GlobalFlags {
-    fn config(_: Option<GlobalFlags>, c: FlagConfiguration) -> FlagConfiguration {
-        c.short("verbose", 'v')
-    }
-}
+hammer_config!(GlobalFlags |c| {
+    c.short("verbose", 'v').short("help", 'h')
+})
 
 pub fn execute_main<'a,
                     T: RepresentsFlags,
@@ -124,7 +148,16 @@ pub fn execute_main_without_stdin<'a,
 
     match global_flags() {
         Err(e) => handle_error(e, true),
-        Ok(val) => process_executed(call(exec, val.rest.as_slice()), val)
+        Ok(val) => {
+            if val.help {
+                println!("Usage:\n");
+
+                print!("{}", hammer::usage::<T>(true));
+                print!("{}", hammer::usage::<GlobalFlags>(false));
+            } else {
+                process_executed(call(exec, val.rest.as_slice()), val)
+            }
+        }
     }
 }
 
@@ -170,7 +203,7 @@ fn args() -> Vec<String> {
 
 fn flags_from_args<T: RepresentsFlags>(args: &[String]) -> CliResult<T> {
     let mut decoder = FlagDecoder::new::<T>(args);
-    Decodable::decode(&mut decoder).map_err(|e: HammerError| {
+    FlagParse::decode_flags(&mut decoder).map_err(|e: HammerError| {
         CliError::new(e.message, 1)
     })
 }