Skip to content

Commit

Permalink
Closes #21
Browse files Browse the repository at this point in the history
  • Loading branch information
Matteo Guarda committed Nov 16, 2020
1 parent 4c012f2 commit 8f7e1bb
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 17 deletions.
31 changes: 16 additions & 15 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -277,7 +277,7 @@ fn main() -> anyhow::Result<()> {
let mut yaml_loader = YamlLoader::load_from_str(&stdin)?;
yaml_loader.remove(0)
};
let mut yaml_config = yaml_loader.into_hash().context("Invalid YAML config")?;
let yaml_config = yaml_loader.into_hash().context("Invalid YAML config")?;
config_checker::required(&yaml_config)?;
config_checker::banned(&yaml_config)?;

Expand All @@ -286,25 +286,11 @@ fn main() -> anyhow::Result<()> {
.map(|mut x| x.remove(0))
.unwrap();

let yaml_loader = {
yaml_config.remove_entry(&subcommands_key);
let new_yaml_content = {
let mut buffer = String::new();
let mut emitter = yaml_rust::YamlEmitter::new(&mut buffer);
let yaml_hash = Yaml::Hash(yaml_config);
emitter.dump(&yaml_hash).unwrap();
buffer
};
let mut loader = YamlLoader::load_from_str(&new_yaml_content)?;
loader.remove(0)
};

// Clap doesn't let us redirect --help and --version to stderr so we have to do it manually.
// This block of code parses the subcommands into a Vec<AppWrapper> and removes from the
// YamlLoader the subcommands parts so we can add them manually later to the `external_app`
// clap::App.
let external_app_subcommands = {
let yaml_config = yaml_loader.as_hash().unwrap();
if yaml_config.contains_key(&subcommands_key) {
let subcommands = yaml_config.get(&subcommands_key).unwrap();
let external_app_subcommands = subcommands
Expand All @@ -323,6 +309,21 @@ fn main() -> anyhow::Result<()> {
}
};

let yaml_loader = {
#[allow(clippy::redundant_clone)]
let mut yaml_config = yaml_config.clone();
yaml_config.remove_entry(&subcommands_key);
let new_yaml_content = {
let mut buffer = String::new();
let mut emitter = yaml_rust::YamlEmitter::new(&mut buffer);
let yaml_hash = Yaml::Hash(yaml_config);
emitter.dump(&yaml_hash).unwrap();
buffer
};
let mut loader = YamlLoader::load_from_str(&new_yaml_content)?;
loader.remove(0)
};

let external_app_help_subcmd = AppWrapper::new(SubCommand::with_name("help"), |app: App| {
app.arg(Arg::with_name("SUBCMD").required(false))
.about("Prints this message or the help of the given subcommand(s)")
Expand Down
4 changes: 2 additions & 2 deletions src/shell.rs
Original file line number Diff line number Diff line change
Expand Up @@ -180,15 +180,15 @@ impl Shell {
buffer.push('\n');
}

Ok(buffer.trim_end().into())
Ok(buffer)
}

pub fn parse(
&self,
matches: clap::ArgMatches,
var_prefix: Option<&str>,
) -> anyhow::Result<String> {
self.parse_(&matches, var_prefix, None)
Ok(self.parse_(&matches, var_prefix, None)?.trim_end().into())
}
}

Expand Down

0 comments on commit 8f7e1bb

Please sign in to comment.