From 4ac45e6830456f02999f71af53c241237c04a427 Mon Sep 17 00:00:00 2001 From: Yehuda Katz Date: Sat, 21 Jun 2014 16:15:31 -0700 Subject: [PATCH] Adds initial usage support --- libs/hammer.rs | 2 +- src/bin/cargo-compile.rs | 8 +++-- src/bin/cargo-git-checkout.rs | 11 ++++--- src/bin/cargo-read-manifest.rs | 6 ++-- src/cargo/lib.rs | 59 ++++++++++++++++++++++++++-------- 5 files changed, 62 insertions(+), 24 deletions(-) diff --git a/libs/hammer.rs b/libs/hammer.rs index 9af78f5b1..6d26b74a6 160000 --- a/libs/hammer.rs +++ b/libs/hammer.rs @@ -1 +1 @@ -Subproject commit 9af78f5b1fdfd09580758ec3b559c7704e6a5382 +Subproject commit 6d26b74a66ce16f9d146a407a2384aa6ef431f7c diff --git a/src/bin/cargo-compile.rs b/src/bin/cargo-compile.rs index 2c46d1778..c4cff306e 100644 --- a/src/bin/cargo-compile.rs +++ b/src/bin/cargo-compile.rs @@ -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 } -impl FlagConfig for Options {} +hammer_config!(Options) fn main() { execute_main_without_stdin(execute); diff --git a/src/bin/cargo-git-checkout.rs b/src/bin/cargo-git-checkout.rs index f72b0d56b..a9a769874 100644 --- a/src/bin/cargo-git-checkout.rs +++ b/src/bin/cargo-git-checkout.rs @@ -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); diff --git a/src/bin/cargo-read-manifest.rs b/src/bin/cargo-read-manifest.rs index af9c904e9..9772a1275 100644 --- a/src/bin/cargo-read-manifest.rs +++ b/src/bin/cargo-read-manifest.rs @@ -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); diff --git a/src/cargo/lib.rs b/src/cargo/lib.rs index 96651618a..f2ed95c1b 100644 --- a/src/cargo/lib.rs +++ b/src/cargo/lib.rs @@ -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 {} -impl> RepresentsFlags for T {} +trait FlagParse : FlagConfig { + fn decode_flags(d: &mut FlagDecoder) -> Result; +} + +trait FlagUsage : FlagConfig { + fn decode_usage(d: &mut UsageDecoder) -> Result; +} + +//impl> FlagParse for T {} +//impl> FlagUsage for T {} + +impl> FlagParse for T { + fn decode_flags(d: &mut FlagDecoder) -> Result { + Decodable::decode(d) + } +} + +impl> FlagUsage for T { + fn decode_usage(d: &mut UsageDecoder) -> Result { + Decodable::decode(d) + } +} + +trait RepresentsFlags : FlagParse + Decodable {} +impl> RepresentsFlags for T {} trait RepresentsJSON : Decodable {} -impl > RepresentsJSON for T {} +impl> 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 } -impl FlagConfig for GlobalFlags { - fn config(_: Option, 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::(true)); + print!("{}", hammer::usage::(false)); + } else { + process_executed(call(exec, val.rest.as_slice()), val) + } + } } } @@ -170,7 +203,7 @@ fn args() -> Vec { fn flags_from_args(args: &[String]) -> CliResult { let mut decoder = FlagDecoder::new::(args); - Decodable::decode(&mut decoder).map_err(|e: HammerError| { + FlagParse::decode_flags(&mut decoder).map_err(|e: HammerError| { CliError::new(e.message, 1) }) } -- 2.30.2