If you want you can make a code that parses the command line.
But there are many libraries that you can use for this.
There is a very good command line library: 'https://github.com/commandlineparser/commandline'
It is very easy to use and I hopes this helps.
You must create a 'options' class with all options provided.
To declare a option you use the 'option' attribute:
[Option(char shortoption, string longoption, Required = bool, MetaValue = string, Min = int, Seperator = char, SetName = string)]
public <type> <name> { get; set; }
Then you can parse the string array to your 'options' class and you can read the options from the class variables.
Example:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.IO;
using CommandLine;
namespace ConsoleApp1
{
//Declare some options
public class Options
{
//Format:
//[Option(char shortoption, string longoption, Required = bool, MetaValue = string, Min = int, Seperator = char, SetName = string)]
//public <type> <name> { get; set; }
[Option('r', "read", Required = true, HelpText = "Filename", Separator = ' ')]
public IEnumerable<string> InputFiles { get; set; }
[Option('e', "echo", Required = true, HelpText = "Echo message")]
public string echo { get; set; }
}
class Program
{
static void Main(string[] args)
{
bool s = true;
CommandLine.Parser.Default.ParseArguments<Options>(args).WithNotParsed<Options>((errs) =>
{
foreach (Error e in errs)
{
Console.WriteLine("ERROR " + e.Tag.ToString());
s = e.StopsProcessing ? false : s;
}
if(!s)
{
return;
}
}).WithParsed<Options>(opts => {
Console.WriteLine(opts.echo);
foreach(string filename in opts.InputFiles)
{
Console.WriteLine(File.ReadAllText(filename));
}
});
}
}
}
Console:
ConsoleApp1\bin\Debug>ConsoleApp1.exe -r helps.txt sayhello.txt -e "Thanks for reading"
Thanks for reading
I hope this helps
Hello world
ConsoleApp1\bin\Debug>ConsoleApp1.exe -r -fds fds fds-f dsf -
ConsoleApp1 1.0.0.0
Copyright © 2018
ERROR(S):
Option 'f' is unknown.
Required option 'e, echo' is missing.
-r, --read Required. Filename
-e, --echo Required. Echo message
--help Display this help screen.
--version Display version information.
ERROR UnknownOptionError
ERROR MissingRequiredOptionError
ConsoleApp1\bin\Debug>ConsoleApp1.exe -e f
ConsoleApp1 1.0.0.0
Copyright © 2018
ERROR(S):
Required option 'r, read' is missing.
-r, --read Required. Filename
-e, --echo Required. Echo message
--help Display this help screen.
--version Display version information.
ERROR MissingRequiredOptionError
ConsoleApp1\bin\Debug>ConsoleApp1.exe -r helps.txt sayhello.txt -e ":)"
:)
I hope this helps
Hello world
ConsoleApp1\bin\Debug>
How do I install it?
Go to View -> Other windows -> Package Manager Console
Enter the command:
Install-Package CommandLineParser -Version 2.2.1 -ProjectName <yourprojectname>
Github:
https://github.com/commandlineparser/commandline
I hope this helps.
Read this: Split string containing command-line parameters into string[] in C#. Windows already import that function. (split command args)
But you can also make your own (simple function that not split between "):
static string[] ParseArguments(string commandLine)
{
char[] parmChars = commandLine.ToCharArray();
bool inQuote = false;
for (int index = 0; index < parmChars.Length; index++)
{
if (parmChars[index] == '"')
inQuote = !inQuote;
if (!inQuote && parmChars[index] == ' ')
parmChars[index] = '\n';
}
return (new string(parmChars)).Split('\n');
}