예제 #1
0
파일: Program.cs 프로젝트: msc4266/CLArgs
        private static void Main(string[] args)
        {
            Console.WriteLine(">>> Start Main");
            Console.WriteLine($"Command-Line: {COMMAND_LINE}");
            args = Helper.SplitCommandLine(COMMAND_LINE);
            // ------------------------------------------------

            CommandLineArguments commandLineArguments = CommandLineParser.Parse(args);

            // Check if the 'fileName' option exist - was provided in the command-line
            // E.g. --fileName=.. /fileName:...
            const string FILENAME_TAG     = "fileName";
            bool         fileNameProvided = commandLineArguments.OptionTagProvided(FILENAME_TAG);

            Console.WriteLine($"*** Option '{FILENAME_TAG}' was provided in the command-line: {fileNameProvided}");

            // Set default value if not provided
            // Upsert = Update or Insert = Update or Add
            if (!fileNameProvided)
            {
                commandLineArguments.AddOption(new Option(FILENAME_TAG, "default.txt"));
            }

            foreach (Option option in commandLineArguments.Options)
            {
                Console.WriteLine($"Options[{option.Key}] = '{option.Value}'");
            }

            // ------------------------------------------------
            Console.WriteLine("<<< End Main");
        }
예제 #2
0
파일: Program.cs 프로젝트: msc4266/CLArgs
        private static void Main(string[] args)
        {
            Console.WriteLine(">>> Start Main");
            Console.WriteLine($"Command-Line: {COMMAND_LINE}");
            args = Helper.SplitCommandLine(COMMAND_LINE);
            // ------------------------------------------------

            //
            // The easy way: use the static Commander.ExecuteCommand()
            //
            // Commander auto resolution will resolve the one and only
            // class with [Command("DemoCommand")] annotation, which
            // becomes the ' DefaultCommand'.
            //
            // Then Commander then evaluates the command parameter type and
            // converts the command-line args into an
            // DefaultCommand.Parameters object.
            //
            // Finally it executes the Command.
            //
            Console.WriteLine("--- Minimum Code");
            Commander.ExecuteCommand(args, new Settings {
                IgnoreCase = true
            });

            //
            // Alternative way of doing it with manual command registration
            // and the chance to 'tweak' the provided Options
            // Option conversion remains the same: nothing to do for it.
            //
            Console.WriteLine("--- The alternative way - using an explicit Commander instance ");
            Commander commander = new Commander(new Settings {
                IgnoreCase = true, AutoResolveCommands = false
            });

            commander.RegisterCommand(new CommandDescriptor("TheOneAndOnlyCommand", () => new Command()));

            // Let's change the argument from commands-line
            CommandLineArguments commandLineArguments = CommandLineParser.Parse(args);

            commandLineArguments.AddOption(new Option("DatabaseTableName", "AnotherTable"));

            // Execute the Command with slightly different Options
            commander.ExecuteCommand(commandLineArguments);

            // ------------------------------------------------
            Console.WriteLine("<<< End Main()");
        }