public override void RunCommand() { ICommandLineCommand cmd = CmdLine.FindCommand(Command); if (cmd == null) { throw new CommandLineError("DAE-00154 Unknown command:" + Command); } ICommandLineCommandInstance inst = cmd.CreateInstance(); List <CmdLine.ParamHolder> holders = CmdLine.LoadHolders(new object[] { inst }); Dictionary <int, string> posparams = new Dictionary <int, string>(); foreach (CmdLine.ParamHolder holder in holders) { if (holder.Position != null) { posparams[holder.Position.Value] = holder.Name; } } List <string> posparamslist = new List <string>(); while (posparams.Count > 0) { int min = PyList.Minimum(posparams.Keys); posparamslist.Add(posparams[min]); posparams.Remove(min); } Console.Out.WriteLine("Usage: daci " + Command + " " + String.Join(" ", posparamslist.ToArray()) + " [--param1 value1 --param2 value2...]"); Console.Out.WriteLine(cmd.Description); foreach (CmdLine.ParamHolder holder in holders) { Console.WriteLine(" " + holder.Name + " - " + holder.Description); } }
public ICommandLineCommand ParseStandardCommandLine(ICommandLineArguments arguments) { ICommandLineCommand command = null; var parser = arguments.Parse(); while (command?.StopParsing != true && String.IsNullOrEmpty(parser.ErrorArgument) && parser.TryGetNextSwitchOrArgument(out var arg)) { if (String.IsNullOrWhiteSpace(arg)) // skip blank arguments. { continue; } // First argument must be the command or global switch (that creates a command). if (command == null) { if (!this.TryParseUnknownCommandArg(arg, parser, out command)) { parser.ReportErrorArgument(arg, ErrorMessages.HarvestTypeNotFound(arg)); } } else if (!command.TryParseArgument(parser, arg)) { parser.ReportErrorArgument(arg); } } return(command ?? new HelpCommand(this.extensions)); }
static bool ProcessDefaultCommand(string[] args, ICommandLineCommand defaultCommand, int startingArgIndex) { //Console.WriteLine("No default command line. Further processing is not possible."); Trace.TraceInformation("Processing default command '"); if (defaultCommand == null) { Console.WriteLine("No default command line. Further processing is not possible."); return(false);; } if (args == null || args.Length <= 0) { Trace.TraceInformation("Command line arguments are not provided."); Trace.TraceInformation("Execute the default command with no arguments"); defaultCommand.Execute(args, 0); } else { if (args[0].Trim().ToLower() == defaultCommand.Name.Trim().ToLower()) { Trace.TraceInformation("Execute default command with first argument as command name..."); defaultCommand.Execute(args, 1); } else { Trace.TraceInformation("Execute default command with no command name in the arguments..."); defaultCommand.Execute(args, 0); } } return(true); }
public bool TryParseUnknownCommandArg(string arg, ICommandLineParser parser, out ICommandLineCommand command) { command = null; if (parser.IsSwitch(arg)) { var parameter = arg.Substring(1); switch (parameter.ToLowerInvariant()) { case "?": case "h": case "help": command = new HelpCommand(this.extensions); return(true); } } foreach (var heatExtension in this.extensions) { if (heatExtension.CommandLineTypes.Any(o => o.Option == arg)) { command = new HeatCommand(arg, this.extensions, this.serviceProvider); return(true); } } return(false); }
public static void PrintHelp() { Console.Out.WriteLine("List of commands"); foreach (var item in CommandLineCommandAddonType.Instance.CommonSpace.GetAllAddons()) { ICommandLineCommand cmd = (ICommandLineCommand)item.InstanceModel; Console.Out.WriteLine(" " + cmd.Name); } Console.Out.WriteLine("Try \"daci help command\" for help about command"); }
private static string CreateMessage(ICommandLineCommand command, IReadOnlyCollection <Exception> exceptions) { if (exceptions.Count > 1) { return(CreateMultipleExceptionsMessage(command, exceptions)); } else { return(CreateSingleExceptionMessage(command, exceptions.First())); } }
private static string CreateMultipleExceptionsMessage(ICommandLineCommand command, IReadOnlyCollection <Exception> exceptions) { var message = new StringBuilder(); message.AppendLine($"Unable to parse command '{command.Name}' because {exceptions.Count} errors occured"); foreach (var exception in exceptions) { message.AppendLine($" - {exception.Message}"); } return(message.ToString()); }
private ICommandLineCommand Parse(ICommandLineContext context) { var extensions = this.ExtensionManager.Create <IExtensionCommandLine>(); foreach (var extension in extensions) { extension.PreParse(context); } ICommandLineCommand command = null; var parser = context.Arguments.Parse(); while (command?.StopParsing != true && String.IsNullOrEmpty(parser.ErrorArgument) && parser.TryGetNextSwitchOrArgument(out var arg)) { if (String.IsNullOrWhiteSpace(arg)) // skip blank arguments. { continue; } // First argument must be the command or global switch (that creates a command). if (command == null) { if (!this.TryParseUnknownCommandArg(arg, parser, out command, extensions)) { parser.ErrorArgument = arg; } } else if (parser.IsSwitch(arg)) { if (!command.TryParseArgument(parser, arg) && !TryParseCommandLineArgumentWithExtension(arg, parser, extensions)) { parser.ErrorArgument = arg; } } else if (!TryParseCommandLineArgumentWithExtension(arg, parser, extensions) && command?.TryParseArgument(parser, arg) == false) { parser.ErrorArgument = arg; } } foreach (var extension in extensions) { extension.PostParse(); } return(command ?? new HelpCommand()); }
private bool CommandsAreEqual(ICommandLineCommand command, ICommandLineCommand otherCommand, StringComparison stringComparison) { if (command == null && otherCommand == null) { return(true); } if (command == null) { return(false); } if (otherCommand == null) { return(false); } return(string.Equals(command.Name, otherCommand.Name, stringComparison)); }
private static void Main(string[] args) { var commands = new ICommandLineCommand[] { new PipelineProcessorCommand(), //new FilterProcessorCommand(), new AnnotationProcessorCommand(), new ValidationProcessorCommand(), new ExtractProcessorCommand(), new SomaticMutationTableBuilderCommand() }.ToDictionary(m => m.Name.ToLower()); SoftwareInfo.SoftwareName = GlmvcAssembly.Name; SoftwareInfo.SoftwareVersion = GlmvcAssembly.Version; if (!SystemUtils.IsLinux) { AttachConsole(AttachParentProcess); } ICommandLineCommand command; if (args.Length == 0) { ShowUsage(commands); } else if (commands.TryGetValue(args[0].ToLower(), out command)) { if (command.Process(args.Skip(1).ToArray())) { Console.WriteLine("Done!"); } else { Console.Error.WriteLine("Failed!"); } } else { Console.WriteLine("Error command " + args[0] + "."); ShowUsage(commands); } }
private static void Main(string[] args) { var commands = new ICommandLineCommand[] { new PipelineProcessorCommand(), //new FilterProcessorCommand(), new AnnotationProcessorCommand(), new ValidationProcessorCommand(), new ExtractProcessorCommand(), new SomaticMutationTableBuilderCommand() }.ToDictionary(m => m.Name.ToLower()); if (!SystemUtils.IsLinux) { AttachConsole(AttachParentProcess); } ICommandLineCommand command; if (args.Length == 0) { ShowUsage(commands); } else if (commands.TryGetValue(args[0].ToLower(), out command)) { if (command.Process(args.Skip(1).ToArray())) { Console.WriteLine("Done!"); } else { Console.Error.WriteLine("Failed!"); } } else { Console.WriteLine("Error command " + args[0] + "."); ShowUsage(commands); } }
public static ICommandLineCommandInstance LoadCommand(string[] args) { if (args.Length < 1) { throw new CommandLineError("DAE-00260 Missing command parameter"); } ICommandLineCommand cmd = FindCommand(args[0]); if (cmd != null) { ICommandLineCommandInstance res = cmd.CreateInstance(); Dictionary <string, string> extparams = null; if (cmd.AllowExtParams) { extparams = new Dictionary <string, string>(); } LoadParameters(PyList.SliceFrom(args, 1), new object[] { res }, extparams); res.ExtParams = extparams; return(res); } throw new CommandLineError("DAE-00261 Command not defined:" + args[0]); }
public CommandLineCommand(string commandKeyword, ICommandLineCommand parent) { CommandKeyword = commandKeyword; Parent = parent; }
private bool TryParseUnknownCommandArg(string arg, ICommandLineParser parser, out ICommandLineCommand command, IEnumerable <IExtensionCommandLine> extensions) { command = null; if (parser.IsSwitch(arg)) { var parameter = arg.Substring(1); switch (parameter.ToLowerInvariant()) { case "?": case "h": case "help": command = new HelpCommand(); break; case "version": case "-version": command = new VersionCommand(); break; } } else { if (Enum.TryParse(arg, true, out CommandTypes commandType)) { switch (commandType) { case CommandTypes.Build: command = new BuildCommand(this.ServiceProvider); break; case CommandTypes.Compile: command = new CompileCommand(this.ServiceProvider); break; case CommandTypes.Decompile: command = new DecompileCommand(this.ServiceProvider); break; } } else { foreach (var extension in extensions) { if (extension.TryParseCommand(parser, out command)) { break; } command = null; } } } return(command != null); }
/// <inheritdoc/> public virtual void PrintUsage(ICommandLineCommand command) => PrintCommandUsage(command);
public override bool TryParseCommand(ICommandLineParser parser, string argument, out ICommandLineCommand command) { command = null; if ("extension".Equals(argument, StringComparison.OrdinalIgnoreCase)) { command = new ExtensionCacheManagerCommand(this.ServiceProvider); } return(command != null); }
/// <summary> /// /// </summary> /// <param name="args"></param> /// <remarks>Command line is something like this: /// CommadName [args] </remarks> public static bool ProcessCommandLine(this string[] args, Func <ICommandLineCommand[]> getCommands, ICommandLineCommand defaultCommand) { return(ProcessCommandLine(args, getCommands, defaultCommand, 0)); }
private static void Main(string[] args) { var commands = new ICommandLineCommand[] { new UniformSummaryBuilderUI.Command(), new PeptideSpectrumMatchDistillerCommand(), new Mgf2Ms2ConverterCommand(), new MascotGenericFormatShiftPrecursorProcessorCommand(), new BuildSummaryResultParserCommand(), new DeuteriumCalculatorUI.Command(), new ChromatographProfileBuilderUI.Command(), new MultipleRaw2MgfCommand(), new ReversedDatabaseBuilderCommand() }.ToDictionary(m => m.Name.ToLower()); SoftwareInfo.SoftwareName = RcpaToolsAssembly.Name; SoftwareInfo.SoftwareVersion = RcpaToolsAssembly.Version; if (!SystemUtils.IsLinux && args.Length == 0) { Application.EnableVisualStyles(); Application.SetCompatibleTextRenderingDefault(false); var mainForm = new MainForm(); foreach (var command in commands.Values) { if (command is IToolCommand) { mainForm.AddCommand(command as IToolCommand); } } Application.Run(mainForm); } else { if (!SystemUtils.IsLinux) { AttachConsole(AttachParentProcess); } Console.WriteLine("Current system = " + SystemUtils.CurrentSystem.ToString()); ICommandLineCommand command; if (args.Length == 0) { ShowUsage(commands); } else if (commands.TryGetValue(args[0].ToLower(), out command)) { if (command.Process(args.Skip(1).ToArray())) { Console.WriteLine("Done!"); } } else { if (!args[0].Equals("-h") && !args[0].Equals("--help")) { Console.WriteLine("Error command " + args[0] + "."); } ShowUsage(commands); } } }
/// <summary> /// See <see cref="IExtensionCommandLine.TryParseCommand" /> /// </summary> public virtual bool TryParseCommand(ICommandLineParser parser, string argument, out ICommandLineCommand command) { command = null; return(false); }
private static string CreateMessage(ICommandLineCommand command, Exception exception) => $"Command '{command.Name}' failed to execute because: {exception.Message}";
void DisplayHelp(ICommandLineCommand command) { Console.WriteLine($"'{command.Name}' : {command.ShortDescription}"); //Console.WriteLine(" " + command.LongDescription); Console.WriteLine(); }
public override bool TryParseCommand(ICommandLineParser parser, string argument, out ICommandLineCommand command) { command = null; if ("convert".Equals(argument, StringComparison.OrdinalIgnoreCase)) { command = new ConvertCommand(this.ServiceProvider); } else if ("format".Equals(argument, StringComparison.OrdinalIgnoreCase)) { command = new FormatCommand(this.ServiceProvider); } return(command != null); }
public CommandNotFoundParserResult(ICommandLineCommand cmd) => Command = cmd;
/// <summary> /// The add. /// </summary> /// <param name="command"> /// The command. /// </param> public void Add(ICommandLineCommand command) { Contract.Requires<ArgumentNullException>(command != null, "command"); }
/// <summary> /// Creates a new command parse exception /// </summary> /// <param name="command">the failed command</param> /// <param name="innerExceptions">collection of inner exception</param> public CommandParseException(ICommandLineCommand command, IReadOnlyCollection <Exception> innerExceptions) : base(command, "", new AggregateException(innerExceptions)) { }
public static bool ProcessCommandLine(this string[] args, Func <ICommandLineCommand[]> getCommands, ICommandLineCommand defaultCommand, int startingArgIndex) { Trace.TraceInformation("Processing command line..."); if (args == null || args.Length <= startingArgIndex) { Console.WriteLine("No command line arguments."); return(ProcessDefaultCommand(args, defaultCommand, startingArgIndex)); } ICommandLineCommand[] commands; commands = getCommands(); if (commands == null || commands.Length <= 0) { Console.WriteLine("Command list not available."); return(ProcessDefaultCommand(args, defaultCommand, startingArgIndex)); } string commandName = args[startingArgIndex].Trim().ToLower(); ICommandLineCommand cmd; cmd = commands.FirstOrDefault(x => x.Name.ToLower() == commandName); if (cmd == null) { return(ProcessDefaultCommand(args, defaultCommand, startingArgIndex)); } cmd.Execute(args, startingArgIndex + 1); return(true); }
/// <summary> /// Specifies a command to attached the option too. /// </summary> /// <param name="command">The command to attach the option too. This must not be <c>null</c> and already be setup with the parser.</param> /// <returns>A <see cref="ICommandLineOptionFluent{T}"/>.</returns> /// <exception cref="ArgumentNullException">Thrown if <paramref name="command"/> is <c>null</c>.</exception> /// <exception cref="CommandNotFoundException">Thrown if the specified <paramref name="command"/> does not exist in the parser.</exception> public ICommandLineOptionFluent <T> AssignToCommand(ICommandLineCommand command) { this.Command = command; return(this); }
/// <summary> /// The add. /// </summary> /// <param name="command"> /// The command. /// </param> public void Add(ICommandLineCommand command) { _commands.Add(command); }
/// <inheritdoc/> public virtual void PrintCommandUsage(ICommandLineCommand command) { Builder.AddCommand(command.Name, command); console.WriteLine(Builder.Build()); }
public bool TryParseCommand(ICommandLineParser parser, out ICommandLineCommand command) { command = null; return(false); }
public void PrintUsage(ICommandLineCommand command) { m_usageBuilder.PrintCommand(command.Name, (ICommandLineCommandContainer)command); m_usageBuilder.Print(); }
private static string CreateSingleExceptionMessage(ICommandLineCommand command, Exception exception) => $"Unable to parse command '{command.Name}' reason: {exception.Message}";
/// <summary> /// Command failed to execute exception /// </summary> /// <param name="command">Command that failed</param> /// <param name="innerException">Actual exception</param> public CommandExecutionFailedException(ICommandLineCommand command, Exception innerException) : base(CreateMessage(command, innerException), innerException) { }