add System.CommandLine argument parser

This commit is contained in:
Boris Nikolaev
2026-02-15 22:45:57 +03:00
parent 46d2169b1d
commit 5e2df678ab
4 changed files with 52 additions and 77 deletions
+12 -10
View File
@@ -19,25 +19,27 @@ A command-line tool for converting .CHR character model files from Dragon Quest
| Option | Description | | Option | Description |
|--------|-------------| |--------|-------------|
| `-e` | Extract only - unpack `.chr` without conversion | | `-i`, `--input` | Input path |
| `-t` | Output as `.glTF` (text) instead of `.glb` (binary) | | `-o`, `--output` | Output path |
| `-b` | Batch mode - process all `.chr` files in directory | | `-e`, `--extract` | Extract only - unpack `.chr` without conversion |
| `-f`, `--format` | Output format. `GLTF` for text format or `GLB` for binary (default: `GLB`) |
| `-b`, `--batch` | Batch mode - process all `.chr` files in directory |
| `-h`, `--help` | Show help |
## Usage ## Usage
### Basic Syntax ### Basic Syntax
``` ```
dq8chr2glb.exe <input_file> <output_dir> [options] dq8chr2glb.exe -i <input_file> -o <output_dir> [options]
dq8chr2glb.exe <input_dir> -b # Batch mode dq8chr2glb.exe -i <input_dir> -b # Batch mode
dq8chr2glb.exe # No args for get help
``` ```
### Examples ### Examples
``` ```
dq8chr2glb.exe "C:\Games\DQ8\@DATA.DAT\chara\ap002.chr" "C:\Exports" dq8chr2glb.exe -i "C:\Games\DQ8\@DATA.DAT\chara\ap002.chr" -o "C:\Exports"
dq8chr2glb.exe "C:\Games\DQ8\@DATA.DAT\chara" -b dq8chr2glb.exe -i "C:\Games\DQ8\@DATA.DAT\chara" -b
dq8chr2glb.exe "C:\Games\DQ8\@DATA.DAT\chara\ap002.chr" "C:\Exports" -e dq8chr2glb.exe -i "C:\Games\DQ8\@DATA.DAT\chara\ap002.chr" -o "C:\Exports" -e
dq8chr2glb.exe "C:\Games\DQ8\@DATA.DAT\chara" -b -e dq8chr2glb.exe -i "C:\Games\DQ8\@DATA.DAT\chara" -b -t GLTF
``` ```
+7
View File
@@ -0,0 +1,7 @@
namespace dq8chr2glb.Converter;
public enum OutputFormat
{
GLB,
GLTF,
}
+32 -67
View File
@@ -1,6 +1,8 @@
using System; using System;
using System.Collections.Generic; using System.Collections.Generic;
using System.CommandLine;
using System.IO; using System.IO;
using dq8chr2glb.Converter;
using dq8chr2glb.Logger; using dq8chr2glb.Logger;
namespace dq8chr2glb; namespace dq8chr2glb;
@@ -9,57 +11,41 @@ public class Program
{ {
public static void Main(string[] args) public static void Main(string[] args)
{ {
if (args.Length < 2) var rootCommand = new RootCommand("Converts .CHR files from Dragon Quest VIII (Playstation 2) to .glb/.glTF format.")
{ {
ShowHelp(); new Option<string>("-i", "--input") { Description = "Input path", HelpName = "input path"},
return; new Option<string>("-o", "--output") { Description = "Output path", HelpName = "output path"},
} new Option<OutputFormat>("-f", "--format") {Description = "Output format", DefaultValueFactory = _ => OutputFormat.GLB},
new Option<bool>("-e", "--extract") {Description = "Extract only - unpack .chr without conversion"},
new Option<bool>("-b", "--batch") {Description = "Batch mode - process all .chr files in the input directory"},
new Option<LogMode>("-l", "--log") { Description = "Log level", DefaultValueFactory = _ => LogMode.MINIMAL },
};
var extractOnly = false; var parser = rootCommand.Parse(args);
var textFormat = false;
var batchMode = false;
var processedArgs = new List<string>(); var inputPath = parser.GetValue<string>("-i");
for (int i = 0; i < args.Length; i++) var outputPath = parser.GetValue<string>("-o");
{ var extractOnly = parser.GetValue<bool>("-e");
var arg = args[i]; var textFormat = parser.GetValue<OutputFormat>("-f") == OutputFormat.GLTF;
if (arg == "-e") var batchMode = parser.GetValue<bool>("-b");
extractOnly = true;
else if (arg == "-t")
textFormat = true;
else if (arg == "-b")
batchMode = true;
else
processedArgs.Add(arg);
}
if (batchMode)
{
if (processedArgs.Count != 1)
{
Console.WriteLine("Error: Batch mode (-b) requires exactly one argument: <input_dir>");
ShowHelp();
return;
}
}
else
{
if (processedArgs.Count != 2)
{
Console.WriteLine("Error: Expected <input_file> and <output_dir>");
ShowHelp();
return;
}
}
var inputPath = processedArgs[0];
var outputPath = batchMode ? inputPath : processedArgs[1];
var chrFile = new ChrFile(); var chrFile = new ChrFile();
chrFile.convert = !extractOnly; chrFile.convert = !extractOnly;
chrFile.extract = extractOnly; chrFile.extract = extractOnly;
chrFile.textFormat = textFormat; chrFile.textFormat = textFormat;
if (string.IsNullOrEmpty(inputPath))
{
Console.WriteLine("Input path is required! Use -h to see help screen.");
return;
}
if (string.IsNullOrEmpty(outputPath))
{
var dirName = Path.GetFileNameWithoutExtension(inputPath);
outputPath = Path.Combine(Path.GetDirectoryName(inputPath), dirName);
}
if (batchMode) if (batchMode)
{ {
var files = Directory.GetFiles(inputPath, "*.chr", SearchOption.TopDirectoryOnly); var files = Directory.GetFiles(inputPath, "*.chr", SearchOption.TopDirectoryOnly);
@@ -75,41 +61,20 @@ public class Program
Console.WriteLine(e); Console.WriteLine(e);
throw; throw;
} }
chrFile.Clean();
} }
Log.Line(files.Length == 0 ? "No .chr files found in the input directory." : "Done!", LogLevel.Info); Console.WriteLine(files.Length == 0 ? "No .chr files found in the input directory." : "Done!");
} }
else else
{ {
if (!File.Exists(inputPath)) if (!File.Exists(inputPath))
{ {
Log.Line($"Input file not found: {inputPath}", LogLevel.Error); Console.WriteLine($"Input file not found: {inputPath}");
return; return;
} }
chrFile.Process(inputPath, outputPath); chrFile.Process(inputPath, outputPath);
Log.Line("Done!", LogLevel.Info); Console.WriteLine("Done!");
} }
} }
}
private static void ShowHelp()
{
Console.WriteLine("Converts .CHR files from Dragon Quest VIII (Playstation 2) to .glb/.glTF format.");
Console.WriteLine();
Console.WriteLine("Usage:");
Console.WriteLine(" dq8chr2glb.exe <input_file> <output_dir> [options]");
Console.WriteLine(" dq8chr2glb.exe <input_dir> -b (batch mode: output files are saved in the <input_dir>)");
Console.WriteLine("Examples:");
Console.WriteLine(" dq8chr2glb.exe \"C:\\Users\\Boris\\Desktop\\ChrFormatTest\\ap002.chr\" \"C:\\Users\\Boris\\Desktop\\ChrFormatTest\" -e");
Console.WriteLine(" dq8chr2glb.exe \"C:\\Users\\Boris\\Desktop\\ChrFormatTest\" -b");
Console.WriteLine();
Console.WriteLine("Options:");
Console.WriteLine(" -e - Extract only - unpack .chr without conversion");
Console.WriteLine(" -t - Output as .glTF (text) instead of .glb (binary)");
Console.WriteLine(" -b - Batch mode - process all .chr files in the input directory");
Console.WriteLine();
var line = Console.ReadLine();
}
}
+1
View File
@@ -13,6 +13,7 @@
<PackageReference Include="SharpGLTF.Core" Version="1.0.6" /> <PackageReference Include="SharpGLTF.Core" Version="1.0.6" />
<PackageReference Include="SharpGLTF.Toolkit" Version="1.0.6" /> <PackageReference Include="SharpGLTF.Toolkit" Version="1.0.6" />
<PackageReference Include="SixLabors.ImageSharp" Version="3.1.12" /> <PackageReference Include="SixLabors.ImageSharp" Version="3.1.12" />
<PackageReference Include="System.CommandLine" Version="3.0.0-preview.1.26104.118" />
<PackageReference Include="System.Text.Encoding.CodePages" Version="11.0.0-preview.1.26104.118" /> <PackageReference Include="System.Text.Encoding.CodePages" Version="11.0.0-preview.1.26104.118" />
</ItemGroup> </ItemGroup>