1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
// Copyright © 2015 - Samuel Dolt <samuel@dolt.ch>
//
// Licensed under the MIT license. This file may not be copied, modified,
// or distributed except according to those terms.
//
// See the COPYRIGHT file at the top-level directory of this distribution.

#![warn(missing_docs)]

//! Cargo style subcommand
//!
//! This library help to build an app that use a similar command line interface
//! as Cargo or Git.
//!
//! # Example
//!
//! The following example show a simple program with two subcommand:
//!
//! - `cargo build`
//! - `cargo clean`
//!
//! ```
//! extern crate subcmd;
//! use subcmd::CmdHandler;
//! use subcmd::Command;
//!
//! struct CmdBuild;
//!
//! impl Command for CmdBuild {
//!     fn name<'a>(&self) -> &'a str {"build"}
//!     fn help<'a>(&self) -> &'a str {"Usage: cargo build [options]"}
//!     fn description<'a>(&self) -> &'a str { "Compile the current project" }
//!     fn run(&self, argv: &Vec<String>) {
//!         println!("I'm building your files");
//!     }
//! }
//!
//! struct CmdClean;
//!
//! impl Command for CmdClean {
//!     fn name<'a>(&self) -> &'a str {"clean"}
//!     fn help<'a>(&self) -> &'a str {"Usage: cargo clean [options]"}
//!     fn description<'a>(&self) -> &'a str { "Remove the target directory" }
//!     fn run(&self, argv: &Vec<String>) {
//!         println!("I'm cleaning your files");
//!     }
//! }
//!
//! fn main() {
//!     let mut handler = CmdHandler::new();
//!     handler.add(Box::new(CmdBuild));
//!     handler.add(Box::new(CmdClean));
//!     handler.run();
//! }
//! ```

extern crate getopts;
extern crate tabwriter;
extern crate strsim;
extern crate ansi_term;

mod handler;
pub use handler::CmdHandler;

mod message;
pub use message::Message;

mod result;
pub use result::CmdResult;

mod wrapper;
pub use wrapper::CmdWrapper;

/// This trait must be implemented for each subcommand
pub trait Command {
    /// This fonction must return the command line, without space. Like
    /// `build`, `clean`, ...
    fn name<'a>(&self) -> &'a str;

    /// Return the help message of a subcommand. Used for `bin help subcommand`
    fn help<'a>(&self) -> &'a str;

    /// Return a one line description. Used for the program help `bin -h`
    fn description<'a>(&self) -> &'a str;

    /// Main entry point. argv contains all argument passed to the binary,
    /// with the program name in argv[0]
    fn run(&self, argv: &Vec<String>);
}