subcmd/lib.rs
1// Copyright © 2015 - Samuel Dolt <samuel@dolt.ch>
2//
3// Licensed under the MIT license. This file may not be copied, modified,
4// or distributed except according to those terms.
5//
6// See the COPYRIGHT file at the top-level directory of this distribution.
7
8#![warn(missing_docs)]
9
10//! Cargo style subcommand
11//!
12//! This library help to build an app that use a similar command line interface
13//! as Cargo or Git.
14//!
15//! # Example
16//!
17//! The following example show a simple program with two subcommand:
18//!
19//! - `cargo build`
20//! - `cargo clean`
21//!
22//! ```
23//! extern crate subcmd;
24//! use subcmd::CmdHandler;
25//! use subcmd::Command;
26//!
27//! struct CmdBuild;
28//!
29//! impl Command for CmdBuild {
30//! fn name<'a>(&self) -> &'a str {"build"}
31//! fn help<'a>(&self) -> &'a str {"Usage: cargo build [options]"}
32//! fn description<'a>(&self) -> &'a str { "Compile the current project" }
33//! fn run(&self, argv: &Vec<String>) {
34//! println!("I'm building your files");
35//! }
36//! }
37//!
38//! struct CmdClean;
39//!
40//! impl Command for CmdClean {
41//! fn name<'a>(&self) -> &'a str {"clean"}
42//! fn help<'a>(&self) -> &'a str {"Usage: cargo clean [options]"}
43//! fn description<'a>(&self) -> &'a str { "Remove the target directory" }
44//! fn run(&self, argv: &Vec<String>) {
45//! println!("I'm cleaning your files");
46//! }
47//! }
48//!
49//! fn main() {
50//! let mut handler = CmdHandler::new();
51//! handler.add(Box::new(CmdBuild));
52//! handler.add(Box::new(CmdClean));
53//! handler.run();
54//! }
55//! ```
56
57extern crate getopts;
58extern crate tabwriter;
59extern crate strsim;
60extern crate ansi_term;
61
62mod handler;
63pub use handler::CmdHandler;
64
65mod message;
66pub use message::Message;
67
68mod result;
69pub use result::CmdResult;
70
71mod wrapper;
72pub use wrapper::CmdWrapper;
73
74/// This trait must be implemented for each subcommand
75pub trait Command {
76 /// This fonction must return the command line, without space. Like
77 /// `build`, `clean`, ...
78 fn name<'a>(&self) -> &'a str;
79
80 /// Return the help message of a subcommand. Used for `bin help subcommand`
81 fn help<'a>(&self) -> &'a str;
82
83 /// Return a one line description. Used for the program help `bin -h`
84 fn description<'a>(&self) -> &'a str;
85
86 /// Main entry point. argv contains all argument passed to the binary,
87 /// with the program name in argv[0]
88 fn run(&self, argv: &Vec<String>);
89}