命令行参数改进

This commit is contained in:
codeboss 2024-06-05 12:12:31 +08:00
parent 221c3757b1
commit f0ed01bff6
2 changed files with 31 additions and 9 deletions

View File

@ -3,7 +3,7 @@
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|x64'"> <PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|x64'">
<LocalDebuggerWorkingDirectory>$(SolutionDir)$(Platform)\$(Configuration)\</LocalDebuggerWorkingDirectory> <LocalDebuggerWorkingDirectory>$(SolutionDir)$(Platform)\$(Configuration)\</LocalDebuggerWorkingDirectory>
<DebuggerFlavor>WindowsLocalDebugger</DebuggerFlavor> <DebuggerFlavor>WindowsLocalDebugger</DebuggerFlavor>
<LocalDebuggerCommandArguments>-path "D:\手作小说\科学+修仙+创造世界"</LocalDebuggerCommandArguments> <LocalDebuggerCommandArguments>--path "D:\手作小说\科学+修仙+创造世界"</LocalDebuggerCommandArguments>
</PropertyGroup> </PropertyGroup>
<PropertyGroup Label="QtSettings" Condition="'$(Configuration)|$(Platform)'=='Debug|x64'"> <PropertyGroup Label="QtSettings" Condition="'$(Configuration)|$(Platform)'=='Debug|x64'">
<QtLastBackgroundBuild>2024-06-04T12:14:58.9515031Z</QtLastBackgroundBuild> <QtLastBackgroundBuild>2024-06-04T12:14:58.9515031Z</QtLastBackgroundBuild>

View File

@ -34,25 +34,47 @@ std::function<void(std::shared_ptr<const ast_gen::ElementAccess>, int)> tnode_pr
* nsc [opts] --path path-to-dir * nsc [opts] --path path-to-dir
* opts: * opts:
* -p print-struct * -p print-struct
* -s service
*/ */
int main(int argc, char* argv[]) { int main(int argc, char* argv[]) {
QCoreApplication a(argc, argv); QCoreApplication a(argc, argv);
if (argc != 3 || strcmp(argv[1], "-path")) { QStringList args;
for (int idx = 0; idx < argc; idx++) {
args += QString::fromLocal8Bit(argv[idx]);
}
// 帮助特殊用法
if (args.contains(u8"--help")) {
std::cout << "nsc(WsNovelStoryCompiler故事线编译器)" << std::endl; std::cout << "nsc(WsNovelStoryCompiler故事线编译器)" << std::endl;
std::cout << "版本V1.0.0" << std::endl; std::cout << "版本V1.0.0" << std::endl;
std::cout << "nsc -path path-to-dir" << std::endl; std::cout << "nsc -path path-to-dir" << std::endl;
exit(0); exit(0);
} }
auto sdir = QDir::current(); auto source_dir = QDir::current();
if (argc >= 3 && !strcmp(argv[1], "-path")) { auto index = args.indexOf(u8"--path");
auto tdir = QDir(QString::fromLocal8Bit(argv[2])); if (index < 0) {
if (tdir.exists()) std::cout << "参数错误:必须指定 --path 参数" << endl;
sdir = tdir; exit(1);
}
if (index + 1 >= args.size()) {
std::cout << "参数错误:--path 后面应该续接story文件的父文件夹路径。" << std::endl;
exit(1);
}
else {
auto tdir = QDir(args[index+1]);
if (tdir.exists()) {
source_dir = tdir;
}
else {
std::cout << "参数错误传入的story文件父文件夹路径无效。"<< std::endl;
exit(1);
}
} }
auto files = sdir.entryInfoList(QStringList() << "*.story");
auto files = source_dir.entryInfoList(QStringList() << "*.story");
if (files.size()) { if (files.size()) {
try { try {
auto parser = std::make_shared<NovelParser>(); auto parser = std::make_shared<NovelParser>();
@ -131,7 +153,7 @@ int main(int argc, char* argv[]) {
body.appendChild(dom_storyline); body.appendChild(dom_storyline);
auto lines = tool.storyline_defines.values(); auto lines = tool.storyline_defines.values();
std::sort(lines.begin(), lines.end(), [](std::shared_ptr<printer::StoryLine> a, std::shared_ptr<printer::StoryLine> b)->bool{ std::sort(lines.begin(), lines.end(), [](std::shared_ptr<printer::StoryLine> a, std::shared_ptr<printer::StoryLine> b)->bool {
auto a_elm = std::dynamic_pointer_cast<const example_novel::StoryDefine>(a->accessPeers()->element()); auto a_elm = std::dynamic_pointer_cast<const example_novel::StoryDefine>(a->accessPeers()->element());
auto b_elm = std::dynamic_pointer_cast<const example_novel::StoryDefine>(b->accessPeers()->element()); auto b_elm = std::dynamic_pointer_cast<const example_novel::StoryDefine>(b->accessPeers()->element());
return a_elm->sort() < b_elm->sort(); return a_elm->sort() < b_elm->sort();