static void Main(string[] args)
class MainClass { static int Main(string[] args) { // Test if input arguments were supplied: if (args.Length == 0) { System.Console.WriteLine("Please enter a numeric argument."); System.Console.WriteLine("Usage: Factorial <num>"); return 1; } try { // Convert the input arguments to numbers: int num = int.Parse(args[0]); System.Console.WriteLine("The Factorial of {0} is {1}.", num, Functions.Factorial(num)); return 0; } catch (System.FormatException) { System.Console.WriteLine("Please enter a numeric argument."); System.Console.WriteLine("Usage: Factorial <num>"); return 1; } } }
int num = int.Parse(args[]);
where X is the parameter number.
public enum OptimizeFor { Unspecified, Speed, Accuracy } public sealed class Options : CommandLineOptionsBase { #region Standard Option Attribute [Option("r", "read", Required = true, HelpText = "Input file with data to process.")] public string InputFile = String.Empty; [Option("w", "write", HelpText = "Output file with processed data (otherwise standard output).")] public string OutputFile = String.Empty; [Option("j", "jump", HelpText = "Data processing start offset.")] public double StartOffset = 0; [Option(null, "optimize", HelpText = "Optimize for Speed|Accuracy.")] public OptimizeFor Optimization = OptimizeFor.Unspecified; #endregion #region Specialized Option Attribute [ValueList(typeof(List<string>))] public IList<string> DefinitionFiles = null; [OptionList("o", "operators", Separator = ';', HelpText = "Operators included in processing (+;-;...)." + " Separate each operator with a semicolon." + " Do not include spaces between operators and separator.")] public IList<string> AllowedOperators = null; [HelpOption(HelpText = "Dispaly this help screen.")] public string GetUsage() { var help = new HelpText(Program._headingInfo); help.AdditionalNewLineAfterOption = true; help.Copyright = new CopyrightInfo("Giacomo Stelluti Scala", 2005, 2009); this.HandleParsingErrorsInHelp(help); help.AddPreOptionsLine("This is free software. You may redistribute copies of it under the terms of"); help.AddPreOptionsLine("the MIT License <http://www.opensource.org/licenses/mit-license.php>."); help.AddPreOptionsLine("Usage: SampleApp -rMyData.in -wMyData.out --calculate"); help.AddPreOptionsLine(string.Format(" SampleApp -rMyData.in -i -j{0} file0.def file1.def", 9.7)); help.AddPreOptionsLine(" SampleApp -rMath.xml -wReport.bin -o *;/;+;-"); help.AddOptions(this); return help; } private void HandleParsingErrorsInHelp(HelpText help) { string errors = help.RenderParsingErrorsText(this); if (!string.IsNullOrEmpty(errors)) { help.AddPreOptionsLine(string.Concat(Environment.NewLine, "ERROR: ", errors, Environment.NewLine)); } } }
private static void Main(string[] args) { var options = new Options(); ICommandLineParser parser = new CommandLineParser(); if (!parser.ParseArguments(args, options)) Environment.Exit(1); DoCoreTask(options); Environment.Exit(0); }
Source: https://habr.com/ru/post/141987/