ShareX/HelpersLib/CLI/CLIManager.cs

135 lines
4.1 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
2014-05-13 21:06:40 +12:00
Copyright (C) 2007-2014 ShareX Developers
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;
2013-11-03 23:53:49 +13:00
2014-11-07 00:27:23 +13:00
namespace 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
2014-11-07 00:27:23 +13:00
public CLIManager(string[] arguments)
: this()
{
Arguments = arguments;
}
2013-11-03 23:53:49 +13:00
2014-11-07 00:27:23 +13:00
public CLIManager(string arguments)
: this()
2013-11-03 23:53:49 +13:00
{
2014-11-07 00:27:23 +13:00
CLIParser parser = new CLIParser();
Arguments = parser.Parse(arguments).ToArray();
2013-11-03 23:53:49 +13:00
}
public bool Parse()
{
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 00:27:23 +13:00
lastCommand = new CLICommand();
if (argument[0] == '-')
{
lastCommand.IsCommand = true;
lastCommand.Command = argument.Substring(1);
}
else
{
lastCommand.Command = argument;
}
2013-11-03 23:53:49 +13:00
Commands.Add(lastCommand);
}
2014-11-07 00:27:23 +13:00
else if (string.IsNullOrEmpty(lastCommand.Parameter))
2013-11-03 23:53:49 +13:00
{
2014-11-07 00:27:23 +13:00
lastCommand.Parameter = argument;
2013-11-03 23:53:49 +13:00
}
else
{
2014-11-07 00:27:23 +13:00
throw new Exception("Argument not starting with '-' or more than one parameter exist.");
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;
}
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 && !string.IsNullOrEmpty(x.Command)))
{
if (command1.Equals(command2.Command, StringComparison.InvariantCultureIgnoreCase))
{
return true;
}
}
}
}
return false;
2013-11-03 23:53:49 +13:00
}
public void ExecuteActions()
{
foreach (CLICommandAction action in Actions)
{
action.CheckCommands(Commands);
}
}
}
}