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
// Copyright 2015 Axel Rasmussen
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

use command::{CommandResult, ExecutableCommand};
use error::Result;
use help;
use io::get_writer_impl;
use parsed_parameters::ParsedParameters;
use parsed_parameters::parse_command;
use std::string::String;
use std::vec::Vec;

fn parse_and_execute_impl<E>(program: &str,
                             parameters: &[String],
                             commands: Vec<ExecutableCommand<E>>,
                             print_program_help: bool,
                             print_command_name: bool)
                             -> Result<CommandResult<E>> {
    let mut parameters_iterator = parameters.iter().peekable();

    let command = parse_command(program,
                                &mut parameters_iterator,
                                commands,
                                print_program_help)?;
    let parsed_parameters = match ParsedParameters::new(&command.command,
                                                        &mut parameters_iterator) {
        Ok(p) => p,
        Err(e) => {
            help::print_command_help(&mut get_writer_impl(),
                                     program,
                                     &command.command,
                                     print_command_name)?;
            return Err(e);
        },
    };

    Ok(parsed_parameters.execute(command))
}

/// This function parses the given program parameters, and calls the appropriate
/// command callback. It prints out usage information if the parameters are
/// invalid, and returns a reasonable exit code for the process.
///
/// This is the function which should be used for typical multi-command
/// programs.
pub fn parse_and_execute_command<E>(program: &str,
                                    parameters: &[String],
                                    commands: Vec<ExecutableCommand<E>>)
                                    -> Result<CommandResult<E>> {
    parse_and_execute_impl(program, parameters, commands, true, true)
}

/// This function parses the given program parameters and calls the given
/// command's callback. It prints out usage information if the parameters are
/// invalid, and returns a reasonable exit code for the process.
///
/// This is the function which should be used for typical single-command
/// programs.
pub fn parse_and_execute<E>(program: &str,
                            parameters: &[String],
                            command: ExecutableCommand<E>)
                            -> Result<CommandResult<E>> {
    let parameters: Vec<String> =
        Some(command.command.name.clone()).into_iter().chain(parameters.iter().cloned()).collect();
    parse_and_execute_impl(program, parameters.as_slice(), vec![command], false, false)
}