Skip to content

Commit 4570dea

Browse files
authored
Merge pull request #519 from lewis6991/feat/clap34324
migrate from structopt to clap
2 parents b9647c8 + 788f4f9 commit 4570dea

File tree

12 files changed

+217
-298
lines changed

12 files changed

+217
-298
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
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/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

crates/emmylua_ls/src/cmd_args.rs

Lines changed: 14 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -1,66 +1,39 @@
1-
use structopt::StructOpt;
1+
use clap::{Parser, ValueEnum};
22

33
#[allow(unused)]
4-
#[derive(Debug, StructOpt, Clone)]
5-
#[structopt(name = "emmylua-ls", about = "EmmyLua Language Server")]
4+
#[derive(Debug, Parser, Clone)]
65
pub struct CmdArgs {
76
/// Communication method
8-
#[structopt(
9-
long = "communication",
10-
short = "c",
11-
help = "Communication method",
12-
default_value = "stdio"
13-
)]
7+
#[structopt(long, short, default_value = "stdio")]
148
pub communication: Communication,
159

1610
/// IP address to listen on (only valid when using TCP)
17-
#[structopt(
18-
long = "ip",
19-
help = "IP address to listen on",
20-
default_value = "127.0.0.1"
21-
)]
11+
#[structopt(long, default_value = "127.0.0.1")]
2212
pub ip: String,
2313

2414
/// Port number to listen on (only valid when using TCP)
25-
#[structopt(
26-
long = "port",
27-
help = "Port number to listen on",
28-
default_value = "5007"
29-
)]
15+
#[structopt(long, default_value = "5007")]
3016
pub port: u16,
3117

32-
/// Logging level (e.g., "error", "warn", "info", "debug", "trace")
33-
#[structopt(long = "log-level", help = "Logging level", default_value = "info")]
18+
/// Logging level
19+
#[structopt(long, default_value = "info")]
3420
pub log_level: LogLevel,
3521

36-
/// Path to the log file
37-
#[structopt(
38-
long = "log-path",
39-
help = "Path to the log file. Use 'none' to disable log file output.",
40-
default_value = ""
41-
)]
22+
/// Path to the log file. Use 'none' to disable log file output.
23+
#[structopt(long, default_value = "")]
4224
pub log_path: NoneableString,
4325

4426
/// Path to the resources and logs directory. Use 'none' to indicate that assets should not be output to the file system.
45-
#[structopt(
46-
long = "resources-path",
47-
help = "Path to the resources. Use 'none' to disable resources file output.",
48-
default_value = ""
49-
)]
27+
#[structopt(long, default_value = "")]
5028
pub resources_path: NoneableString,
5129

5230
/// Whether to load the standard library.
53-
#[structopt(
54-
long = "load-stdlib",
55-
help = "Whether to load the standard library",
56-
default_value = "true"
57-
)]
58-
pub load_std_lib: CmdBool,
31+
#[structopt(long, default_value = "true")]
32+
pub load_stdlib: CmdBool,
5933
}
6034

6135
/// Logging level enum
62-
#[derive(Debug, StructOpt, Clone, Copy)]
63-
#[structopt(rename_all = "lowercase")]
36+
#[derive(Debug, Clone, Copy, ValueEnum)]
6437
pub enum LogLevel {
6538
/// Error level
6639
Error,
@@ -89,8 +62,7 @@ impl std::str::FromStr for LogLevel {
8962
}
9063
}
9164

92-
#[derive(Debug, StructOpt, Clone, Copy)]
93-
#[structopt(rename_all = "lowercase")]
65+
#[derive(Debug, ValueEnum, Clone, Copy)]
9466
pub enum Communication {
9567
Stdio,
9668
Tcp,

0 commit comments

Comments
 (0)