Skip to content

Commit

Permalink
boolean option flags not working at all.
Browse files Browse the repository at this point in the history
  • Loading branch information
lavedon committed Jun 19, 2021
1 parent d3fcc12 commit a8ec78e
Show file tree
Hide file tree
Showing 463 changed files with 809 additions and 551 deletions.
2 changes: 1 addition & 1 deletion .vscode/launch.json
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
// If you have changed target frameworks, make sure to update the program path.
"program": "${workspaceFolder}/bin/Debug/net5.0/OxfordV2.dll",
"args": [
"-w \"computer\""
"computer", "-o"
],
"cwd": "${workspaceFolder}",
// For more information about the 'console' field, see https://aka.ms/VSCode-CS-LaunchJson-Console
Expand Down
3 changes: 2 additions & 1 deletion .vscode/settings.json
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
{
"C_Cpp_Runner.cCompilerPath": "C:\\ProgramData\\chocolatey\\bin\\gcc.exe",
"C_Cpp_Runner.cppCompilerPath": "C:\\ProgramData\\chocolatey\\bin\\g++.exe"
"C_Cpp_Runner.cppCompilerPath": "C:\\ProgramData\\chocolatey\\bin\\g++.exe",
"git.ignoreLimitWarning": true
}
18 changes: 15 additions & 3 deletions API.cs
Original file line number Diff line number Diff line change
Expand Up @@ -206,14 +206,20 @@ public static void APICalls(CurrentQuery query)
// I am removing the limit of 1 definition -- going to return all at once
// Uri requestURL = new Uri(baseURL + "words/?lemma=" + query.UserEnteredWord + "&limit=1");
string queryURL;
if ((! query.IncludeObsolete) || (! query.OptionsMenuIncludeObsolete)) {

if (query.IncludeObsolete.HasValue) {
if (!query.IncludeObsolete.Value) {
queryURL = @"words/?lemma=" + query.UserEnteredWord + @"&obsolete=false";
} else {
queryURL = @"words/?lemma=" + query.UserEnteredWord + @"&obsolete=true";
}
}
else {
queryURL = @"words/?lemma=" + query.UserEnteredWord;
}

Uri requestURL = new Uri(baseURL + queryURL);
Console.WriteLine($"requestURL: {requestURL.ToString()}");
Trace.WriteLine("Making the request");

var response = client.GetStreamAsync(requestURL).Result;
Expand All @@ -228,12 +234,18 @@ public static void APICalls(CurrentQuery query)
{
Trace.WriteLine("Called callSensesAPI");
string queryURL;
if ((! query.IncludeObsolete) || (! query.OptionsMenuIncludeObsolete)) {
queryURL = @"word/" + query.Definitions[0].WordID + @"/senses/?obsolete=false";
if (query.IncludeObsolete.HasValue) {
if (!query.IncludeObsolete.Value) {
queryURL = @"word/" + query.Definitions[0].WordID + @"/senses/?obsolete=false";
}
else {
queryURL = @"word/" + query.Definitions[0].WordID + @"/senses/?obsolete=true";
}
}
else {
queryURL = @"word/" + query.Definitions[0].WordID + @"/senses/";
}

Uri requestURL = new Uri(baseURL + queryURL);
Trace.WriteLine("Making the request");
try {
Expand Down
6 changes: 2 additions & 4 deletions CurrentQuery.cs
Original file line number Diff line number Diff line change
Expand Up @@ -32,9 +32,8 @@ public class CurrentQuery : IDisposable
public Modes QueryMode { get; set; }

public bool HasLookedUpWord { get; set; }
public bool OptionsMenuIncludeObsolete { get; set; }
public bool OptionsMenuVerboseMode { get; set;}
public bool IncludeObsolete { get; set; }
public bool? IncludeObsolete { get; set; }
public bool DateRangeSet { get; set; }

public int NumberOfQuotes { get; set; }
Expand All @@ -46,8 +45,7 @@ public class CurrentQuery : IDisposable
public CurrentQuery() {
this.HasLookedUpWord = false;
this.DateRangeSet = false;
this.IncludeObsolete = true;
this.OptionsMenuIncludeObsolete = true;
this.IncludeObsolete = null;
this.OptionsMenuVerboseMode = true;
this.Definitions = new List<Definition>();
this.Quotes = new List<Quote>();
Expand Down
4 changes: 2 additions & 2 deletions OptionsMenu.cs
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ public static void ShowOptionsMenu(CurrentQuery query)
}
Console.WriteLine($"Delete old XML file and saved quotations on export?: {yesNo}");

if (query.OptionsMenuIncludeObsolete == true)
if (query.IncludeObsolete == true)
{
yesNo = "Yes";
}
Expand Down Expand Up @@ -89,7 +89,7 @@ public static void ShowOptionsMenu(CurrentQuery query)
Console.WriteLine("Toggle Obsolete");
query.HasLookedUpWord = false;
query.Definitions.Clear();
query.OptionsMenuIncludeObsolete = !query.OptionsMenuIncludeObsolete;
query.IncludeObsolete = !query.IncludeObsolete;
ConsoleUI.MainMenu(query);
break;

Expand Down
176 changes: 96 additions & 80 deletions Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,97 +2,113 @@
using System.Diagnostics;
using System.CommandLine;
using System.CommandLine.Invocation;
using System.Reflection;
using System.IO;

namespace OxfordV2
{
class Program
{

static int Main(string[] args)
{
// Show Trace on console
// TextWriterTraceListener tr1 = new TextWriterTraceListener(System.Console.Out);
// Trace.Listeners.Add(tr1);

// Try to create the logs directory
var wordOption = new Option<string>(
"--word",
description: "The word to look up. Most advanced features first require a WordID which we can only acquire by looking up a word"
);

wordOption.AddAlias("-w");
wordOption.IsRequired = false;

var obsoleteOption = new Option<bool>(
"--obsolete-exclude",
description: "Do NOT include obsolete words. (Default is to include BOTH obsolete and non obsolete words)"
);
obsoleteOption.AddAlias("-oe");

var onlyObsoleteOption = new Option<bool>(
"--obsolete-only",
description: "ONLY return obsolete words. (Default is to include BOTH obsolete and non obsolete words)"
);
onlyObsoleteOption.AddAlias("-o");


var rootCommand = new RootCommand
{
wordOption,
obsoleteOption,
onlyObsoleteOption
};

rootCommand.Description = "An app which processes the Oxford English Dictionary Researcher API, and exports to SuperMemo.";
rootCommand.Handler = CommandHandler.Create<string, string, bool, bool>((word, etomny, obsoleteOption, onlyObsoleteOption) =>
{
Trace.WriteLine($"CLI word entered was {word}");
CurrentQuery query = new();
if(string.IsNullOrWhiteSpace(word)) {
Console.WriteLine("No CLI word entered.");
ConsoleUI.Start(query);
} else {
Console.WriteLine($"Entered CLI word is {word}");
ConsoleUI.Start(word, query);
// Show Trace on console
// TextWriterTraceListener tr1 = new TextWriterTraceListener(System.Console.Out);
// Trace.Listeners.Add(tr1);

// Try to create the logs directory
/*
*/
Console.WriteLine($"args are: {args.ToString()}");
foreach (var a in args){
Console.WriteLine(a);
}
Console.WriteLine($"args.Length: {args.Length}");
Console.ReadLine();

/*
var excludeObsoleteOption = new Option(new[] {
"--obsolete-exclude","-eo"},
description: "Exclude obsolete usages."
);
*/


var rootCommand = new RootCommand
{
new Argument<string>("Word"),
new Option<bool>("-o" , description: "Only return obsolete usages.")
};

rootCommand.Description = "An app which processes the Oxford English Dictionary Researcher API, and exports to SuperMemo.";
rootCommand.Handler = CommandHandler.Create<string, bool, IConsole>(
HandleArgs);


string directoryPath = string.Concat(Environment.CurrentDirectory, "\\logs");

try
{
// Determine whether the directory exists
if (Directory.Exists(directoryPath))
{
Trace.WriteLine("The logs path already exists.");

}
else
{
// Try to create the directory
DirectoryInfo di = Directory.CreateDirectory(directoryPath);
}
string fullPath = string.Concat(Environment.CurrentDirectory, $"\\logs\\Log_OxfordApplication_{DateTime.Now.ToString("yyyyMMdd-HHmm")}.txt");
Trace.WriteLine("Path is {0}", fullPath);
TextWriterTraceListener tr2 = new TextWriterTraceListener(System.IO.File.CreateText(fullPath));

Trace.Listeners.Add(tr2);
}

catch (Exception e)
{
Console.WriteLine("The process failed: {0}", e.ToString());
}
finally { }

Trace.WriteLine("Leaving Main method.");
Trace.Flush();
// ConsoleUI.Start();
// @TODO Make an overload of ConsoleUI.Start(which passes a word from the command line)
return rootCommand.Invoke(args);

});


string directoryPath = string.Concat(Environment.CurrentDirectory, "\\logs");

try
{
// Determine whether the directory exists
if (Directory.Exists(directoryPath))
{
Trace.WriteLine("The logs path already exists.");

}
else
{
// Try to create the directory
DirectoryInfo di = Directory.CreateDirectory(directoryPath);
}
string fullPath = string.Concat(Environment.CurrentDirectory, $"\\logs\\Log_OxfordApplication_{DateTime.Now.ToString("yyyyMMdd-HHmm")}.txt");
Trace.WriteLine("Path is {0}", fullPath);
TextWriterTraceListener tr2 = new TextWriterTraceListener(System.IO.File.CreateText(fullPath));

Trace.Listeners.Add(tr2);
}

catch (Exception e)
{
Console.WriteLine("The process failed: {0}", e.ToString());
}
finally {}

Trace.WriteLine("Leaving Main method.");
Trace.Flush();
// ConsoleUI.Start();
// @TODO Make an overload of ConsoleUI.Start(which passes a word from the command line)
return rootCommand.Invoke(args);
}

public static void HandleArgs(string word, bool obsoleteOnlyOption, IConsole console)
{
Console.WriteLine($"CLI word entered was {word}");
Console.WriteLine($"obsoleteOnlyOption: {obsoleteOnlyOption}");
Console.ReadLine();
// Console.WriteLine($"excludeObsoleteOption: {excludeObsoleteOption}.");
CurrentQuery query = new();
if (obsoleteOnlyOption)
{
query.IncludeObsolete = true;
}
/*
if (excludeObsoleteOption)
{
query.IncludeObsolete = false;
}
*/
if (string.IsNullOrWhiteSpace(word))
{
Console.WriteLine("No CLI word entered.");
ConsoleUI.Start(query);
}
else
{
Console.WriteLine($"Entered CLI word is {word}");
Console.WriteLine($"query.IncludeObsolete: {query.IncludeObsolete}");
ConsoleUI.Start(word, query);
}
}
}
}
21 changes: 21 additions & 0 deletions WithHandlerExtension.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
using System;
using System.Reflection;
using System.CommandLine;
using System.CommandLine.Invocation;

namespace OxfordV2
{
public static class WithHandlerExtension
{
public static Command WithHandler(this Command command, string name)
{
var flags = BindingFlags.NonPublic | BindingFlags.Static;
var method = typeof(Program).GetMethod(name, flags);

// method!
var handler = CommandHandler.Create(method!);
command.Handler = handler;
return command;
}
}
}
Binary file modified bin/Debug/net5.0/OxfordV2.dll
Binary file not shown.
Binary file modified bin/Debug/net5.0/OxfordV2.pdb
Binary file not shown.
Binary file modified bin/Debug/net5.0/ref/OxfordV2.dll
Binary file not shown.
Binary file modified bin/Release/net5.0/win10-x64/Microsoft.CSharp.dll
Binary file not shown.
Binary file modified bin/Release/net5.0/win10-x64/Microsoft.VisualBasic.Core.dll
Binary file not shown.
Binary file modified bin/Release/net5.0/win10-x64/Microsoft.VisualBasic.dll
Binary file not shown.
Binary file modified bin/Release/net5.0/win10-x64/Microsoft.Win32.Primitives.dll
Binary file not shown.
Binary file modified bin/Release/net5.0/win10-x64/Microsoft.Win32.Registry.dll
Binary file not shown.
Loading

0 comments on commit a8ec78e

Please sign in to comment.