-4

So this is probably the most CONFUSING thing I am going to ask. I can't find anything online.

So currently I am working on an application in Visual C#, for Windows.

And basically it's a Console application that takes commands and I want a proper way to create commands.

For example, my current usage is that I take user input and check if the command is there using the String.Contains(); method. Then I check if the parameters are there using String.Contains();

Example:

if the command was "order" and parameters could be "-food" and "-pizza" and "-xl"

then the command the user would type would be:

order -food -pizza -xl

the same way you would do so within linux as in

apt-get install python

but my question is that how do I make such a system WITHIN C# in windows, without using the String.Contains(); system.

Because, in my food example, this is what I wouldve done with the input I grab:

I would check if the command contains "order" then I would check if it contains "-food" then I would check if it contains "-pizza" then I would check if it contains "-xl"

This is a very bad thing I know.

So is there a proper way I can do this in c#?

And no I don't want exe's that are launched via Command Prompt, like the way you do

ping 192.168.1.1 

I want it so the "command prompt" or "terminal" is within my console application, in c# and i can enter commands.

I went in as specific as possible but I can't go in anymore.

I'm looking for what this guy is saying: C#: How to detect arguments typed into console application?

Community
  • 1
  • 1
bin
  • 510
  • 7
  • 18
  • 2
    you need to look at how to pass command line parameters Console Application there are actually plenty of examples online on how to do this and you would probably be working with a `switch() { } case` statement as well. you can also do ping statements in a console application as well once again..plenty of working examples on how to do this online as well.. – MethodMan Dec 13 '16 at 17:16
  • Lots of pre-built options [here](http://stackoverflow.com/questions/491595/best-way-to-parse-command-line-arguments-in-c?noredirect=1&lq=1). – Jasen Dec 13 '16 at 17:18
  • @MethodMan I don't want it so, for example if my application is help.exe i dont want to use windows command prompt to do help -g -h – bin Dec 13 '16 at 17:19
  • 3
    It sounds like you want a basic repl? You basically need to write a parser based on the command grammar and dispatch based on the result to the appropriate command handler. This question is really too broad as it is. – Lee Dec 13 '16 at 17:19
  • 1
    The easiest way is to use a library like NDesk or System.Commandline from Corefx Labs. You can find them on NuGet. Just pick the one that looks more natural – Panagiotis Kanavos Dec 13 '16 at 17:20

1 Answers1

1

First thing would be to split your input string into the actual parts. Then, after you've done this, the first item is your command itself. Switch this, and find out what to do. All other parts are arguments for this command, so they should be handled by the command itself, this is the way it's done. Consider this:

rm -R *

This command will recursively delete everything. How? Split it by the space character to receive the following array:

["rm", "-R", "*"]

Okay, so where to go from here? The first item, rm, is your command. Take a look at it and decide what to do. Everything else are arguments, so the command should handle them. Create a class RmHandler that extends CommandHandler (you have to create those classes yourself) and hand the other arguments over to that class. In there, do whatever you need to do with them.

NikxDa
  • 4,137
  • 1
  • 26
  • 48
  • This is what I'm looking for, Not entirely but does help my question, I already did make a "CommandHandler" class, as a seperate dll file and this will be another rm handle that extends command hendler. – bin Dec 13 '16 at 17:23
  • so the other person above in the above comments was talking about NDesk or System.Commandline? Would this be an easier way to do this or could I just grab a library and do it. – bin Dec 13 '16 at 17:24
  • Those are external libraries and I see no need for those here. I do not know them though, so they might solve your problem too. The easiest way is the approach I explained, imo. – NikxDa Dec 13 '16 at 17:35
  • Could you link me to a premade tutorial for my specific purpose? – bin Dec 13 '16 at 17:37
  • IT would be useful, @NikxDa Cause if I split it into an array, I would need to make a handler way more advanced – bin Dec 13 '16 at 17:38
  • I don't think your handler has to be very advanced. Items in the array starting with "-" are flags, while other items are normal arguments. Seperate those and then just create a handleCommand-function which decides - based on present flags and arguments - what to do. This is normal if-then-else. I actually don't think there is any tutorial just for this case. – NikxDa Dec 13 '16 at 17:43
  • I'm looking for something like this guys http://stackoverflow.com/questions/316463/c-how-to-detect-arguments-typed-into-console-application?rq=1 – bin Dec 13 '16 at 17:50
  • So where exactly is the problem? – NikxDa Dec 13 '16 at 17:53
  • let me just place the code – bin Dec 13 '16 at 18:14
  • http://lux.ejectedmatrix.com/dl/Versions/Personal1_0/lux_console.zip here is my app and you will see what I need if u look at what it does @NikxDa – bin Dec 13 '16 at 18:39
  • hm? @NiksDa what do you think? – bin Dec 13 '16 at 19:26
  • How about we discuss in chat? – NikxDa Dec 13 '16 at 20:50
  • It wont let me chat because I dont have the rep – bin Dec 13 '16 at 23:17