自定义命令行程序(窗体) [英] Custom Command Line program (Form)

查看:71
本文介绍了自定义命令行程序(窗体)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

你好在那里

我有一个关于制作自定义命令行程序(例如控制台)的问题-我目前正在寻找的唯一东西是使用参数解析命令的高级"但易于使用的方式.

我不想在List(Of String)Array()中注册命令以简化操作.同样,我也不想定义一个命令可以并且可以有多少个参数.

假设我已经注册了一个名为Custom的命令,并且该命令有两个参数是必需的和一个是可选的,所以每当我输入Custom Argument1 Argument2时,可以在我的代码中使用这两个参数,但是如果我不想使用Argument2,那么我将其用作Custom Argument1,它仍然可以运行,但仅与Argument1一起使用(并且完全没有参数,它应该引发错误)

我一直在寻找带有参数的命令行程序,但是还没有找到我想要的方式.我之所以要这样做,是因为我不想创建以以下形式运行的控制台- NOT 命令行程序(或命令提示符)

在我的脑海中,使用List(Of String)注册命令的示例称为Commands:

Hello out there

I have a question about making a custom command line program (like a console) - The only thing I''m currently looking for is an ''advanced'' but easy-to-use way of parsing commands with arguments.

I wan''t to register my commands in either a List(Of String) or an Array() to make it easy. Also I wan''t to define how many arguments a command may and can have.

Let''s say I have registered a command called Custom and it have two arguments on that is required and one that is optional so whenever I enter Custom Argument1 Argument2 I can use these two arguments within my code but if I don''t wanna use Argument2 so I use it as Custom Argument1 it will still run but only with Argument1 (and with no arguments at all it should throw an error)

I have been looking in to command line programs with arguments, but haven''t found the way I wan''t it. The reason why I''m looking for this, is that I wan''t to create a console which runs in a form - NOT command line program (or command prompt)

An example in my head of registering commands with List(Of String) called Commands:

Commands.Add("run <{0}> [{1}]")



其中run是我的命令,<{0}>是必需的参数,[{1}]是可选的参数.

将使用1必需参数和1可选参数注册Command.

因此,当我在代码中使用它时:



where run is my command and <{0}> is the required argument and [{1}] is the optional argument.

will register Command with 1 required argument and 1 optional argument.

So when I use it in my code:

Select Case Commands
     Case "run"
        _output.Text = "Command: " & Command & " - Arguments: " & Argument(0) & " " & Argument(1)
     End Select



请记住,只有一个主意,也许您可​​以在此基础上更轻松地进行一些事情.

注意:这不是解析参数通过命令提示符或命令行程序.它是具有2个文本框(INPUT和OUTPUT)的表单.

希望您能对此有所帮助,并会为您带来真正的帮助.

最好的问候,
SyncError



Remember, only an idea maybe you have somthing easier builded upon this.

NOTE: This is NOT to parse arguments trough command prompt or a command line program. It is a form with 2 textboxes (an INPUT and OUTPUT).

I hope you can help me with this, and will really appriciate it.

Best Regards,
SyncError

推荐答案

在我看来,您只需要实现一个字符串解析器,并且由于参数可以包含空格,因此您需要允许带引号的字符串.我认为本文将为您提供所需的解析器.然后,您要做的就是根据实现要求验证已解析的部分:

持久性字符串解析器 [
Well, it seems to me that you simply need to implement a string parser, and because parameters can include spaces, you need to allowed for quoted strings. I think this article will provide the parser you need. All you then have to do is validate the parsed parts as required by your implementation:

Persistent String Parser[^]


令人惊讶的是,.网.但是,有许多免费的第三方库.这是一个称为选项 [
Surprisingly, there are no built-in solutions to this seemingly common problem in .NET. However, a number of free third-party libraries are available. Here''s one called Options[^] - it presents a compact interface that is very easy to learn.


使用替代方法.我创建了UI代码,该代码只是为可用的命令解释器重新定向输入和输出.这样,我不必解析任何内容.我只是通过流重定向来替换UI.它为命令窗口和输出窗口提供了功能完善的文本编辑器界面,并带有文本搜索等功能.它还分别为输入和输出引入了Unicode(更普遍地说是可选编码).

此应用程序可帮助我使用控制台模式程序.当我向应用程序展示它们时,系统管理员喜欢它.不幸的是,我并不真的支持它.它是用Delphi Pascal编写的,我目前不使用.此外,还需要改进/修复.

这个想法很简单.使用.NET,可以使用带有重定向的StandardInputStandardOutputStandardError以及System.Diagnostics.ProcessStartInfo.RedirectStandardOutputRedirectStandardInputRedirectStandardError的类System.Diagnostics.Process来实现.
这在此处的代码示例中显示:
http://msdn.microsoft.com/en-us/library/system. diagnostics.process.standardoutput.aspx [ ^ ].

—SA
I''m using an alternative approach. I''ve created UI code which simply re-directs inputs and outputs for available command interpreter. This way, I don''t have to parse anything. I simply replace the UI through stream re-direction. It provides a fully-fledged text editor interface for command windows and output window, with text search, etc. It also introduces Unicode (more generally, selectable encoding) separately for input and output.

This application helps me to work with console-mode programs. System administrator enjoyed it when I presented the application the them. Unfortunately, I don''t really support it. It was written in Delphi Pascal which I currently don''t use. Besides, it needs improvements/fixes.

The idea is simple enough though. With .NET, it can be implemented using the class System.Diagnostics.Process with re-directed StandardInput, StandardOutput and StandardError and also System.Diagnostics.ProcessStartInfo.RedirectStandardOutput, RedirectStandardInput and RedirectStandardError.

This is shown on a code sample here:
http://msdn.microsoft.com/en-us/library/system.diagnostics.process.standardoutput.aspx[^].

—SA


这篇关于自定义命令行程序(窗体)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