Skip to content

Commit 51c3ece

Browse files
committed
Fully use CommandLine modelling
1 parent 97a1338 commit 51c3ece

File tree

5 files changed

+68
-66
lines changed

5 files changed

+68
-66
lines changed
Lines changed: 5 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -12,17 +12,15 @@ namespace Hasher
1212
/// Set of options for the test executable
1313
/// </summary>
1414
/// TODO: Add file output
15-
internal sealed class Options : Feature
15+
internal sealed class HashFeature : Feature
1616
{
1717
#region Feature Definition
1818

1919
public const string DisplayName = "options";
2020

21-
/// <remarks>Default feature does not use flags</remarks>
22-
private static readonly string[] _flags = [];
21+
private static readonly string[] _flags = ["hash"];
2322

24-
/// <remarks>Default feature does not use description</remarks>
25-
private const string _description = "";
23+
private const string _description = "Hash all input paths (default)";
2624

2725
#endregion
2826

@@ -42,13 +40,13 @@ internal sealed class Options : Feature
4240

4341
#region Constructors
4442

45-
public Options()
43+
public HashFeature()
4644
: base(DisplayName, _flags, _description)
4745
{
4846
RequiresInputs = true;
4947

5048
Add(new FlagInput("debug", ["-d", "--debug"], "Enable debug mode"));
51-
Add(new StringListInput("type", ["-t", "--type"], "Output file hashes"));
49+
Add(new StringListInput("type", ["-t", "--type"], "Select included hashes"));
5250
}
5351

5452
#endregion
@@ -86,13 +84,6 @@ public override bool ProcessArgs(string[] args, int index)
8684
}
8785
}
8886

89-
// Validate we have any input paths to work on
90-
if (!VerifyInputs())
91-
{
92-
Console.WriteLine("At least one path is required!");
93-
return false;
94-
}
95-
9687
return true;
9788
}
9889

@@ -110,28 +101,6 @@ public override bool Execute()
110101
return true;
111102
}
112103

113-
/// <summary>
114-
/// Display help text
115-
/// </summary>
116-
/// TODO: Replace this with standard help output
117-
public static void DisplayHelp()
118-
{
119-
Console.WriteLine("File Hashing Program");
120-
Console.WriteLine();
121-
Console.WriteLine("Hasher <options> file|directory ...");
122-
Console.WriteLine();
123-
Console.WriteLine("Options:");
124-
Console.WriteLine("-?, -h, --help Display this help text and quit");
125-
Console.WriteLine("-d, --debug Enable debug mode");
126-
Console.WriteLine("-l, --list List all available hashes and quit");
127-
Console.WriteLine("-t, --type [TYPE] Output file hashes");
128-
Console.WriteLine();
129-
Console.WriteLine("If no hash types are provided, this tool will default");
130-
Console.WriteLine("to outputting CRC-32, MD5, SHA-1, and SHA-256.");
131-
Console.WriteLine("Optionally, all supported hashes can be output");
132-
Console.WriteLine("by specifying a value of 'all'.");
133-
}
134-
135104
/// <summary>
136105
/// Wrapper to print hashes for a single path
137106
/// </summary>

Hasher/HelpFeature.cs

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
2+
3+
using SabreTools.CommandLine;
4+
5+
namespace Hasher
6+
{
7+
/// <remarks>Copied implementation from CommandLine until printing is fixed</remarks>
8+
internal sealed class HelpFeature : Feature
9+
{
10+
public const string DisplayName = "Help";
11+
12+
private static readonly string[] _flags = ["-?", "-h", "--help"];
13+
14+
private const string _description = "Show this help";
15+
16+
public HelpFeature()
17+
: base(DisplayName, _flags, _description)
18+
{
19+
RequiresInputs = false;
20+
}
21+
22+
/// <inheritdoc/>
23+
public override bool VerifyInputs() => true;
24+
25+
/// <inheritdoc/>
26+
public override bool Execute() => true;
27+
}
28+
}

Hasher/ListFeature.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44

