命令行参数改进

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'">
<LocalDebuggerWorkingDirectory>$(SolutionDir)$(Platform)\$(Configuration)\</LocalDebuggerWorkingDirectory>
<DebuggerFlavor>WindowsLocalDebugger</DebuggerFlavor>
<LocalDebuggerCommandArguments>-path "D:\手作小说\科学+修仙+创造世界"</LocalDebuggerCommandArguments>
<LocalDebuggerCommandArguments>--path "D:\手作小说\科学+修仙+创造世界"</LocalDebuggerCommandArguments>
</PropertyGroup>
<PropertyGroup Label="QtSettings" Condition="'$(Configuration)|$(Platform)'=='Debug|x64'">
<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
* opts:
* -p print-struct
* -s service
*/
int main(int argc, char* 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 << "版本V1.0.0" << std::endl;
std::cout << "nsc -path path-to-dir" << std::endl;
exit(0);
}
auto sdir = QDir::current();
if (argc >= 3 && !strcmp(argv[1], "-path")) {
auto tdir = QDir(QString::fromLocal8Bit(argv[2]));
if (tdir.exists())
sdir = tdir;
auto source_dir = QDir::current();
auto index = args.indexOf(u8"--path");
if (index < 0) {
std::cout << "参数错误:必须指定 --path 参数" << endl;
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()) {
try {
auto parser = std::make_shared<NovelParser>();