Skip to content

Commit 734314e

Browse files
committed
修改命令行工具可以在指定输入文件时指定工作区
1 parent c667b7b commit 734314e

File tree

3 files changed

+19
-13
lines changed

3 files changed

+19
-13
lines changed

CodeFormat/src/CodeFormat.cpp

Lines changed: 17 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ int main(int argc, char** argv)
2828
cmd.AddTarget("format")
2929
.Add<std::string>("file", "f", "Specify the input file")
3030
.Add<std::string>("workspace", "w",
31-
"Specify workspace directory perform batch formatting")
31+
"Specify workspace directory,if no input file is specified, bulk formatting is performed")
3232
.Add<int>("stdin", "i", "Read from stdin and specify read size")
3333
.Add<std::string>("config", "c",
3434
"Specify .editorconfig file, it decides on the effect of formatting")
@@ -41,7 +41,7 @@ int main(int argc, char** argv)
4141
cmd.AddTarget("check")
4242
.Add<std::string>("file", "f", "Specify the input file")
4343
.Add<std::string>("workspace", "w",
44-
"Specify workspace directory perform batch checking")
44+
"Specify workspace directory, if no input file is specified, bulk checking is performed")
4545
.Add<std::string>("config", "c",
4646
"Specify editorconfig file, it decides on the effect of formatting or diagnosis")
4747
.Add<bool>("detect-config", "d",
@@ -57,7 +57,7 @@ int main(int argc, char** argv)
5757
return -1;
5858
}
5959

60-
if (!cmd.HasOption("workspace"))
60+
if (cmd.HasOption("file"))
6161
{
6262
auto luaFormat = std::make_shared<LuaFormat>();
6363
if (cmd.HasOption("file"))
@@ -90,7 +90,14 @@ int main(int argc, char** argv)
9090

9191
if (cmd.Get<bool>("detect-config"))
9292
{
93-
luaFormat->AutoDetectConfig();
93+
if (cmd.HasOption("workspace"))
94+
{
95+
luaFormat->AutoDetectConfig(std::filesystem::path(cmd.Get<std::string>("workspace")));
96+
}
97+
else
98+
{
99+
luaFormat->AutoDetectConfig();
100+
}
94101
}
95102
else if (cmd.HasOption("config"))
96103
{
@@ -115,28 +122,28 @@ int main(int argc, char** argv)
115122
}
116123
}
117124
}
118-
else
125+
else if (cmd.HasOption("workspace"))
119126
{
120127
LuaWorkspaceFormat workspaceFormat(cmd.Get<std::string>("workspace"));
121128

122-
if(cmd.Get<bool>("detect-config"))
129+
if (cmd.Get<bool>("detect-config"))
123130
{
124131
workspaceFormat.SetAutoDetectConfig(true);
125132
}
126-
else if(cmd.HasOption("config"))
133+
else if (cmd.HasOption("config"))
127134
{
128135
workspaceFormat.SetConfigPath(cmd.Get<std::string>("config"));
129136
}
130137

131138
workspaceFormat.SetKeyValues(cmd.GetKeyValueOptions());
132139

133-
if(cmd.GetTarget() == "format")
140+
if (cmd.GetTarget() == "format")
134141
{
135142
workspaceFormat.ReformatWorkspace();
136143
}
137-
else if(cmd.GetTarget() == "check")
144+
else if (cmd.GetTarget() == "check")
138145
{
139-
if(!workspaceFormat.CheckWorkspace() && cmd.Get<bool>("diagnosis-as-error"))
146+
if (!workspaceFormat.CheckWorkspace() && cmd.Get<bool>("diagnosis-as-error"))
140147
{
141148
return -1;
142149
}

CodeFormat/src/LuaFormat.cpp

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -52,13 +52,12 @@ bool IsSubRelative(std::filesystem::path& path, std::filesystem::path base)
5252
return !relativeString.starts_with(".");
5353
}
5454

55-
void LuaFormat::AutoDetectConfig()
55+
void LuaFormat::AutoDetectConfig(std::filesystem::path workspace)
5656
{
5757
if (_inputFile.empty())
5858
{
5959
return;
6060
}
61-
std::filesystem::path workspace(std::filesystem::current_path());
6261
std::filesystem::path inputPath(_inputFile);
6362
if (!IsSubRelative(inputPath, workspace))
6463
{

CodeFormat/src/LuaFormat.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ class LuaFormat
2020

2121
void SetOutputFile(std::string_view path);
2222

23-
void AutoDetectConfig();
23+
void AutoDetectConfig(std::filesystem::path workspace = std::filesystem::current_path());
2424

2525
void SetConfigPath(std::string_view config);
2626

0 commit comments

Comments
 (0)