命令行应用程序的搭建

104 阅读1分钟

搭建一个根据参数执行不同方法的命令行应用程序

        static void Main(string[] args)
        {
            var app = new CommandLineApplication(throwOnUnexpectedArg: false);
            app.FullName = "LINQ Sample App";
            LinqSamples.Register(app);
            GroupingSamples.Register(app);

            app.Command("help", cmd =>
            {
                cmd.Description = "Get help for the application";
                CommandArgument commandArgument = cmd.Argument("<COMMAND>", "The command to get help for");
                cmd.OnExecute(() =>
                {
                    app.ShowHelp(commandArgument.Value);
                    return 0;
                });
            });

            app.OnExecute(() =>
            {
                app.ShowHelp();
                return 0;
            });

            app.Execute(args);
        }

internal class LinqSamples
{
        internal static void Register(CommandLineApplication app)
        {
            MethodInfo[] methods = Assembly.GetExecutingAssembly()
                .GetTypes()
                .Where(t => t.Name == nameof(LinqSamples))
                .Single()
                .GetMethods()
                .Where(m => m.IsPublic && m.IsStatic)
                .ToArray();

            foreach (var method in methods)
            {
                app.Command(method.Name.ToLower(), cmd =>
                {
                    cmd.Description = method.Name;
                    cmd.OnExecute(() => { method.Invoke(null, null); return 0; });
                });
            }
        }
}
        
class GroupingSamples
{
        internal static void Register(CommandLineApplication app)
        {
            app.Command("group", cmd =>
            {
                var methodOption = new CommandOption("-m", CommandOptionType.NoValue);
                var variableOption = new CommandOption("-v", CommandOptionType.NoValue);
                var anonymousOption = new CommandOption("-a", CommandOptionType.NoValue);
                var nestedOption = new CommandOption("-n", CommandOptionType.NoValue);
                var nestedMethodOption = new CommandOption("-nm", CommandOptionType.NoValue);
                cmd.Options.AddRange(new[] { methodOption, variableOption, anonymousOption, nestedOption, nestedMethodOption });
                cmd.Description = "group -[m|v|a|n|nm]";
                cmd.OnExecute(() =>
                {
                    if (methodOption.HasValue())
                    {
                        GroupingWithMethods();
                    }
                    else if (variableOption.HasValue())
                    {
                        GroupingWithVariables();
                    }
                    else if (anonymousOption.HasValue())
                    {
                        GroupingWithAnonymousTypes();
                    }
                    else if (nestedOption.HasValue())
                    {
                        GroupingAndNestedObjects();
                    }
                    else if (nestedMethodOption.HasValue())
                    {
                        GroupingAndNestedObjectsWithMethods();
                    }
                    else
                    {
                        Grouping();
                    }
                    return 0;
                });
            });
        }
}


image.png

搭建一个根据参数执行不同方法的命令行应用程序,另一种写法

        static void Main(string[] args)
        {
            if (args.Length != 1)
            {
                Usage();
                return;
            }

            switch (args[0].ToLower())
            {
                case "-donthandle":
                    DontHandle();
                    break;
                case "-handle":
                    HandleOneError();
                    break;
                case "-two":
                    StartTwoTasks();
                    break;
                case "-twop":
                    StartTwoTasksParallel();
                    break;
                case "-agg":
                    ShowAggregatedException();
                    break;
                default:
                    Usage();
                    break;
            }

            Console.ReadLine();
        }
        public static void Usage()
        {
            Console.WriteLine("Usage: ErrorHandling Command");
            Console.WriteLine();
            Console.WriteLine("Commands:");
            Console.WriteLine("\t-donthandle\t\tcall async methods with exceptions not caught");
            Console.WriteLine("\t-two\t\tstart two tasks");
            Console.WriteLine("\t-twop\t\tstart two tasks parallel");
            Console.WriteLine("\t-agg\t\taggregated exception");
            Console.WriteLine("\t");
        }