<commit_before>// Copyright 2017 The Exonum Team
//
// 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 clap;

use std::ffi::OsString;

use super::{Context, ArgumentType};
use super::internal::{Feedback, CollectedCommand};

pub struct ClapBackend;

impl ClapBackend {
    // TODO: remove code duplication (ECR-164)
    #[doc(hidden)]
    pub fn execute_cmd_string<I, T>(commands: &[CollectedCommand], line: I) -> Feedback
    where
        I: IntoIterator<Item = T>,
        T: Into<OsString> + Clone,
    {
        let subcommands: Vec<_> = commands
            .iter()
            .map(|command| ClapBackend::command_into_subcommand(command))
            .collect();
        let matches = clap::App::new("Exonum application based on fabric configuration.")
            .version(crate_version!())
            .author(crate_authors!("\n"))
            .about(
                "It contain basic set of command, to deploy network on exonum.",
            )
            .subcommands(subcommands.into_iter())
            .get_matches_from_safe(line)
            .unwrap();

        let subcommand = matches.subcommand();
        for command in commands {
            if command.name() == subcommand.0 {
                return command.execute(Context::new_from_args(
                    command.args(),
                    subcommand.1.expect("Arguments not found."),
                ));
            }
        }

        panic!("Subcommand not found");
    }

    pub fn execute(commands: &[CollectedCommand]) -> Feedback {
        let subcommands: Vec<_> = commands
            .iter()
            .map(|command| ClapBackend::command_into_subcommand(command))
            .collect();

        let matches = clap::App::new("Exonum application based on fabric configuration.")
            .version(crate_version!())
            .author(crate_authors!("\n"))
            .about("Exonum application based on fabric configuration.")
            .subcommands(subcommands.into_iter())
            .get_matches();

        let subcommand = matches.subcommand();
        for command in commands {
            if command.name() == subcommand.0 {
                return command.execute(Context::new_from_args(
                    command.args(),
                    subcommand.1.expect("Arguments not found."),
                ));
            }
        }

        panic!("Subcommand not found");
    }

    fn command_into_subcommand(command: &CollectedCommand) -> clap::App {
        let mut index = 1;
        let command_args: Vec<_> = command
            .args()
            .iter()
            .map(|arg| {
                let clap_arg = clap::Arg::with_name(arg.name);
                let clap_arg = match arg.argument_type {
                    ArgumentType::Positional => {
                        let arg = clap_arg.index(index);
                        index += 1;<commit_msg>ClapBackend: Show a help message when there're no args<commit_after>// Copyright 2017 The Exonum Team
//
// 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 clap;

use std::ffi::OsString;

use super::{Context, ArgumentType};
use super::internal::{Feedback, CollectedCommand};

pub struct ClapBackend;

impl ClapBackend {
    // TODO: remove code duplication (ECR-164)
    #[doc(hidden)]
    pub fn execute_cmd_string<I, T>(commands: &[CollectedCommand], line: I) -> Feedback
    where
        I: IntoIterator<Item = T>,
        T: Into<OsString> + Clone,
    {
        let subcommands: Vec<_> = commands
            .iter()
            .map(|command| ClapBackend::command_into_subcommand(command))
            .collect();
        let matches = clap::App::new("Exonum application based on fabric configuration.")
            .setting(clap::AppSettings::ArgRequiredElseHelp)
            .version(crate_version!())
            .author(crate_authors!("\n"))
            .about(
                "It contain basic set of command, to deploy network on exonum.",
            )
            .subcommands(subcommands.into_iter())
            .get_matches_from_safe(line)
            .unwrap();

        let subcommand = matches.subcommand();
        for command in commands {
            if command.name() == subcommand.0 {
                return command.execute(Context::new_from_args(
                    command.args(),
                    subcommand.1.expect("Arguments not found."),
                ));
            }
        }

        panic!("Subcommand not found");
    }

    pub fn execute(commands: &[CollectedCommand]) -> Feedback {
        let subcommands: Vec<_> = commands
            .iter()
            .map(|command| ClapBackend::command_into_subcommand(command))
            .collect();

        let matches = clap::App::new("Exonum application based on fabric configuration.")
            .setting(clap::AppSettings::ArgRequiredElseHelp)
            .version(crate_version!())
            .author(crate_authors!("\n"))
            .about("Exonum application based on fabric configuration.")
            .subcommands(subcommands.into_iter())
            .get_matches();

        let subcommand = matches.subcommand();
        for command in commands {
            if command.name() == subcommand.0 {
                return command.execute(Context::new_from_args(
                    command.args(),
                    subcommand.1.expect("Arguments not found."),
                ));
            }
        }

        panic!("Subcommand not found");
    }

    fn command_into_subcommand(command: &CollectedCommand) -> clap::App {
        let mut index = 1;
        let command_args: Vec<_> = command
            .args()
            .iter()
            .map(|arg| {
                let clap_arg = clap::Arg::with_name(arg.name);
                let clap_arg = match arg.argument_type {
                    ArgumentType::Positional => {
                        let arg = clap_arg.index(index);
                        index += 1;
{'fraction_non_alphanumeric': 0.11247650715628163, 'fraction_numerical': 0.00491542576261385, 'mean_word_length': 2.009134406263593, 'pattern_counts': {'":': 0, '<': 15, '<?xml version=': 0, '>': 23, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5204608', 'n_tokens_mistral': 1838, 'n_tokens_neox': 1788, 'n_words': 509}