Finish up basic CLI. Next up: testing!

This commit is contained in:
Starbeamrainbowlabs 2018-09-22 12:11:47 +01:00
parent 2c2575998a
commit b24f573eb9
Signed by: sbrl
GPG key ID: 1BE5172E637709C2
3 changed files with 30 additions and 7 deletions

View file

@ -28,6 +28,8 @@ namespace SearchBoxCLI
}
class MainClass {
private static List<string> Extras = new List<string>();
private static OperatingModes Mode = OperatingModes.Query;
private static bool Batch = false;
private static string Name = string.Empty;
@ -39,11 +41,10 @@ namespace SearchBoxCLI
public static int Main(string[] args)
{
List<string> extras = new List<string>();
for (int i = 0; i < args.Length; i++)
{
if (!args[i].StartsWith("-")) {
extras.Add(args[i]);
Extras.Add(args[i]);
continue;
}
@ -89,8 +90,8 @@ namespace SearchBoxCLI
return 1;
}
}
if (extras.Count < 1) return HandleHelp();
string modeText = extras.First(); extras.RemoveAt(0);
if (Extras.Count < 1) return HandleHelp();
string modeText = Extras.First(); Extras.RemoveAt(0);
Mode = (OperatingModes)Enum.Parse(typeof(OperatingModes), modeText, true);
switch (Mode) {
@ -111,6 +112,7 @@ namespace SearchBoxCLI
Console.WriteLine();
Console.WriteLine("Usage:");
Console.WriteLine(" ./SearchBox.exe {mode} [options]");
Console.WriteLine(" ./SearchBox.exe query \"{query string}\" [options]");
Console.WriteLine();
Console.WriteLine("Modes:");
Console.WriteLine(" query Query a pre-existing inverted search index");
@ -213,13 +215,25 @@ namespace SearchBoxCLI
private static int HandleQuery()
{
if (Extras.Count < 1) {
Console.Error.WriteLine("Error: No query specified!");
return 1;
}
SearchBox searchBox = JsonConvert.DeserializeObject<SearchBox>(
File.ReadAllText(SearchIndexFilepath)
);
List<SearchResult> results = searchBox.Query(Extras[0], new QuerySettings());
switch (OutputMode)
{
case OutputModes.Json:
Console.WriteLine(JsonConvert.SerializeObject(results));
break;
case OutputModes.Text:
foreach (SearchResult nextResult in results)
Console.WriteLine(nextResult);
break;
}
return 0;

View file

@ -114,6 +114,7 @@ namespace LibSearchBox
List<SearchOffset> offsets = new List<SearchOffset>();
foreach (Tuple<int, string> token in tokenizer.IterateTokens())
offsets.AddRange(index.Query(token.Item2)[pageDef.Key].Select((int offset) => new SearchOffset(token.Item2, offset)));
offsets.Sort((SearchOffset x, SearchOffset y) => x.Offset - y.Offset);
resultsRaw.Add(new SearchResult(
idMap.GetPageName(pageDef.Key),

View file

@ -1,5 +1,7 @@
using System;
using System.Collections.Generic;
using System.Text;
using System.Linq;
namespace LibSearchBox
{
@ -30,6 +32,12 @@ namespace LibSearchBox
Offsets = inOffsets;
}
public override string ToString()
{
StringBuilder result = new StringBuilder();
result.AppendLine($"{Rank.ToString().PadLeft(5)}: {PageName}");
result.Append($" {string.Join(", ", Offsets.Select((SearchOffset nextOffset) => $"{nextOffset.Term} @ {nextOffset.Offset}"))}");
return base.ToString();
}
}
}