// ********************************************************************************
 /// <summary>
 /// Create a new Command Argument Configurator class.
 /// </summary>
 /// <param name="console">The output console</param>
 /// <param name="config">The parameter configuration</param>
 /// <returns>CmdArgConfiguration</returns>
 /// <created>GJK,23/10/2020</created>
 /// <changed>GJK,23/10/2020</changed>
 // ********************************************************************************
 public CmdArgConfigurator(CmdArgConfiguration config, IStdOut console)
 {
     if (config == null)
     {
         throw new ArgumentNullException(nameof(config), "Configuration is required.");
     }
     _config = config;
     Console = console;
 }
 // ********************************************************************************
 /// <summary>
 /// Create a new Command Argument Configurator class.
 /// </summary>
 /// <param name="config">The parameter configuration</param>
 /// <returns>CmdArgConfiguration</returns>
 /// <created>GJK,23/10/2020</created>
 /// <changed>GJK,23/10/2020</changed>
 // ********************************************************************************
 public CmdArgConfigurator(CmdArgConfiguration config) : this(config, null)
 {
 }
예제 #3
0
        // ********************************************************************************
        /// <summary>
        /// Parse and handle the arguments.
        /// </summary>
        /// <param name="configAction">The command line parameter configuration.</param>
        /// <param name="stdOut">Output channel (default null)</param>
        /// <param name="args">arguments</param>
        /// <returns>A list with unrecognized command line arguments.</returns>
        /// <created>GJK,23/10/2020</created>
        /// <changed>GJK,23/10/2020</changed>
        // ********************************************************************************
        public static List <string> ParseArgs(Action <CmdArgConfigurator> configAction, IStdOut stdOut = null, params string[] args)
        {
            if (configAction == null)
            {
                throw new ArgumentNullException(nameof(configAction), "Expected a method for configurating the command arguments.");
            }
            var config       = new CmdArgConfiguration();
            var configurator = new CmdArgConfigurator(config);

            try
            {
                configAction(configurator);
            }
            catch (Exception e)
            {
                throw new ArgumentException("Exception thrown in config action passed to CmdArgParser.Parse method. See inner exception for more details", e);
            }

            var extraArgs = new List <string>();

            if (args.Length > 0)
            {
                foreach (var argument in args)
                {
                    var argumentLC = argument.ToLower();
                    var argParsed  = false;
                    foreach (var parameter in config.Parameters)
                    {
                        foreach (var key in parameter.Keys)
                        {
                            if (argumentLC.StartsWith(key))
                            {
                                var rightSide = argument.Substring(key.Length);
                                if (string.IsNullOrEmpty(rightSide))
                                {
                                    parameter.Handler(string.Empty);
                                    argParsed = true;
                                }
                                else if (rightSide.StartsWith(":"))
                                {
                                    var value = rightSide.Substring(1);
                                    parameter.Handler(value);
                                    argParsed = true;
                                }
                            }
                        }
                    }
                    if (!argParsed)
                    {
                        extraArgs.Add(argument);
                    }
                }
            }
            if (extraArgs.Count > 0 && config.ShowHelpOnExtraArguments)
            {
                stdOut?.WriteLine("Unrecognized arguments: ");
                foreach (var extraArg in extraArgs)
                {
                    stdOut?.WriteLine("Key: {0}", extraArg);
                }
            }
            return(extraArgs);
        }