Skip to content

Commit a3118ce

Browse files
committed
Use CommandLine library for executable
1 parent daa72eb commit a3118ce

File tree

3 files changed

+71
-139
lines changed

3 files changed

+71
-139
lines changed

ProtectionScan/Options.cs

Lines changed: 0 additions & 127 deletions
This file was deleted.

ProtectionScan/Program.cs

Lines changed: 70 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,24 @@
22
using System.Collections.Generic;
33
using System.IO;
44
using BinaryObjectScanner;
5+
using SabreTools.CommandLine;
6+
using SabreTools.CommandLine.Inputs;
57

68
namespace ProtectionScan
79
{
810
class Program
911
{
12+
#region Constants
13+
14+
private const string _debugName = "debug";
15+
private const string _helpName = "help";
16+
private const string _noArchivesName = "no-archives";
17+
private const string _noContentsName = "no-contents";
18+
private const string _noPathsName = "no-paths";
19+
private const string _noSubdirsName = "no-subdirs";
20+
21+
#endregion
22+
1023
static void Main(string[] args)
1124
{
1225
#if NET462_OR_GREATER || NETCOREAPP || NETSTANDARD2_0_OR_GREATER
@@ -18,32 +31,77 @@ static void Main(string[] args)
1831
var fileProgress = new Progress<ProtectionProgress>();
1932
fileProgress.ProgressChanged += Changed;
2033

21-
// Get the options from the arguments
22-
var options = Options.ParseOptions(args);
34+
// Create the command set
35+
var commandSet = CreateCommands();
2336

24-
// If we have an invalid state
25-
if (options == null)
37+
// If we have no args, show the help and quit
38+
if (args == null || args.Length == 0)
2639
{
27-
Options.DisplayHelp();
40+
commandSet.OutputAllHelp();
41+
return;
42+
}
43+
44+
// Loop through and process the options
45+
int firstFileIndex = 0;
46+
for (; firstFileIndex < args.Length; firstFileIndex++)
47+
{
48+
string arg = args[firstFileIndex];
49+
50+
var input = commandSet.GetTopLevel(arg);
51+
if (input == null)
52+
break;
53+
54+
input.ProcessInput(args, ref firstFileIndex);
55+
}
56+
57+
// If help was specified
58+
if (commandSet.GetBoolean(_helpName))
59+
{
60+
commandSet.OutputAllHelp();
2861
return;
2962
}
3063

3164
// Create scanner for all paths
3265
var scanner = new Scanner(
33-
options.ScanArchives,
34-
options.ScanContents,
35-
options.ScanPaths,
36-
options.ScanSubdirectories,
37-
options.Debug,
66+
!commandSet.GetBoolean(_noArchivesName),
67+
!commandSet.GetBoolean(_noContentsName),
68+
!commandSet.GetBoolean(_noPathsName),
69+
!commandSet.GetBoolean(_noSubdirsName),
70+
!commandSet.GetBoolean(_debugName),
3871
fileProgress);
3972

4073
// Loop through the input paths
41-
foreach (string inputPath in options.InputPaths)
74+
for (int i = firstFileIndex; i < args.Length; i++)
4275
{
43-
GetAndWriteProtections(scanner, inputPath);
76+
string arg = args[i];
77+
GetAndWriteProtections(scanner, arg);
4478
}
4579
}
4680

81+
/// <summary>
82+
/// Create the command set for the program
83+
/// </summary>
84+
private static CommandSet CreateCommands()
85+
{
86+
List<string> header = [
87+
"Protection Scanner",
88+
string.Empty,
89+
"ProtectionScan <options> file|directory ...",
90+
string.Empty,
91+
];
92+
93+
var commandSet = new CommandSet(header);
94+
95+
commandSet.Add(new FlagInput(_helpName, ["-?", "-h", "--help"], "Display this help text"));
96+
commandSet.Add(new FlagInput(_debugName, ["-d", "--debug"], "Enable debug mode"));
97+
commandSet.Add(new FlagInput(_noContentsName, ["-nc", "--no-contents"], "Disable scanning for content checks"));
98+
commandSet.Add(new FlagInput(_noArchivesName, ["-na", "--no-archives"], "Disable scanning archives"));
99+
commandSet.Add(new FlagInput(_noPathsName, ["-np", "--no-paths"], "Disable scanning for path checks"));
100+
commandSet.Add(new FlagInput(_noSubdirsName, ["-ns", "--no-subdirs"], "Disable scanning subdirectories"));
101+
102+
return commandSet;
103+
}
104+
47105
/// <summary>
48106
/// Wrapper to get and log protections for a single path
49107
/// </summary>

ProtectionScan/ProtectionScan.csproj

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@
3232
</ItemGroup>
3333

3434
<ItemGroup>
35+
<PackageReference Include="SabreTools.CommandLine" Version="[1.3.2]" />
3536
<PackageReference Include="SabreTools.Serialization" Version="[2.0.1]" />
3637
</ItemGroup>
3738

0 commit comments

Comments
 (0)