55
namespace Hasher
66
{
7-
internal class ListFeature : Feature
7+
internal sealed class ListFeature : Feature
88
{
99
#region Feature Definition
1010

Hasher/Program.cs

Lines changed: 23 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
1-
using System.Collections.Generic;
1+
using System;
2+
using System.Collections.Generic;
23
using SabreTools.CommandLine;
3-
using SabreTools.CommandLine.Features;
44

55
namespace Hasher
66
{
@@ -22,10 +22,11 @@ public static class Program
2222
/// Help footer
2323
/// </summary>
2424
private static readonly List<string> _footer = [
25-
"If no hash types are provided, this tool will default",
26-
"to outputting CRC-32, MD5, SHA-1, and SHA-256.",
27-
"Optionally, all supported hashes can be output",
28-
"by specifying a value of 'all'.",
25+
string.Empty,
26+
"If no hash types are provided, this tool will default to",
27+
"outputting CRC-32, MD5, SHA-1, and SHA-256.",
28+
"Optionally, all supported hashes can be output by",
29+
"specifying a value of 'all'.",
2930
];
3031

3132
#endregion
@@ -34,15 +35,15 @@ public static void Main(string[] args)
3435
{
3536
// Build the command set
3637
var commandSet = new CommandSet(_header, _footer);
37-
commandSet.Add(new Help(["-?", "-h", "--help"]));
38+
commandSet.Add(new HelpFeature());
3839
commandSet.Add(new ListFeature());
39-
var options = new Options();
40-
commandSet.Add(options);
40+
var hashFeature = new HashFeature();
41+
commandSet.Add(hashFeature);
4142

4243
// If there are no arguments
4344
if (args.Length == 0)
4445
{
45-
Options.DisplayHelp();
46+
commandSet.OutputAllHelp();
4647
return;
4748
}
4849

@@ -51,26 +52,29 @@ public static void Main(string[] args)
5152

5253
// Check if the feature is recognized
5354
var feature = commandSet.GetTopLevel(featureName);
54-
if (feature is Help)
55+
switch (feature)
5556
{
56-
Options.DisplayHelp();
57-
return;
57+
case HelpFeature: commandSet.OutputAllHelp(); return;
58+
case ListFeature lf: lf.Execute(); return;
5859
}
59-
else if (feature is ListFeature lf)
60+
61+
// Otherwise, process the arguments normally
62+
if (!hashFeature.ProcessArgs(args, 0))
6063
{
61-
lf.Execute();
64+
commandSet.OutputAllHelp();
6265
return;
6366
}
6467

65-
// Otherwise, process the arguments normally
66-
if (!options.ProcessArgs(args, 0))
68+
// If there are no valid inputs
69+
if (hashFeature.RequiresInputs && !hashFeature.VerifyInputs())
6770
{
68-
commandSet.OutputGenericHelp();
71+
Console.WriteLine("At least one path is required!");
72+
commandSet.OutputAllHelp();
6973
return;
7074
}
7175

7276
// Execute based on the options set
73-
options.Execute();
77+
hashFeature.Execute();
7478
}
7579
}
7680
}

README.MD

Lines changed: 11 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -19,16 +19,17 @@ For the latest WIP build here: [Rolling Release](https://github.com/SabreTools/S
1919
```
2020
Hasher <options> file|directory ...
2121
22-
Options:
23-
-?, -h, --help Display this help text and quit
24-
-d, --debug Enable debug mode
25-
-l, --list List all available hashes and quit
26-
-t, --type [TYPE] Output file hashes
27-
28-
If no hash types are provided, this tool will default
29-
to outputting CRC-32, MD5, SHA-1, and SHA-256.
30-
Optionally, all supported hashes can be output
31-
by specifying a value of 'all'.
22+
Available options:
23+
-?, -h, --help Show this help
24+
-l, --list List all available hashes and quit
25+
hash Hash all input paths (default)
26+
-d, --debug Enable debug mode
27+
-t=, --type= Select included hashes
28+
29+
If no hash types are provided, this tool will default to
30+
outputting CRC-32, MD5, SHA-1, and SHA-256.
31+
Optionally, all supported hashes can be output by
32+
specifying a value of 'all'.
3233
```
3334

3435
## Internal Implementations

0 commit comments

Comments
 (0)