Skip to content

Commit 788f4f9

Browse files
committed
migrate from structopt to clap
1 parent c3257c3 commit 788f4f9

File tree

13 files changed

+221
-299
lines changed

13 files changed

+221
-299
lines changed

Cargo.lock

Lines changed: 156 additions & 167 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,6 @@ walkdir = "2.5.0"
3131
serde_yml = "0.0.12"
3232
dirs = "5"
3333
emmylua_codestyle = "0.4.1"
34-
structopt = "0.3"
3534
wax = "0.6.0"
3635
percent-encoding = "2.3"
3736
flagset = "0.4.6"
@@ -48,3 +47,4 @@ include_dir = "0.7.4"
4847
toml_edit = "0.22.23"
4948
itertools = "0.11.0"
5049
ariadne = { version = "0.5.0", features = ["auto-color"] }
50+
clap = { version = "4.5.39", features = ["derive", "wrap_help"] }

crates/emmylua_check/Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ log.workspace = true
2323
fern.workspace = true
2424
rowan.workspace = true
2525
walkdir.workspace = true
26-
structopt.workspace = true
2726
tokio.workspace = true
2827
tokio-util.workspace = true
2928
ariadne.workspace = true
29+
clap.workspace = true

crates/emmylua_check/src/cmd_args.rs

Lines changed: 14 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -1,82 +1,59 @@
1-
use structopt::StructOpt;
1+
use clap::{Parser, ValueEnum};
2+
use std::path::PathBuf;
23

34
#[allow(unused)]
4-
#[derive(Debug, StructOpt, Clone)]
5-
#[structopt(name = "emmylua-check", about = "EmmyLua Check")]
5+
#[derive(Debug, Parser, Clone)]
66
pub struct CmdArgs {
77
/// Configuration file paths.
88
/// If not provided, both ".emmyrc.json" and ".luarc.json" will be searched in the workspace
99
/// directory
10-
#[structopt(short, long, use_delimiter = true, parse(from_os_str))]
11-
pub config: Option<Vec<std::path::PathBuf>>,
10+
#[arg(short, long, value_delimiter = ',')]
11+
pub config: Option<Vec<PathBuf>>,
1212

1313
/// Path to the workspace directory
14-
#[structopt(parse(from_os_str))]
15-
pub workspace: std::path::PathBuf,
14+
pub workspace: PathBuf,
1615

1716
/// Comma separated list of ignore patterns.
1817
/// Patterns must follow glob syntax
19-
#[structopt(short, long, use_delimiter = true)]
18+
#[arg(short, long, value_delimiter = ',')]
2019
pub ignore: Option<Vec<String>>,
2120

2221
/// Specify output format
23-
#[structopt(
24-
long,
25-
default_value = "text",
26-
possible_values = &OutputFormat::variants(),
27-
case_insensitive = true
28-
)]
22+
#[arg(long, default_value = "text", value_enum, ignore_case = true)]
2923
pub output_format: OutputFormat,
3024

3125
/// Specify output destination (stdout or a file path, only used when output_format is json).
32-
#[structopt(long, default_value = "stdout", parse(try_from_str))]
26+
#[arg(long, default_value = "stdout")]
3327
pub output: OutputDestination,
3428

3529
/// Treat warnings as errors
36-
#[structopt(long)]
30+
#[arg(long)]
3731
pub warnings_as_errors: bool,
3832

3933
/// Verbose output
40-
#[structopt(long)]
34+
#[arg(long)]
4135
pub verbose: bool,
4236
}
4337

44-
#[derive(Debug, Clone, PartialEq)]
38+
#[derive(Debug, Clone, PartialEq, ValueEnum)]
4539
pub enum OutputFormat {
4640
Json,
4741
Text,
4842
}
4943

50-
impl std::str::FromStr for OutputFormat {
51-
type Err = String;
52-
fn from_str(s: &str) -> Result<Self, Self::Err> {
53-
match s.to_lowercase().as_str() {
54-
"json" => Ok(OutputFormat::Json),
55-
"text" => Ok(OutputFormat::Text),
56-
_ => Err(format!("Invalid output format: {}", s)),
57-
}
58-
}
59-
}
60-
61-
impl OutputFormat {
62-
pub fn variants() -> [&'static str; 2] {
63-
["json", "text"]
64-
}
65-
}
66-
6744
#[allow(unused)]
6845
#[derive(Debug, Clone)]
6946
pub enum OutputDestination {
7047
Stdout,
71-
File(std::path::PathBuf),
48+
File(PathBuf),
7249
}
7350

7451
impl std::str::FromStr for OutputDestination {
7552
type Err = String;
7653
fn from_str(s: &str) -> Result<Self, Self::Err> {
7754
match s.to_lowercase().as_str() {
7855
"stdout" => Ok(OutputDestination::Stdout),
79-
_ => Ok(OutputDestination::File(std::path::PathBuf::from(s))),
56+
_ => Ok(OutputDestination::File(PathBuf::from(s))),
8057
}
8158
}
8259
}

