In my static void Main method I have a string variable 'Name'. I want to use Name in a sub method.
//Main method holds Name
static void Main(string[] args)
{
WriteLine("Please Enter Name);
string Name = ReadLine();
}
static void subMethod()
{
// subMethod wants to use Name
foreach (char i in Name)
{
Write("~");
}
}
I thought that since both of these functions were in the same class that the sub function would automatically know what Name is since it's used in the Main method.
I've looked online and have tried different ways to reference the variable in
The closest I've gotten to be able to do this (or at least what has given me the least amount of error messages) is write this in my sub method:
//Main method holds Name
static void Main(string[] args)
{
WriteLine("Please Enter Name);
string Name = ReadLine();
}
static void subMethod()
{
//This is my attempt at referencing "Name" from Main
string Name = Main(Name);
// subMethod wants to use Name
foreach (char i in Name)
{
Write("~");
}
}
When I have this in my sub method I get these errors:
CS1503 Argument 1: cannot convert from 'string' to 'string[]'
CS0165 Use of unassigned local variable 'Name'
When I look up the error codes, it takes me to Microsoft, but everything they say about it is just a bunch of jargon that I can't understand (I'm very new to c#). It'd help a lot if anyone explained their solution as if you're talking to a 5 year old.
I can see a few mistakes in your code:
WriteLine("Please Enter Name);
You cannot just call WriteLine, you wanna tell the Console to WriteLine (or ReadLine). You are also missing quotation marks, it should be:
Console.WriteLine("Please Enter Name");
After fixing this, you can pass an argument to method like this:
static void Main(string[] args)
{
Console.WriteLine("Please Enter Name");
string Name = Console.ReadLine();
subMethod(Name);
}
static void subMethod(string name)
{
string Name = name;
//subMethod wants to use Name
foreach (char i in Name)
{
Console.Write("~");
}
}
For learning C# I would recommend watching tutorials on youtube, or reading them on some websites (f.e. here), microsoft documentation is good, but it might be a little bit too complicated for beginners.
Scenario: I want to parse a console app's command line that has a number of options (that don't relate to a command), and a command that has a number of options. I've simplified what I'm trying to into a fictitious example. E.g. myapp --i infile.dat --o outfile.dat conversion --threshold 42
Problem: I'm finding that if I put the "conversion" command and it's option on the command-line then only its handler gets called, but not the handler for the root command. So I have no way to determine the values of the root command's --i and --o options.
(Conversely, if I omit the "conversion" command then root command's handler only is called - which is what i would expect.)
Example code:
public class Program
{
public static async Task<int> Main(string[] args)
{
// Conversion command
var thresholdOpt = new Option<int>("--threshold");
var conversionCommand = new Command("conversion") { thresholdOpt };
conversionCommand.SetHandler(
(int threshold) => { Console.WriteLine($"threshold={threshold}"); },
thresholdOpt);
// Root command
var infileOpt = new Option<string>("--i");
var outfileOpt = new Option<string>("--o");
var rootCommand = new RootCommand("test") { infileOpt, outfileOpt, conversionCommand };
rootCommand.SetHandler(
(string i, string o) => { Console.WriteLine($"i={i}, o={o}"); },
infileOpt, outfileOpt);
return await rootCommand.InvokeAsync(args);
}
}
Unexpected outputs:
> myapp --i infile.dat --o outfile.dat conversion --threshold 42
threshold=42
In the above I expect to see the value for the --i and --o options, as well as the threshold options associated with the conversion command, but the root command's handler isn't invoked.
Expected outputs:
> myapp --i infile.dat --o outfile.dat
i=infile.dat, o=outfile.dat
> myapp conversion --threshold 42
threshold=42
The above are what I'd expect to see.
Dependencies: I'm using System.CommandLine 2.0.0-beta3.22114.1, System.CommandLine.NamingConventionBinder v2.0.0-beta3.22114.1, .net 6.0, and Visual Studio 17.1.3.
I'd be grateful for help in understanding what I'm doing wrong. Thanks.
Based on the docs sample it seems only one verb gets executed. For example next:
var rootCommand = new RootCommand();
rootCommand.SetHandler(() => Console.WriteLine("root"));
var verbCommand = new Command("verb");
verbCommand.SetHandler(() => Console.WriteLine("verb"));
rootCommand.Add(verbCommand);
var childVerbCommand = new Command("childverb");
childVerbCommand.SetHandler(() => Console.WriteLine("childverb"));
verbCommand.Add(childVerbCommand);
return await rootCommand.InvokeAsync(args);
For no arguments will print root, for verb will print verb and for verb childverb will print childverb.
So if you need multiple actions performed it seems you will need to use another approach (for example manually processing rootCommand.Parse() result).
If you just want "--i" and "--o" accessible for conversion then add them to corresponding command:
// actually works without specifying infileOpt, outfileOpt on conversionCommand
// but should be still present on the root one
// also rootCommand.AddGlobalOption can be a more valid approach
var conversionCommand = new Command("conversion") { thresholdOpt, infileOpt, outfileOpt};
// add here for handler
conversionCommand.SetHandler(
(int threshold, string i, string o) => { Console.WriteLine($"threshold={threshold}i={i}, o={o}"); },
thresholdOpt, infileOpt, outfileOpt);
I have a WPF application. For the purpose of this question, let's say it's a simple Window with a button. When I click on that button, I would like a Python script to be executed. Therefore, I went looking around and found out that I can run Python scripts using IronPython. Part1 works well, it runs the python scripts. From what I've gathered from looking around the web, Part2 is what I should do if I want to call a specific method.
private void btnWhatever_Click(object sender, RoutedEventArgs e)
{
//Basic engine to run python script. - Part1
ScriptEngine engine = Python.CreateEngine();
string pythonScriptPath = System.IO.Path.GetDirectoryName(System.IO.Path.GetDirectoryName(System.IO.Directory.GetCurrentDirectory()));
ScriptSource source = engine.CreateScriptSourceFromFile(pythonScriptPath + "/python.py");
ScriptScope scope = engine.CreateScope();
source.Execute(scope);
//Part2
Object myclass = engine.Operations.Invoke(scope.GetVariable("pythonScriptClass"));
object[] parameters = new object[] { "Hi",3 };
engine.Operations.InvokeMember(myclass, "theMethod", parameters);
}
The problem is, I kept getting 'Microsoft.Scripting.ArgumentTypeException' happened in Microsoft.Dynamic.dll : theMethod() takes exactly 2 arguments (3 given).
I understand from that error that I'm giving 3 arguments instead of 2 but I can't call a specific method another way from what I found out. I'm pretty new to IronPython and Python in general but here is a script example :
class pythonScriptClass:
def swapText(text, number):
return text[number:] + text[:number]
def getLetterIndex(letter, text):
for k in range(len(text)):
if (letter== text[k]):
return k
return -1
def theMethod(text , number):
result= swapText("textToBeSwaped", number)
toBeReturned = ""
for letter in text:
if letter in "abcdefghijklmnopqrstuvwxyz":
toBeReturned = toBeReturned + result[getLetterIndex(letter, result)]
return toBeReturned
My ultimate goal for the moment is to get this to work and therefore be able to call theMethod() from the Python script and get the returned value using C# - IronPython.
I have tried other methods such as : scope.SetVariable("key","value"); but I got the same error.
As for python member method, the first argument is self.
class pythonScriptClass:
def theMethod(self, text, number):
# and call self.swapText(...)
This is why the number of arguments went wrong.
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 years ago.
Improve this question
I have implemented a method that requires 2 parameters (arguments) : the first one is the source of the xml file ( type string) and the second one is the destination path of the generated pdf file (type string)
This application will be used by another application that will assign automatically the 2 parameters.
My question is how should I declare the 2 arguments until I can assign external parameters?
in other meaning : I implemented a console appication . When calling it from cmd , it looks like that : C:> name_of_apllication "path1" "path2". How should I implement the parameters if the called method until they will replaced by "path1" and "path2"?
this is the code of the main class : the method that will be used is : GeneratePDF_CTAF
static void Main(string[] args)
{
string input = "";
string output = "";
GeneratePDF.GeneratePDF_Ctaf( ref input, ref output );
}
this is the error screen , it is in french and that means can not find file
The command line arguments are passed to the Main method as an array of strings. This is the args parameter in your code, so you can simply extract the parameters you need from there:
static int Main(string[] args)
{
if (args.Length != 2)
{
Console.Error.WriteLine("This program requires exactly 2 parameters");
return 1; // error code
}
string input = args[0];
string output = args[1];
GeneratePDF.GeneratePDF_Ctaf(input, output);
return 0; // no error
}
Note here, I've modified Main to return an int. A non-zero return value is often used in console applications to provide error information to the calling program. I've also removed the ref keyword from your parameters because it's almost never necessary to use ref parameters in .NET.
I think there is something fundamental about using a function that you are not understanding so I will give a short example -- if it does not solve your problem please explain why not:
void Main(string[] args)
{
aFunction(args[1], args[2]);
}
void aFunction(string arg1, string arg2)
{
Console.WriteLine(arg1);
Console.WriteLine(arg2);
}
I have a specific question at the end but I want to provide plenty of background and context so that readers can understand my objective.
Background
I am building a console-style application with ASP.NET MVC 3. The concept itself is simple: receive command strings from the client, check if the command supplied exists and if the arguments provided with the command are valid, execute the command, return a set of results.
Inner-workings
With this application I decided to get a little creative. The most obvious solution to a terminal-style application is to build the world's largest IF statement. Run every command through the IF statement and call the appropriate functions from within. I did not like this idea. In an older version of the application this was how it operated and it was a huge mess. Adding functionality to the application was ridiculously difficult.
After much thought I decided to build a custom object called a command module. The idea is to build this command module with each request. The module object would contain all available commands as methods and the site would then use reflection to check if a command supplied by the user matches a method name. The command module object sits behind an interface called ICommandModule shown below.
namespace U413.Business.Interfaces
{
/// <summary>
/// All command modules must ultimately inherit from ICommandModule.
/// </summary>
public interface ICommandModule
{
/// <summary>
/// The method that will locate and execute a given command and pass in all relevant arguments.
/// </summary>
/// <param name="command">The command to locate and execute.</param>
/// <param name="args">A list of relevant arguments.</param>
/// <param name="commandContext">The current command context.</param>
/// <param name="controller">The current controller.</param>
/// <returns>A result object to be passed back tot he client.</returns>
object InvokeCommand(string command, List<string> args, CommandContext commandContext, Controller controller);
}
}
The InvokeCommand() method is the only method on the command module that my MVC controller is immediately aware of. It is then this method's responsibility to use reflection and look at the instance of itself and locate all available command methods.
I use Ninject for dependency injection. My MVC controller has a constructor dependency on ICommandModule. I built a custom Ninject provder that builds this command module when resolving the ICommandModule dependency. There are 4 types of command modules Ninject can build:
VisitorCommandModule
UserCommandModule
ModeratorCommandModule
AdministratorCommandModule
There is one more class BaseCommandModule which all other module classes inherit from. Real quickly, here are the inheritance relationships:
BaseCommandModule : ICommandModule
VisitorCommandModule : BaseCommandModule
UserCommandModule : BaseCommandModule
ModeratorCommandModule : UserCommandModule
AdministratorCommandModule : ModeratorCommandModule
Hopefully you can see how this is constructed by now. Based on the user's membership status (not logged in, regular user, moderator, etc) Ninject will provide the proper command module with only the command methods the user should have access to.
All of this works great. My dilemma comes in when I am parsing the command string and figuring out how to structure the command methods on the command module object.
The Question
How should the command string be parsed and executed?
Current Solution
Currently I break up the command string (the string passed in by the user containing the command and all arguments) in the MVC controller. I then call the InvokeCommand() method on my injected ICommandModule and I pass in a string command and a List<string> args.
Let's say I have the following command:
TOPIC <id> [page #] [reply “reply”]
This line defines the TOPIC command accepting a required ID number, an optional page number, and an optional reply command with a reply value.
I currently implement the command method like this (The attributes above the method are for help menu information. The HELP command uses reflection to read all these and display an organized help menu):
/// <summary>
/// Shows a topic and all replies to that topic.
/// </summary>
/// <param name="args">A string list of user-supplied arguments.</param>
[CommandInfo("Displays a topic and its replies.")]
[CommandArgInfo(Name="ID", Description="Specify topic ID to display the topic and all associated replies.", RequiredArgument=true)]
[CommandArgInfo(Name="REPLY \"reply\"", Description="Subcommands can be used to navigate pages, reply to the topic, edit topic or a reply, or delete topic or a reply.", RequiredArgument=false)]
public void TOPIC(List<string> args)
{
if ((args.Count == 1) && (args[0].IsInt64()))
TOPIC_Execute(args); // View the topic.
else if ((args.Count == 2) && (args[0].IsInt64()))
if (args[1].ToLower() == "reply")
TOPIC_ReplyPrompt(args); // Prompt user to input reply content.
else
_result.DisplayArray.Add("Subcommand Not Found");
else if ((args.Count >= 3) && (args[0].IsInt64()))
if (args[1].ToLower() == "reply")
TOPIC_ReplyExecute(args); // Post user's reply to the topic.
else
_result.DisplayArray.Add("Subcommand Not Found");
else
_result.DisplayArray.Add("Subcommand Not Found");
}
My current implementation is a huge mess. I wanted to avoid giant IF statements, but all I did was trade one giant IF statement for all the commands, for a ton of slightly less giant IF statements for every command and its arguments. This isn't even the half of it; I simplified this command for this question. In actual implementation there are quite a few more arguments that can be provided with this command and that IF statement is the ugliest thing I have ever seen. It's very redundant and not at all DRY (don't repeat yourself) as I have to display "Subcommand Not Found" in three different places.
Suffice it to say, I need a better solution than this.
The Ideal Implementation
Ideally I would love to structure my command methods something like his:
public void TOPIC(int Id, int? page)
{
// Display topic to user, at specific page number if supplied.
}
public void TOPIC(int Id, string reply)
{
if (reply == null)
{
// prompt user for reply text.
}
else
{
// Add reply to topic.
}
}
Then I'd love to do this:
Receive command string from client.
Pass command string directly into InvokeCommand() on ICommandModule.
InvokeCommand() performs some magic parsing and reflection to choose the right command method with the right arguments and invokes that method, passing in only the necessary arguments.
The Dilemma with the Ideal Implementation
I'm not sure how to structure this logic. I've been scratching my head for days. I wish I had a second pair of eyes to help me out on this (hence finally resorting to a novel of an SO question). In what order should things happen?
Should I pull out the command, find all methods with that command name, then loop through all the possible arguments, then loop through my command string's arguments? How do I determine what goes where and what arguments go in pairs. For instance, if I loop through my command string and find Reply "reply" how do I pair the reply content with the reply variable, while encountering <ID> number and supplying it for the Id argument?
I'm sure I'm confusing the hell out of you now. Let me illustrate with some examples of command strings the user might pass in:
TOPIC 36 reply // Should prompt the user to enter reply text.
TOPIC 36 reply "Hey what's up?" // Should post a reply to the topic.
TOPIC 36 // Should display page 1 of the topic.
TOPIC 36 page 4 // Should display page 4 of the topic.
How do I know to send 36 to the Id parameter? How do I know to pair reply with "Hey what's up?" and pass "Hey what's up?" as the value for the reply argument on the method?
In order to know which method overload to call I need to know how many arguments where supplied so that I can match that number to the overload of the command method that takes that same number of arguments. The problem is, `TOPIC 36 reply "Hey what's up?" is actually two arguments, not three as reply and "Hey..." go together as one argument.
I don't mind bloating the InvokeCommand() method a little (or a lot) as long as it means that all the complex parsing and reflection nonsense is handled there and my command methods can remain nice and clean and easy to write.
I guess I'm really just looking for some insight here. Does anyone have any creative ideas to solve this problem? It really is a big issue because the argument IF statements are currently making it very complicated to write new commands for the application. The commands are the one part of the application that I want to be super simple so that they can be easily extended and updated. Here is what the actual TOPIC command method looks like in my app:
/// <summary>
/// Shows a topic and all replies to that topic.
/// </summary>
/// <param name="args">A string list of user-supplied arguments.</param>
[CommandInfo("Displays a topic and its replies.")]
[CommandArgInfo("ID", "Specify topic ID to display the topic and all associated replies.", true, 0)]
[CommandArgInfo("Page#/REPLY/EDIT/DELETE [Reply ID]", "Subcommands can be used to navigate pages, reply to the topic, edit topic or a reply, or delete topic or a reply.", false, 1)]
public void TOPIC(List<string> args)
{
if ((args.Count == 1) && (args[0].IsLong()))
TOPIC_Execute(args);
else if ((args.Count == 2) && (args[0].IsLong()))
if (args[1].ToLower() == "reply" || args[1].ToLower() == "modreply")
TOPIC_ReplyPrompt(args);
else if (args[1].ToLower() == "edit")
TOPIC_EditPrompt(args);
else if (args[1].ToLower() == "delete")
TOPIC_DeletePrompt(args);
else
TOPIC_Execute(args);
else if ((args.Count == 3) && (args[0].IsLong()))
if ((args[1].ToLower() == "edit") && (args[2].IsLong()))
TOPIC_EditReplyPrompt(args);
else if ((args[1].ToLower() == "delete") && (args[2].IsLong()))
TOPIC_DeleteReply(args);
else if (args[1].ToLower() == "edit")
TOPIC_EditExecute(args);
else if (args[1].ToLower() == "reply" || args[1].ToLower() == "modreply")
TOPIC_ReplyExecute(args);
else if (args[1].ToLower() == "delete")
TOPIC_DeleteExecute(args);
else
_result.DisplayArray.Add(DisplayObject.InvalidArguments);
else if ((args.Count >= 3) && (args[0].IsLong()))
if (args[1].ToLower() == "reply" || args[1].ToLower() == "modreply")
TOPIC_ReplyExecute(args);
else if ((args[1].ToLower() == "edit") && (args[2].IsLong()))
TOPIC_EditReplyExecute(args);
else if (args[1].ToLower() == "edit")
TOPIC_EditExecute(args);
else
_result.DisplayArray.Add(DisplayObject.InvalidArguments);
else
_result.DisplayArray.Add(DisplayObject.InvalidArguments);
}
Isn't that ridiculous? Every command has a monster like this and it's unacceptable. I am just going over scenarios in my head and how code might handle it. I was pretty proud of my command module setup, now if I could just be proud of the command method implementation.
While I'm not looking to jump ship with my entire model (command modules) for the application, I am definitely open to suggestions. I'm mostly interested in suggestions related to parsing the command line string and mapping its arguments to the right method overloads. I'm sure whatever solution I go with will require a fair amount of redesign so don't be afraid to suggest anything you think is valuable; even if I don't necessarily use your suggestion, it may put me on the right track.
Further Clarifications
I just wanted to clarify real quick that the mapping of commands to command methods is not really something I'm worried about. I'm mostly concerned about how to parse and organize the command line string. Currently the InvokeCommand() method employs some very simple C# reflection to find the appropriate methods:
/// <summary>
/// Invokes the specified command method and passes it a list of user-supplied arguments.
/// </summary>
/// <param name="command">The name of the command to be executed.</param>
/// <param name="args">A string list of user-supplied arguments.</param>
/// <param name="commandContext">The current command context.</param>
/// <param name="controller">The current controller.</param>
/// <returns>The modified result object to be sent to the client.</returns>
public object InvokeCommand(string command, List<string> args, CommandContext commandContext, Controller controller)
{
_result.CurrentContext = commandContext;
_controller = controller;
MethodInfo commandModuleMethods = this.GetType().GetMethod(command.ToUpper());
if (commandModuleMethods != null)
{
commandModuleMethods.Invoke(this, new object[] { args });
return _result;
}
else
return null;
}
So as you can see, I'm not worried about how to find the command methods as that is already working. I'm just pondering a good way to parse the command string, organize arguments, and then using that information to pick the right command method/overload using reflection.
Final Design Goal
I am looking for a really good way to parse the command string I'm passing in. I want the parser to identify several things:
Options. Identify options in the command string.
Name/Value Pairs. Identify name/value pairs (e.g. [page #] <- includes keyword "page" and value "#")
Value Only. Identify value only.
I want these to be identified via metadata on the first command method overload. Here is a list of sample methods I want to write, decorated with some metadata to be used by the parser when it is doing reflection. I will give you these method samples and some sample command strings that should map to that method. That information should aid me in formulating a good parser solution.
// Metadata to be used by the HELP command when displaying HELP menu, and by the
// command string parser when deciding what types of arguments to look for in the
// string. I want to place these above the first overload of a command method.
// I don't want to do an attribute on each argument as some arguments get passed
// into multiple overloads, so instead the attribute just has a name property
// that is set to the name of the argument. Same name the user should type as well
// when supplying a name/value pair argument (e.g. Page 3).
[CommandInfo("Test command tests things.")]
[ArgInfo(
Name="ID",
Description="The ID of the topic.",
ArgType=ArgType.ValueOnly,
Optional=false
)]
[ArgInfo(
Name="PAGE",
Description="The page number of the topic.",
ArgType=ArgType.NameValuePair,
Optional=true
)]
[ArgInfo(
Name="REPLY",
Description="Context shortcut to execute a reply.",
ArgType=ArgType.NameValuePair,
Optional=true
)]
[ArgInfo(
Name="OPTIONS",
Description="One or more options.",
ArgType=ArgType.MultiOption,
Optional=true
PossibleValues=
{
{ "-S", "Sort by page" },
{ "-R", "Refresh page" },
{ "-F", "Follow topic." }
}
)]
[ArgInfo(
Name="SUBCOMMAND",
Description="One of several possible subcommands.",
ArgType=ArgType.SingleOption,
Optional=true
PossibleValues=
{
{ "NEXT", "Advance current page by one." },
{ "PREV", "Go back a page." },
{ "FIRST", "Go to first page." },
{ "LAST", "Go to last page." }
}
)]
public void TOPIC(int id)
{
// Example Command String: "TOPIC 13"
}
public void TOPIC(int id, int page)
{
// Example Command String: "TOPIC 13 page 2"
}
public void TOPIC(int id, string reply)
{
// Example Command String: TOPIC 13 reply "reply"
// Just a shortcut argument to another command.
// Executes actual reply command.
REPLY(id, reply, { "-T" });
}
public void TOPIC(int id, List<string> options)
{
// options collection should contain a list of supplied options
Example Command String: "TOPIC 13 -S",
"TOPIC 13 -S -R",
"TOPIC 13 -R -S -F",
etc...
}
The parser must take in a command string, use reflection to find all possible command method overloads, use reflection to read the argument attributes to help determine how to divide up the string into a proper list of arguments, then invoke the proper command method overload, passing in the proper arguments.
Take a look at Mono.Options. It's currently part of Mono framework but can be downloaded and used as a single library.
You can obtain it here, or you can grab the current version used in Mono as a single file.
string data = null;
bool help = false;
int verbose = 0;
var p = new OptionSet () {
{ "file=", v => data = v },
{ "v|verbose", v => { ++verbose } },
{ "h|?|help", v => help = v != null },
};
List<string> extra = p.Parse (args);
The solution I generally use looks something like this. Please ignore my syntax errors... been a few months since I've used C#. Basically, replace the if/else/switch with a System.Collections.Generic.Dictionary<string, /* Blah Blah */> lookup and a virtual function call.
interface ICommand
{
string Name { get; }
void Invoke();
}
//Example commands
class Edit : ICommand
{
string Name { get { return "edit"; } }
void Invoke()
{
//Do whatever you need to do for the edit command
}
}
class Delete : ICommand
{
string Name { get { return "delete"; } }
void Invoke()
{
//Do whatever you need to do for the delete command
}
}
class CommandParser
{
private Dictionary<string, ICommand> commands = new ...;
public void AddCommand(ICommand cmd)
{
commands.Insert(cmd.Name, cmd);
}
public void Parse(string commandLine)
{
string[] args = SplitIntoArguments(commandLine); //Write that method yourself :)
foreach(string arg in args)
{
ICommand cmd = commands.Find(arg);
if (!cmd)
{
throw new SyntaxError(String.Format("{0} is not a valid command.", arg));
}
cmd.Invoke();
}
}
}
class CommandParserXyz : CommandParser
{
CommandParserXyz()
{
AddCommand(new Edit);
AddCommand(new Delete);
}
}
Be aware that you can put attributes on parameters which might make things more readable, e.g.
public void TOPIC (
[ArgInfo("Specify topic ID...")] int Id,
[ArgInfo("Specify topic page...")] int? page)
{
...
}
I can see two different problems here:
Resolving method name (as string) to command module
You could use Dictionary to map string to method just like in Billy's answer. If you prefer only method over command object, you can map string to method directly in C#.
static Dictionary<string, Action<List<string>>> commandMapper;
static void Main(string[] args)
{
InitMapper();
Invoke("TOPIC", new string[]{"1","2","3"}.ToList());
Invoke("Topic", new string[] { "1", "2", "3" }.ToList());
Invoke("Browse", new string[] { "1", "2", "3" }.ToList());
Invoke("BadCommand", new string[] { "1", "2", "3" }.ToList());
}
private static void Invoke(string command, List<string> args)
{
command = command.ToLower();
if (commandMapper.ContainsKey(command))
{
// Execute the method
commandMapper[command](args);
}
else
{
// Command not found
Console.WriteLine("{0} : Command not found!", command);
}
}
private static void InitMapper()
{
// Add more command to the mapper here as you have more
commandMapper = new Dictionary<string, Action<List<string>>>();
commandMapper.Add("topic", Topic);
commandMapper.Add("browse", Browse);
}
static void Topic(List<string> args)
{
// ..
Console.WriteLine("Executing Topic");
}
static void Browse(List<string> args)
{
// ..
Console.WriteLine("Executing Browse");
}
Command-line arguments parsing
People have been scratching their heads solving this problem in early days ..
But now we has library that specifically handle this problem. See http://tirania.org/blog/archive/2008/Oct-14.html or NDesk.Options. This should be easier and could handle some pitfall cases than rolling out new one.