Esempio n. 1
0
        // Function that implements functionalty for console utility and returns exit code.
        // Function should not throw exceptions.
        internal static int Execute(ConsoleUtilityContext context)
        {
            // Get 'user' switch from parsed arguments.
            var userNameArgument = context.Arguments.GetArgument(Switches.UserName);

            if (!userNameArgument.isSpecified)
            {
                // If switch is not specified, print error message to default output.
                context.Output.Stream.WriteLine("Error: User name not specified.");

                if (!context.Silent)
                {
                    // If not in silent mode, we can additionally print help message.
                    context.Output.Stream.WriteLine();
                    context.Output.Stream.Write(context.Switches.ToString());
                }

                // Return error code.
                return(ExitCodes.InvalidArgumentSet);
            }

            // Get user name from switch.
            string userName = userNameArgument.parameters[0];

            // Get 'greeting' switch from parsed arguments.
            var greetingArgument = context.Arguments.GetArgument(Switches.Greeting);

            // If switch is present, use greeting message from it.
            string greeting = greetingArgument.isSpecified
                ? greetingArgument.parameters[0] : "Hello";

            // Parameter chesk is done. Now to primary functionality.
            // Print greeting message to default output.
            context.Output.Stream.WriteLine($"{greeting}, {userName}!");

            // Return success exit code.
            return(DefaultExitCodes.Success);
        }
        /// <summary>
        /// Initializes a new instance of the <see cref="SimpleConsoleUtility"/> class.
        /// </summary>
        public SimpleConsoleUtility()
        {
            this.context = new ConsoleUtilityContext();

            this.ExitCode = DefaultExitCodes.UnexpectedExit;
        }