Skip to content

Commit

Permalink
migrate to clap 4
Browse files Browse the repository at this point in the history
  • Loading branch information
rlespinasse committed Jan 7, 2024
1 parent a267d4b commit 9a30df8
Show file tree
Hide file tree
Showing 27 changed files with 251 additions and 232 deletions.
134 changes: 71 additions & 63 deletions Cargo.lock

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

2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ path = "src/wints/lib.rs"

[dependencies]
anyhow = "1.0"
clap = { version = "3", features = ["cargo"] }
clap = { version = "4.4", features = ["cargo"] }
directories-next = "2.0"
fuzzy-matcher = "0.3"
glob = "0.3"
Expand Down
15 changes: 6 additions & 9 deletions src/bin/wints/cli.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use crate::commands;
use anyhow::Result;
use clap::AppSettings;
use wints::util::command_prelude::*;
use clap::Command;

use crate::commands;

pub fn main() -> Result<()> {
let matches = cli().get_matches();
Expand All @@ -17,17 +17,14 @@ pub fn main() -> Result<()> {
command_exec(command_args)
}

fn cli() -> clap::App<'static> {
fn cli() -> Command {
let args = commands::global_args();
let subcommands = commands::builtin();

App::new("wints")
Command::new("wints")
.about("What I Need To See - a fuzzy term-based URLs opener")
.version(crate_version!())
.settings(&[
AppSettings::DeriveDisplayOrder,
AppSettings::AllowExternalSubcommands,
])
.allow_external_subcommands(true)
.args(args)
.subcommands(subcommands)
}
36 changes: 17 additions & 19 deletions src/bin/wints/commands/add.rs
Original file line number Diff line number Diff line change
@@ -1,27 +1,28 @@
use crate::commands::{general_args, global_arg, module_arg};
use anyhow::Result;
use clap::ArgMatches;
use directories_next::BaseDirs;
use std::path::PathBuf;
use clap::{Arg, ArgMatches, Command};

use wints::ops;
use wints::ops::wints_add::AddOptions;
use wints::util::command_prelude::*;

pub fn command() -> App {
subcommand("add")
use crate::commands::{
general_args, get_global_basedir, get_pathbuf_arg, get_string_arg, global_arg, module_arg,
};

pub fn command() -> Command {
Command::new("add")
.about("Add a url to a context")
.args(general_args())
.arg(module_arg())
.arg(global_arg())
.arg(
arg("url")
Arg::new("url")
.help("URL to set")
.value_name("URL")
.required(true)
.index(1),
)
.arg(
arg("context")
Arg::new("context")
.help("Context of the URL")
.value_name("CONTEXT")
.required(true)
Expand All @@ -30,16 +31,13 @@ pub fn command() -> App {
}

pub fn exec(args: &ArgMatches) -> Result<()> {
let local_basedir = PathBuf::from(args.value_of("config").unwrap().to_string());
let global_basedir = match args.value_of("global-config") {
None => BaseDirs::new().unwrap().home_dir().join(".wints"),
Some(value) => PathBuf::from(value),
};
let module_name = args.value_of("module").unwrap().to_string();
let url = args.value_of("url").unwrap().to_string();
let context = args.value_of("context").unwrap().to_string();
let global_module = args.is_present("global");
let dry_run = args.is_present("dry-run");
let local_basedir = get_pathbuf_arg(args, "config");
let global_basedir = get_global_basedir(args);
let module_name = get_string_arg(args, "module");
let url = get_string_arg(args, "url");
let context = get_string_arg(args, "context");
let global_module = args.get_flag("global");
let dry_run = args.get_flag("dry-run");

ops::wints_add::add(AddOptions {
local_basedir,
Expand Down
35 changes: 17 additions & 18 deletions src/bin/wints/commands/init.rs
Original file line number Diff line number Diff line change
@@ -1,38 +1,37 @@
use crate::commands::{general_args, global_arg, module_arg};
use anyhow::Result;
use clap::ArgMatches;
use directories_next::BaseDirs;
use std::path::PathBuf;
use clap::builder::PossibleValuesParser;
use clap::{Arg, ArgMatches, Command};

use wints::ops;
use wints::ops::wints_init::InitOptions;
use wints::util::command_prelude::*;

pub fn command() -> App {
subcommand("init")
use crate::commands::{
general_args, get_global_basedir, get_pathbuf_arg, get_string_arg, global_arg, module_arg,
};

pub fn command() -> Command {
Command::new("init")
.about("Initialise a new module")
.args(general_args())
.arg(module_arg())
.arg(global_arg())
.arg(
arg("template")
Arg::new("template")
.help("Template name to use")
.value_name("TEMPLATE")
.possible_values(["empty", "default"])
.value_parser(PossibleValuesParser::new(["empty", "default"]))
.default_value("empty")
.index(1),
)
}

pub fn exec(args: &ArgMatches) -> Result<()> {
let local_basedir = PathBuf::from(args.value_of("config").unwrap().to_string());
let global_basedir = match args.value_of("global-config") {
None => BaseDirs::new().unwrap().home_dir().join(".wints"),
Some(value) => PathBuf::from(value),
};
let module_name = args.value_of("module").unwrap().to_string();
let template = args.value_of("template").unwrap().to_string();
let global_module = args.is_present("global");
let dry_run = args.is_present("dry-run");
let local_basedir = get_pathbuf_arg(args, "config");
let global_basedir = get_global_basedir(args);
let module_name = get_string_arg(args, "module");
let template = get_string_arg(args, "template");
let global_module = args.get_flag("global");
let dry_run = args.get_flag("dry-run");

ops::wints_init::init(InitOptions {
local_basedir,
Expand Down
Loading

0 comments on commit 9a30df8

Please sign in to comment.