ShareX/ShareX.HelpersLib/CLI/CLIManager.cs

195 lines
5.5 KiB
C#
Raw Normal View History

2013-11-03 23:53:49 +13:00
#region License Information (GPL v3)
/*
ShareX - A program that allows you to take screenshots and share any file type
2024-01-03 12:57:14 +13:00
Copyright (c) 2007-2024 ShareX Team
2013-11-03 23:53:49 +13:00
This program is free software; you can redistribute it and/or
modify it under the terms of the GNU General Public License
as published by the Free Software Foundation; either version 2
of the License, or (at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
Optionally you can also view the license at <http://www.gnu.org/licenses/>.
*/
#endregion License Information (GPL v3)
using System;
using System.Collections.Generic;
2014-11-07 00:27:23 +13:00
using System.Linq;
2014-11-07 01:40:42 +13:00
using System.Text;
2013-11-03 23:53:49 +13:00
2014-12-11 09:25:20 +13:00
namespace ShareX.HelpersLib
2013-11-03 23:53:49 +13:00
{
public class CLIManager
{
2014-11-07 00:27:23 +13:00
public string[] Arguments { get; private set; }
public List<CLICommand> Commands { get; private set; }
public List<CLICommandAction> Actions { get; private set; }
2013-11-03 23:53:49 +13:00
2014-11-07 00:27:23 +13:00
public CLIManager()
{
Commands = new List<CLICommand>();
Actions = new List<CLICommandAction>();
}
2013-11-03 23:53:49 +13:00
2016-04-07 04:02:48 +12:00
public CLIManager(string[] arguments) : this()
2014-11-07 00:27:23 +13:00
{
Arguments = arguments;
}
2013-11-03 23:53:49 +13:00
2016-04-07 04:02:48 +12:00
public CLIManager(string arguments) : this()
2013-11-03 23:53:49 +13:00
{
2014-11-07 01:40:42 +13:00
Arguments = ParseCLI(arguments);
2013-11-03 23:53:49 +13:00
}
2014-11-07 01:40:42 +13:00
public bool ParseCommands()
2013-11-03 23:53:49 +13:00
{
try
{
CLICommand lastCommand = null;
2014-11-07 00:27:23 +13:00
foreach (string argument in Arguments)
2013-11-03 23:53:49 +13:00
{
2014-11-07 00:27:23 +13:00
if (lastCommand == null || argument[0] == '-')
2013-11-03 23:53:49 +13:00
{
2014-11-07 01:40:42 +13:00
CLICommand command = new CLICommand();
2014-11-07 00:27:23 +13:00
if (argument[0] == '-')
{
2014-11-07 01:40:42 +13:00
command.IsCommand = true;
command.Command = argument.Substring(1);
lastCommand = command;
2014-11-07 00:27:23 +13:00
}
else
{
2014-11-07 01:40:42 +13:00
command.Command = argument;
2014-11-07 00:27:23 +13:00
}
2014-11-07 01:40:42 +13:00
Commands.Add(command);
2013-11-03 23:53:49 +13:00
}
else
{
2014-11-07 01:40:42 +13:00
lastCommand.Parameter = argument;
lastCommand = null;
2013-11-03 23:53:49 +13:00
}
}
2014-11-07 00:27:23 +13:00
return true;
2013-11-03 23:53:49 +13:00
}
catch (Exception e)
{
DebugHelper.WriteException(e);
}
2014-11-07 00:27:23 +13:00
return false;
}
2014-11-07 01:40:42 +13:00
private string[] ParseCLI(string arguments)
{
List<string> commands = new List<string>();
bool inDoubleQuotes = false;
for (int i = 0, start = 0; i < arguments.Length; i++)
{
if ((!inDoubleQuotes && char.IsWhiteSpace(arguments[i])) || (inDoubleQuotes && arguments[i] == '"'))
{
string command = arguments.Substring(start, i - start);
if (!string.IsNullOrEmpty(command))
{
commands.Add(command);
}
if (inDoubleQuotes) inDoubleQuotes = false;
start = i + 1;
}
else if (arguments[i] == '"')
{
inDoubleQuotes = true;
start = i + 1;
}
}
return commands.ToArray();
}
2014-11-07 00:27:23 +13:00
public bool IsCommandExist(params string[] commands)
{
if (Commands != null && commands != null)
{
foreach (string command in commands.Where(x => !string.IsNullOrEmpty(x)))
{
string command1 = command;
if (command1[0] == '-')
{
command1 = command1.Substring(1);
}
foreach (CLICommand command2 in Commands.Where(x => x != null && x.IsCommand))
2014-11-07 00:27:23 +13:00
{
if (command2.CheckCommand(command1))
2014-11-07 00:27:23 +13:00
{
return true;
}
}
}
}
return false;
2013-11-03 23:53:49 +13:00
}
2022-10-15 11:28:50 +13:00
public CLICommand GetCommand(string command)
{
return Commands.Find(x => x.CheckCommand(command));
}
public string GetParameter(string command)
{
2022-10-15 11:28:50 +13:00
CLICommand cliCommand = GetCommand(command);
if (cliCommand != null)
{
return cliCommand.Parameter;
}
return null;
}
2013-11-03 23:53:49 +13:00
public void ExecuteActions()
{
foreach (CLICommandAction action in Actions)
{
action.CheckCommands(Commands);
}
}
2014-11-07 01:40:42 +13:00
public override string ToString()
{
StringBuilder sb = new StringBuilder();
foreach (CLICommand command in Commands)
{
if (sb.Length > 0)
{
sb.AppendLine();
}
sb.Append(command);
}
return sb.ToString();
}
2013-11-03 23:53:49 +13:00
}
}