use std::{result::Result, str::FromStr};
use structopt::{clap::AppSettings::ColoredHelp, StructOpt};
use cargo_deps::Config;
const USAGE: &str = "\
cargo-deps writes a graph in dot format to standard output.
Typical usage is `cargo deps | dot -Tpng > graph.png`.";
#[derive(Debug, StructOpt)]
#[structopt(bin_name = "cargo")]
pub enum Command {
#[structopt(name = "deps", usage = USAGE, author, setting(ColoredHelp))]
Deps(Args),
}
#[derive(Debug, StructOpt)]
pub struct Args {
#[structopt(long = "depth", short = "d", value_name = "DEPTH", parse(try_from_str = parse_depth))]
pub depth: Option<usize>,
#[structopt(long = "dot-file", short = "o", value_name = "PATH")]
pub dot_file: Option<String>,
#[structopt(long = "filter", value_name = "DEPNAMES")]
pub filter: Option<Vec<String>>,
#[structopt(long = "exclude", value_name = "DEPNAMES")]
pub exclude: Option<Vec<String>>,
#[structopt(
long = "manifest-path",
value_name = "PATH",
default_value = "Cargo.toml"
)]
pub manifest_path: String,
#[structopt(long = "subgraph", value_name = "DEPNAMES")]
pub subgraph: Option<Vec<String>>,
#[structopt(long = "subgraph-name", value_name = "NAME", requires = "subgraph")]
pub subgraph_name: Option<String>,
#[structopt(long = "all-deps")]
pub all_deps: bool,
#[structopt(long = "build-deps")]
pub build_deps: bool,
#[structopt(long = "dev-deps")]
pub dev_deps: bool,
#[structopt(long = "optional-deps")]
pub optional_deps: bool,
#[structopt(long = "include-orphans")]
pub include_orphans: bool,
#[structopt(long = "include-versions", short = "I")]
pub include_versions: bool,
#[structopt(long = "no-regular-deps")]
pub no_regular_deps: bool,
#[structopt(long = "no-transitive-deps")]
pub no_transitive_deps: bool,
}
fn parse_depth(src: &str) -> Result<usize, String> {
usize::from_str(src).map_err(|e| format!("'{}': {}", src, e))
}
pub fn parse_args(args: Args) -> Config {
Config {
depth: args.depth,
dot_file: args.dot_file,
filter: args.filter,
exclude: args.exclude,
include_orphans: args.include_orphans,
include_versions: args.include_versions,
manifest_path: args.manifest_path,
subgraph: args.subgraph,
subgraph_name: args.subgraph_name,
regular_deps: !args.no_regular_deps,
build_deps: args.all_deps || args.build_deps,
dev_deps: args.all_deps || args.dev_deps,
optional_deps: args.all_deps || args.optional_deps,
transitive_deps: !args.no_transitive_deps,
}
}