crates/emmylua_check/src/init.rs

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,10 @@ pub fn load_workspace(
5050
vec![
5151
main_path.join(".luarc.json"),
5252
main_path.join(".emmyrc.json"),
53-
],
53+
]
54+
.into_iter()
55+
.filter(|path| path.exists())
56+
.collect(),
5457
main_path.clone(),
5558
)
5659
};

crates/emmylua_check/src/main.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,18 +2,18 @@ mod cmd_args;
22
mod init;
33
mod output;
44

5+
use clap::Parser;
56
use cmd_args::CmdArgs;
67
use emmylua_code_analysis::{DbIndex, FileId};
78
use fern::Dispatch;
89
use log::LevelFilter;
910
use output::output_result;
1011
use std::{error::Error, path::PathBuf, sync::Arc};
11-
use structopt::StructOpt;
1212
use tokio_util::sync::CancellationToken;
1313

1414
#[tokio::main]
1515
async fn main() -> Result<(), Box<dyn Error + Sync + Send>> {
16-
let cmd_args = CmdArgs::from_args();
16+
let cmd_args = CmdArgs::parse();
1717
let mut workspace = cmd_args.workspace;
1818
if !workspace.is_absolute() {
1919
workspace = std::env::current_dir()?.join(workspace);

crates/emmylua_doc_cli/Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,6 @@ serde_json.workspace = true
2626
lsp-types.workspace = true
2727
rowan.workspace = true
2828
walkdir.workspace = true
29-
structopt.workspace = true
3029
tera.workspace = true
3130
include_dir.workspace = true
31+
clap.workspace = true

crates/emmylua_doc_cli/src/cmd_args.rs

Lines changed: 22 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -1,55 +1,36 @@
1-
use std::str::FromStr;
2-
use structopt::StructOpt;
1+
use clap::{Parser, ValueEnum};
2+
use std::path::PathBuf;
33

4-
#[derive(Debug, StructOpt)]
4+
#[derive(Debug, Parser)]
55
pub struct CmdArgs {
6-
#[structopt(
7-
parse(from_os_str),
8-
long = "input",
9-
short = "i",
10-
help = "The path of the lua project"
11-
)]
12-
pub input: std::path::PathBuf,
13-
14-
#[structopt(
15-
default_value = "markdown",
16-
long = "format",
17-
short = "f",
18-
help = "Format of the output, default is Markdown"
19-
)]
6+
/// The path of the lua project
7+
#[arg(long, short)]
8+
pub input: PathBuf,
9+
10+
/// Format of the output, default is Markdown
11+
#[arg(long, short, default_value = "markdown")]
2012
pub format: Format,
2113

22-
#[structopt(
23-
parse(from_os_str),
24-
default_value = "./output",
25-
long = "output",
26-
short = "o",
27-
help = "The output path of the docs file"
28-
)]
29-
pub output: std::path::PathBuf,
30-
31-
#[structopt(
32-
parse(from_os_str),
33-
long = "override-template",
34-
help = "The path of the override template"
35-
)]
36-
pub override_template: Option<std::path::PathBuf>,
37-
38-
#[structopt(
39-
parse(from_os_str),
40-
long = "mixin",
41-
help = "The path of the mixin md file"
42-
)]
43-
pub mixin: Option<std::path::PathBuf>,
14+
/// The output path of the docs file
15+
#[arg(long, short, default_value = "./output")]
16+
pub output: PathBuf,
17+
18+
/// The path of the override template
19+
#[arg(long)]
20+
pub override_template: Option<PathBuf>,
21+
22+
/// The path of the mixin md file
23+
#[arg(long)]
24+
pub mixin: Option<PathBuf>,
4425
}
4526

46-
#[derive(Debug, Eq, PartialEq, StructOpt)]
27+
#[derive(Debug, Clone, Eq, PartialEq, ValueEnum)]
4728
pub enum Format {
4829
Markdown,
4930
Json,
5031
}
5132

52-
impl FromStr for Format {
33+
impl std::str::FromStr for Format {
5334
type Err = &'static str;
5435

5536
fn from_str(s: &str) -> Result<Self, Self::Err> {

crates/emmylua_doc_cli/src/main.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
use crate::cmd_args::Format;
2+
use clap::Parser;
23
use cmd_args::CmdArgs;
34
use std::process::exit;
4-
use structopt::StructOpt;
55

66
mod cmd_args;
77
mod common;
@@ -10,7 +10,7 @@ mod json_generator;
1010
mod markdown_generator;
1111

1212
fn main() {
13-
let args = CmdArgs::from_args();
13+
let args = CmdArgs::parse();
1414
let mut input = args.input;
1515
if input.is_relative() {
1616
input = std::env::current_dir().ok().unwrap().join(&input);

crates/emmylua_ls/Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,9 +28,9 @@ notify.workspace = true
2828
tokio-util.workspace = true
2929
rowan.workspace = true
3030
walkdir.workspace = true
31-
structopt.workspace = true
3231
rust-i18n.workspace = true
3332
glob.workspace = true
3433
serde_yml.workspace = true
3534
itertools.workspace = true
3635
dirs.workspace = true
36+
clap.workspace = true

0 commit comments

Comments
 (0)