From c7ed2ad73ee8dd60cc4e22e3c8027845d5754cf2 Mon Sep 17 00:00:00 2001 From: Jaex Date: Sun, 20 Sep 2015 11:01:36 +0300 Subject: [PATCH] Added -Task CLI command which will allow uploading with specific task setting Example usage: "Image.png" -Task "Upload to Imgur" --- ShareX.HelpersLib/CLI/CLICommand.cs | 7 +++++ ShareX.HelpersLib/CLI/CLICommandAction.cs | 2 +- ShareX.HelpersLib/CLI/CLIManager.cs | 6 ++--- ShareX/Forms/MainForm.cs | 31 ++++++++++++++++++++--- 4 files changed, 38 insertions(+), 8 deletions(-) diff --git a/ShareX.HelpersLib/CLI/CLICommand.cs b/ShareX.HelpersLib/CLI/CLICommand.cs index f8027f06b..f3516bdb7 100644 --- a/ShareX.HelpersLib/CLI/CLICommand.cs +++ b/ShareX.HelpersLib/CLI/CLICommand.cs @@ -23,6 +23,8 @@ You should have received a copy of the GNU General Public License #endregion License Information (GPL v3) +using System; + namespace ShareX.HelpersLib { public class CLICommand @@ -37,6 +39,11 @@ public CLICommand(string command = null, string parameter = null) Parameter = parameter; } + public bool CheckCommand(string command) + { + return !string.IsNullOrEmpty(Command) && Command.Equals(command, StringComparison.InvariantCultureIgnoreCase); + } + public override string ToString() { string text = string.Format("Command: \"{0}\"", Command); diff --git a/ShareX.HelpersLib/CLI/CLICommandAction.cs b/ShareX.HelpersLib/CLI/CLICommandAction.cs index 45da0b6b3..d02be3fc2 100644 --- a/ShareX.HelpersLib/CLI/CLICommandAction.cs +++ b/ShareX.HelpersLib/CLI/CLICommandAction.cs @@ -46,7 +46,7 @@ public bool CheckCommands(List commands) { foreach (string text in Commands) { - if (command.Command.Equals(text, StringComparison.InvariantCultureIgnoreCase)) + if (command.CheckCommand(text)) { ExecuteAction(command.Parameter); return true; diff --git a/ShareX.HelpersLib/CLI/CLIManager.cs b/ShareX.HelpersLib/CLI/CLIManager.cs index 5d7f840f9..dbca130b3 100644 --- a/ShareX.HelpersLib/CLI/CLIManager.cs +++ b/ShareX.HelpersLib/CLI/CLIManager.cs @@ -139,9 +139,9 @@ public bool IsCommandExist(params string[] commands) command1 = command1.Substring(1); } - foreach (CLICommand command2 in Commands.Where(x => x != null && x.IsCommand && !string.IsNullOrEmpty(x.Command))) + foreach (CLICommand command2 in Commands.Where(x => x != null && x.IsCommand)) { - if (command1.Equals(command2.Command, StringComparison.InvariantCultureIgnoreCase)) + if (command2.CheckCommand(command1)) { return true; } @@ -154,7 +154,7 @@ public bool IsCommandExist(params string[] commands) public string GetParameter(string command) { - CLICommand cliCommand = Commands.Find(x => command.Equals(x.Command, StringComparison.InvariantCultureIgnoreCase)); + CLICommand cliCommand = Commands.Find(x => x.CheckCommand(command)); if (cliCommand != null) { diff --git a/ShareX/Forms/MainForm.cs b/ShareX/Forms/MainForm.cs index 7559349d4..b28d80c59 100644 --- a/ShareX/Forms/MainForm.cs +++ b/ShareX/Forms/MainForm.cs @@ -719,6 +719,8 @@ private void ForceClose() public void UseCommandLineArgs(List commands) { + TaskSettings taskSettings = FindCLITask(commands); + foreach (CLICommand command in commands) { DebugHelper.WriteLine("CommandLine: " + command.Command); @@ -730,11 +732,11 @@ public void UseCommandLineArgs(List commands) if (URLHelpers.IsValidURLRegex(command.Command)) { - UploadManager.DownloadAndUploadFile(command.Command); + UploadManager.DownloadAndUploadFile(command.Command, taskSettings); } else { - UploadManager.UploadFile(command.Command); + UploadManager.UploadFile(command.Command, taskSettings); } } } @@ -743,7 +745,7 @@ private bool CheckCLIHotkey(CLICommand command) { foreach (HotkeyType job in Helpers.GetEnums()) { - if (command.Command.Equals(job.ToString(), StringComparison.InvariantCultureIgnoreCase)) + if (command.CheckCommand(job.ToString())) { ExecuteJob(job); return true; @@ -755,7 +757,7 @@ private bool CheckCLIHotkey(CLICommand command) private bool CheckCLIWorkflow(CLICommand command) { - if (command.Command.Equals("workflow", StringComparison.InvariantCultureIgnoreCase) && !string.IsNullOrEmpty(command.Parameter) && Program.HotkeysConfig != null) + if (Program.HotkeysConfig != null && command.CheckCommand("workflow") && !string.IsNullOrEmpty(command.Parameter)) { foreach (HotkeySettings hotkeySetting in Program.HotkeysConfig.Hotkeys) { @@ -773,6 +775,27 @@ private bool CheckCLIWorkflow(CLICommand command) return false; } + private TaskSettings FindCLITask(List commands) + { + if (Program.HotkeysConfig != null) + { + CLICommand command = commands.FirstOrDefault(x => x.CheckCommand("task") && !string.IsNullOrEmpty(x.Parameter)); + + if (command != null) + { + foreach (HotkeySettings hotkeySetting in Program.HotkeysConfig.Hotkeys) + { + if (command.Parameter == hotkeySetting.TaskSettings.ToString()) + { + return hotkeySetting.TaskSettings; + } + } + } + } + + return null; + } + private WorkerTask[] GetCurrentTasks() { if (lvUploads.SelectedItems.Count > 0)