Skip to content

Commit

Permalink
refactor: switch arg parsing to clap v3
Browse files Browse the repository at this point in the history
  • Loading branch information
tranzystorekk authored and oknozor committed Jan 16, 2022
1 parent 79c1608 commit 13ebca5
Show file tree
Hide file tree
Showing 4 changed files with 130 additions and 138 deletions.
129 changes: 70 additions & 59 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

9 changes: 5 additions & 4 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,8 @@ shell-words = "^1"
which = "^4"
lazy_static = "^1"
toml = "^0"
structopt = { version = "^0", optional = true }
clap = { version = "3.0", optional = true, features = ["derive"] }
clap_complete = { version = "3.0", optional = true }
conventional_commit_parser = "0.9.3"
pest = "2.1.3"
pest_derive = "2.1.0"
Expand All @@ -53,7 +54,7 @@ cmd_lib = "1.3.0"

[features]
default = ["cli"]
cli = ["structopt"]
cli = ["clap", "clap_complete"]

[lib]
name = "cocogitto"
Expand All @@ -62,12 +63,12 @@ path = "src/lib.rs"
[[bin]]
name = "cog"
path = "src/bin/cog.rs"
required-features = ["structopt"]
required-features = ["clap", "clap_complete"]

[[bin]]
name = "coco"
path = "src/bin/coco.rs"
required-features = ["structopt"]
required-features = ["clap", "clap_complete"]

[[test]]
name = "all"
Expand Down
33 changes: 16 additions & 17 deletions src/bin/coco.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,52 +4,51 @@ mod cog_commit;
use cocogitto::CocoGitto;

use anyhow::Result;
use structopt::clap::{AppSettings, Shell};
use structopt::StructOpt;

const APP_SETTINGS: &[AppSettings] = &[
AppSettings::UnifiedHelpMessage,
AppSettings::ColoredHelp,
AppSettings::DeriveDisplayOrder,
];
use clap::{AppSettings, IntoApp, Parser};
use clap_complete::Shell;

/// A command line tool to create conventional commits
///
/// Deprecated in favor of the `cog commit` utility
#[derive(StructOpt)]
#[structopt(name = "Coco", author = "Paul D. <paul.delafosse@protonmail.com>", settings = APP_SETTINGS)]
#[derive(Parser)]
#[clap(setting = AppSettings::DeriveDisplayOrder)]
#[clap(
version,
name = "Coco",
author = "Paul D. <paul.delafosse@protonmail.com>"
)]
struct Cli {
/// Conventional commit type
#[structopt(name = "type", possible_values = &cog_commit::commit_types(), default_value_if("completion", None, "chore"))]
#[clap(name = "type", value_name = "TYPE", possible_values = cog_commit::commit_types(), default_value_if("completion", None, Some("chore")))]
typ: String,

/// Commit description
#[structopt(default_value_if("completion", None, ""))]
#[clap(default_value_if("completion", None, Some("")))]
message: String,

/// Conventional commit scope
scope: Option<String>,

/// Create a BREAKING CHANGE commit
#[structopt(short = "B", long)]
#[clap(short = 'B', long)]
breaking_change: bool,

/// Open commit message in an editor
#[structopt(short, long)]
#[clap(short, long)]
edit: bool,

/// Generate shell completions
#[structopt(long, conflicts_with = "type")]
#[clap(long, conflicts_with = "type")]
completion: Option<Shell>,
}

fn main() -> Result<()> {
let cli = Cli::from_args();
let cli = Cli::parse();

eprintln!("Warning: `coco` is deprecated, use `cog commit` instead");

if let Some(shell) = cli.completion {
Cli::clap().gen_completions_to("coco", shell, &mut std::io::stdout());
clap_complete::generate(shell, &mut Cli::into_app(), "coco", &mut std::io::stdout());
} else {
let cocogitto = CocoGitto::get()?;

Expand Down
Loading

0 comments on commit 13ebca5

Please sign in to comment.