]> git.proxmox.com Git - rustc.git/blame - src/rustbook/subcommand.rs
Imported Upstream version 1.8.0+dfsg1
[rustc.git] / src / rustbook / subcommand.rs
CommitLineData
1a4d82fc
JJ
1// Copyright 2014 The Rust Project Developers. See the COPYRIGHT
2// file at the top-level directory of this distribution and at
3// http://rust-lang.org/COPYRIGHT.
4//
5// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
6// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
7// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
8// option. This file may not be copied, modified, or distributed
9// except according to those terms.
10
b039eaaf 11//! Common API for all rustbook subcommands.
1a4d82fc
JJ
12
13use error::CliResult;
14use error::CommandResult;
15use term::Term;
16
17use help;
18use build;
19use serve;
20use test;
21
22pub trait Subcommand {
23 /// Mutate the subcommand by parsing its arguments.
24 ///
25 /// Returns `Err` on a parsing error.
26 fn parse_args(&mut self, args: &[String]) -> CliResult<()>;
27 /// Print the CLI usage information.
28 fn usage(&self);
29 /// Actually execute the subcommand.
30 fn execute(&mut self, term: &mut Term) -> CommandResult<()>;
31}
32
33/// Create a Subcommand object based on its name.
34pub fn parse_name(name: &str) -> Option<Box<Subcommand>> {
c34b1796
AL
35 let cmds: [fn(&str) -> Option<Box<Subcommand>>; 4] = [help::parse_cmd,
36 build::parse_cmd,
37 serve::parse_cmd,
38 test::parse_cmd];
62682a34 39 for parser in &cmds {
1a4d82fc
JJ
40 let parsed = (*parser)(name);
41 if parsed.is_some() { return parsed }
42 }
43 None
44}