Click here to Skip to main content
15,893,814 members
Please Sign up or sign in to vote.
4.00/5 (2 votes)
See more:
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}]")


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


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
Posted

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[^]
 
Share this answer
 
Comments
Sergey Alexandrovich Kryukov 28-May-11 14:25pm    
It depends what do you want to do with commands.

Please see my answer -- my approach does not require parsing. It's using available command interpreter and stream re-direction. Depends on requirements though.
--SA
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.
 
Share this answer
 
Comments
Sergey Alexandrovich Kryukov 28-May-11 14:26pm    
Please see my answer -- my approach does not require parsing. It's using available command interpreter and stream re-direction. Depends on requirements though.
--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
 
Share this answer
 
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900