Skip to content

Commit

Permalink
[#] Refactor CLI 2 #25
Browse files Browse the repository at this point in the history
  • Loading branch information
huiyadanli committed May 16, 2021
1 parent dd84b6f commit 561ab97
Showing 1 changed file with 71 additions and 65 deletions.
136 changes: 71 additions & 65 deletions PasteEx/Util/CLIHelper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -17,140 +17,142 @@ public class CLIParams
public const string PASTE = "paste";
public const string MONITOR = "monitor";

public static readonly string[] COMMAND_ARRAY = new string[] { REG, UNREG, PASTE, MONITOR };
public static readonly string[] ACTION_ARRAY = new string[] { REG, UNREG, PASTE, MONITOR };

/** parameters **/
public const char NORMAL = 'n';
public const char QUICK = 'q';
public const char SHIFT = 's';
public const char FORCE = 'f';
public const string NORMAL = "-n";
public const string QUICK = "-q";
public const string SHIFT = "-s";
public const string FORCE = "-f";

public static readonly char[] PARAMETER_ARRAY = new char[] { NORMAL, QUICK, SHIFT, FORCE };
public static readonly string[] PARAMETER_ARRAY = new string[] { NORMAL, QUICK, SHIFT, FORCE };
}

public class CLIHelper
{

private string command;
private string action;

private List<char> parameters;
private List<string> parameters = new List<string>();

private string path;

public CLIHelper(string[] args)
{
if (args.Length > 0)
{
bool isNotEmpty = ParseCommand(args[0]);
if (!isNotEmpty)
{
// eg. PasteEx [-q] path
if (ParseParameters(args[0]))
{
ParsePath(args[1]);
}
else
{
ParsePath(args[0]);
}
}
else
{
// Full command
if (args.Length > 1)
{
if (!ParseParameters(args[1]))
{
throw new ArgumentException(Resources.Strings.TipParseCommandError);
}
if (args.Length > 2)
{
ParsePath(args[2]);
}
}
}
ParseCommand(args);
}
}

private bool ParseCommand(string str)
private void ParseCommand(string[] args)
{
List<string> commands = new List<string>(CLIParams.COMMAND_ARRAY);
if (commands.Contains(str))
List<string> argList = new List<string>();

// paste can be omitted.
// eg. PasteEx [-q] path
if (args[0].Contains("-"))
{
argList.Add(CLIParams.PASTE);
}
argList.AddRange(args);

if (CLIParams.ACTION_ARRAY.Contains(argList[0]))
{
command = str;
return true;
action = argList[0];
}
else
{
// Maybe it's empty.
// eg. PasteEx [-q] location [filename]
return false;
Console.WriteLine("");
return;
}
}

private bool ParseParameters(string str)
{
if (str.StartsWith("-"))
int i;
for (i = 1; i < argList.Count; i++)
{
if (argList[i].StartsWith("-"))
{
parameters.Add(argList[i]);
}
else
{
break;
}
}

if (i <= argList.Count - 1 && action == CLIParams.PASTE)
{
parameters = str.Substring(1).ToList();
return true;
path = DealWithPath(argList[i]);
}
return false;
}

private void ParsePath(string str)
private string DealWithPath(string str)
{
// why the disk root directory has '"' ??
if (str.LastIndexOf('"') == str.Length - 1)
{
str = str.Substring(0, str.Length - 1);
}
path = str;
return str;
}

public void Execute()
{
if (command == CLIParams.REG)
if (action == CLIParams.REG)
{
RightMenu.ShiftSetting shift = HasThisParam(CLIParams.SHIFT) ? RightMenu.ShiftSetting.True : RightMenu.ShiftSetting.False;
RightMenu.QuickSetting quick = HasThisParam(CLIParams.QUICK) ? RightMenu.QuickSetting.True : RightMenu.QuickSetting.False;
RightMenu.Add(shift, quick);
}
else if (command == CLIParams.UNREG)
else if (action == CLIParams.UNREG)
{
RightMenu.QuickSetting quick = HasThisParam(CLIParams.QUICK) ? RightMenu.QuickSetting.True : RightMenu.QuickSetting.False;
RightMenu.Delete(quick);
}
else if (command == CLIParams.MONITOR)
else if (action == CLIParams.MONITOR)
{
if (ApplicationHelper.IsPasteExMonitorModeProcessExist())
{
CommandLine.Error(Resources.Strings.TipMonitorProcessExisted);
MessageBox.Show(Resources.Strings.TipMonitorProcessExisted,
Resources.Strings.TitleError, MessageBoxButtons.OK, MessageBoxIcon.Information);
return;
}
// Monitor mode
Application.Run(new FormMain(null));
}
else
else if (action == CLIParams.PASTE)
{
if (HasThisParam(CLIParams.QUICK))
{
// Quick paste mode
bool forceOverWrite = HasThisParam(CLIParams.FORCE);

if (File.Exists(path))
{
ModeController.QuickPasteEx(Path.GetDirectoryName(path), Path.GetFileName(path), forceOverWrite);
string directory = Path.GetDirectoryName(path);
if (string.IsNullOrEmpty(directory))
{
Console.WriteLine(Resources.Strings.TipTargetPathNotExist);
MessageBox.Show(Resources.Strings.TipTargetPathNotExist,
Resources.Strings.Title, MessageBoxButtons.OK, MessageBoxIcon.Warning);
}
ModeController.QuickPasteEx(directory, Path.GetFileName(path), forceOverWrite);
}
else if (Directory.Exists(path))
{
ModeController.QuickPasteEx(path, null, forceOverWrite);
}
else
{
Console.WriteLine(Resources.Strings.TipTargetPathNotExist);
MessageBox.Show(Resources.Strings.TipTargetPathNotExist,
Resources.Strings.Title, MessageBoxButtons.OK, MessageBoxIcon.Warning);
string directory = Path.GetDirectoryName(path);
if(string.IsNullOrEmpty(directory))
{
Console.WriteLine(Resources.Strings.TipTargetPathNotExist);
MessageBox.Show(Resources.Strings.TipTargetPathNotExist,
Resources.Strings.Title, MessageBoxButtons.OK, MessageBoxIcon.Warning);
}
ModeController.QuickPasteEx(directory, Path.GetFileName(path), forceOverWrite);
}
}
else
Expand Down Expand Up @@ -180,9 +182,13 @@ public void Execute()
}
}
}
else
{
Application.Run(new FormMain());
}
}

private bool HasThisParam(char c)
private bool HasThisParam(string c)
{
if (parameters != null)
{
Expand All @@ -199,22 +205,22 @@ public static string GenerateCmdReg(
ShiftSetting shift = ShiftSetting.False,
QuickSetting quick = QuickSetting.False)
{
string cmd = CLIParams.REG + " -";
string cmd = CLIParams.REG + " ";

if (quick == QuickSetting.True)
cmd += CLIParams.QUICK;
else
cmd += CLIParams.NORMAL;

if (shift == ShiftSetting.True)
cmd += CLIParams.SHIFT;
cmd += " " + CLIParams.SHIFT;

return cmd;
}

public static string GenerateCmdUnReg(QuickSetting quick = QuickSetting.False)
{
string cmd = CLIParams.UNREG + " -";
string cmd = CLIParams.UNREG + " ";
if (quick == QuickSetting.True)
cmd += CLIParams.QUICK;
else
Expand Down

0 comments on commit 561ab97

Please sign in to comment.