|
1 |
| -using System.CommandLine; |
2 |
| -using System.CommandLine.Parsing; |
| 1 | +using CommandLine; |
3 | 2 |
|
4 | 3 | namespace EIV_DataPack.ConsoleApp;
|
5 | 4 |
|
6 | 5 | internal class Program
|
7 | 6 | {
|
8 | 7 | static void Main(string[] args)
|
9 | 8 | {
|
10 |
| - #region options |
11 |
| - var fileOption = new Option<FileInfo>( |
12 |
| - name: "--file", |
13 |
| - description: "The file to to manipulate."); |
14 |
| - |
15 |
| - var dirOption = new Option<DirectoryInfo>( |
16 |
| - name: "--dir", |
17 |
| - description: "The directory to create from."); |
18 |
| - |
19 |
| - var recursiveOption = new Option<bool>( |
20 |
| - name: "--recursive", |
21 |
| - description: "Recursive create from Directory."); |
22 |
| - |
23 |
| - var extractdirOption = new Option<DirectoryInfo>( |
24 |
| - name: "--dir", |
25 |
| - description: "The directory to extract to."); |
26 |
| - |
27 |
| - var exactFile = new Option<string?>( |
28 |
| - name: "--path", |
29 |
| - description: "The exact file path to extract."); |
30 |
| - |
31 |
| - #endregion |
32 |
| - #region commands |
33 |
| - var listCommand = new Command("list", "List all files from the .eivp file") |
34 |
| - { |
35 |
| - fileOption |
36 |
| - }; |
37 |
| - listCommand.SetHandler(ListFiles, fileOption); |
38 |
| - |
39 |
| - var createCommand = new Command("create", "Create .eivp file from the directory") |
40 |
| - { |
41 |
| - fileOption, |
42 |
| - dirOption, |
43 |
| - recursiveOption |
44 |
| - }; |
45 |
| - createCommand.SetHandler(CreateEIVP, fileOption, dirOption, recursiveOption); |
46 |
| - |
47 |
| - var extractCommand = new Command("extract", "Extract specific file from the .eivp Pack to a directory") |
48 |
| - { |
49 |
| - fileOption, |
50 |
| - extractdirOption, |
51 |
| - exactFile |
52 |
| - }; |
53 |
| - extractCommand.SetHandler(ExtractSpecific, fileOption, extractdirOption, exactFile); |
54 |
| - |
55 |
| - #endregion |
56 |
| - |
57 |
| - var rootCommand = new RootCommand("EIV DataPack file manipulator"); |
58 |
| - rootCommand.AddCommand(listCommand); |
59 |
| - rootCommand.AddCommand(createCommand); |
60 |
| - rootCommand.AddCommand(extractCommand); |
61 |
| - rootCommand.Invoke(args); |
| 9 | + bool ret = Parser.Default.ParseArguments<ListOptions, CreateOptions, ExtractOptions, FileVersionOption>(args) |
| 10 | + .MapResult( |
| 11 | + (ListOptions opts) => ListFiles(opts.File), |
| 12 | + (CreateOptions opts) => CreateEIVP(opts.File, opts.Dir, opts.Recursive), |
| 13 | + (ExtractOptions opts) => ExtractSpecific(opts.File, opts.Dir, opts.Path), |
| 14 | + (FileVersionOption opts) => ShowVersion(opts.File), |
| 15 | + errs => false); |
| 16 | + Console.WriteLine(ret); |
62 | 17 | }
|
63 | 18 |
|
64 |
| - static void ListFiles(FileInfo fileInfo) |
| 19 | + static bool ShowVersion(string FileName) |
65 | 20 | {
|
66 |
| - if (fileInfo.Exists) |
| 21 | + if (!File.Exists(FileName)) |
67 | 22 | {
|
68 |
| - var datapackCreator = DatapackCreator.Read(fileInfo.FullName); |
69 |
| - var reader = datapackCreator.GetReader(); |
70 |
| - if (reader == null) |
71 |
| - return; |
72 |
| - reader.ReadFileNames(false); |
73 |
| - reader.Pack.FileNames.ForEach(Console.WriteLine); |
| 23 | + Console.WriteLine("File not Exists!"); |
| 24 | + return false; |
74 | 25 | }
|
75 |
| - else |
| 26 | + var datapackCreator = DatapackManager.Read(FileName); |
| 27 | + var reader = datapackCreator.GetReader(); |
| 28 | + if (reader == null) |
| 29 | + return false; |
| 30 | + Console.WriteLine(reader.Pack.Version); |
| 31 | + return true; |
| 32 | + } |
| 33 | + |
| 34 | + static bool ListFiles(string FileName) |
| 35 | + { |
| 36 | + if (!File.Exists(FileName)) |
76 | 37 | {
|
77 | 38 | Console.WriteLine("File not Exists!");
|
| 39 | + return false; |
78 | 40 | }
|
| 41 | + var datapackCreator = DatapackManager.Read(FileName); |
| 42 | + var reader = datapackCreator.GetReader(); |
| 43 | + if (reader == null) |
| 44 | + return false; |
| 45 | + reader.ReadFileNames(); |
| 46 | + foreach (string item in reader.Pack.FileNames) |
| 47 | + Console.WriteLine(item); |
| 48 | + return true; |
79 | 49 | }
|
80 | 50 |
|
81 |
| - static void CreateEIVP(FileInfo fileInfo, DirectoryInfo directory, bool rec) |
| 51 | + static bool CreateEIVP(string fileName, string directory, bool rec) |
82 | 52 | {
|
83 |
| - var datapack = DatapackCreator.Create(fileInfo.FullName); |
| 53 | + var datapack = DatapackManager.Create(fileName); |
84 | 54 | var writer = datapack.GetWriter();
|
85 | 55 | if (writer == null)
|
86 |
| - return; |
87 |
| - writer.AddDirectory(directory.FullName, rec); |
| 56 | + return false; |
| 57 | + writer.AddDirectory(directory, rec); |
88 | 58 | writer.Save();
|
89 | 59 | Console.WriteLine("File Saved!");
|
| 60 | + return true; |
90 | 61 | }
|
91 | 62 |
|
92 |
| - static void ExtractAll(FileInfo fileInfo, DirectoryInfo directory) |
| 63 | + static bool ExtractSpecific(string fileName, string directory, string? file) |
93 | 64 | {
|
94 |
| - if (fileInfo.Exists) |
95 |
| - { |
96 |
| - var datapackCreator = DatapackCreator.Read(fileInfo.FullName); |
97 |
| - var reader = datapackCreator.GetReader(); |
98 |
| - if (reader == null) |
99 |
| - return; |
100 |
| - reader.ReadFileNames(false); |
101 |
| - |
102 |
| - } |
103 |
| - else |
| 65 | + if (!File.Exists(fileName)) |
104 | 66 | {
|
105 | 67 | Console.WriteLine("File not Exists!");
|
| 68 | + return false; |
106 | 69 | }
|
107 |
| - } |
108 |
| - static void ExtractSpecific(FileInfo fileInfo, DirectoryInfo directory, string? file) |
109 |
| - { |
110 |
| - if (fileInfo.Exists) |
| 70 | + var datapackCreator = DatapackManager.Read(fileName); |
| 71 | + var reader = datapackCreator.GetReader(); |
| 72 | + if (reader == null) |
| 73 | + return false; |
| 74 | + if (file == null) |
111 | 75 | {
|
112 |
| - var datapackCreator = DatapackCreator.Read(fileInfo.FullName); |
113 |
| - var reader = datapackCreator.GetReader(); |
114 |
| - if (reader == null) |
115 |
| - return; |
116 |
| - reader.ReadFileNames(false); |
117 |
| - if (file == null) |
118 |
| - { |
119 |
| - reader.Pack.FileNames.ForEach(x => reader.ExportFile(x, Path.Combine(directory.FullName, x))); |
120 |
| - return; |
121 |
| - } |
122 |
| - if (!reader.Pack.FileNames.Contains(file)) |
123 |
| - { |
124 |
| - Console.WriteLine("File to extract is not exists inside the pack!"); |
125 |
| - return; |
126 |
| - } |
127 |
| - reader.ExportFile(file, Path.Combine(directory.FullName, file)); |
| 76 | + reader.Pack.FileNames.ToList().ForEach(x => reader.ExportFile(x, Path.Combine(directory, x))); |
| 77 | + return false; |
128 | 78 | }
|
129 |
| - else |
| 79 | + if (!reader.Pack.FileNames.Contains(file)) |
130 | 80 | {
|
131 |
| - Console.WriteLine("File not Exists!"); |
| 81 | + Console.WriteLine("File to extract is not exists inside the pack!"); |
| 82 | + return false; |
132 | 83 | }
|
| 84 | + reader.ExportFile(file, Path.Combine(directory, file)); |
| 85 | + return true; |
133 | 86 | }
|
134 | 87 | }
|
0 commit comments