/// <summary>
        /// Called when command line is parsing.
        /// </summary>
        /// <param name="name">The name.</param>
        /// <param name="value">The value.</param>
        /// <param name="type">The type.</param>
        /// <returns><c>true</c> if arguments was handled, <c>false</c> otherwise.</returns>
        protected bool OnCommandLineParsing(string name, string value, CommandLineArgumentType type)
        {
            CommandLineParseEventArgs args = new CommandLineParseEventArgs(name, value, type);

            this.TryInvoke(new Action(() => { this.model.OnCommandLineParsing(args); }));
            return(args.Handled);
        }
 public CommandLineParseEventArgs(string name, string value, CommandLineArgumentType type)
 {
     this.Name    = name;
     this.Value   = value;
     this.Type    = type;
     this.Handled = false;
 }
        public void CommandLineArgumentType_ToArgument_When_BlogUrl_Argument_ReturnsArgument_Test()
        {
            const string arg = "-blogurl";
            //given
            CommandLineArgumentType type = CommandLineArgumentType.BlogUrl;
            //when
            string value = type.ToArgument();

            //then
            value.Should().Be(arg);
        }
        public void CommandLineArgumentType_ToArgument_When_OutputDir_Argument_ReturnsArgument_Test()
        {
            const string arg = "-outputdir";
            //given
            CommandLineArgumentType type = CommandLineArgumentType.OutputDir;
            //when
            string value = type.ToArgument();

            //then
            value.Should().Be(arg);
        }
예제 #5
0
        public CommandLineArgument Parse(string[] args, CommandLineArgumentType type)
        {
            string arg   = type.ToArgument();
            int    index = Array.IndexOf(args, arg);

            if (index == -1)
            {
                return(null);
            }

            if (index + 1 > args.Length - 1)
            {
                return(null);
            }

            return(new CommandLineArgument(type, args[index + 1]));
        }
            public Argument(CommandLineArgumentAttribute attribute, FieldInfo field, ErrorReporter reporter)
            {
                this.longName          = CommandLineArgumentParser.LongName(attribute, field);
                this.description       = CommandLineArgumentParser.Description(attribute);
                this.explicitShortName = CommandLineArgumentParser.ExplicitShortName(attribute);
                this.shortName         = CommandLineArgumentParser.ShortName(attribute, field);
                this.elementType       = ElementType(field);
                this.flags             = Flags(attribute, field);
                this.field             = field;
                this.seenValue         = false;
                this.reporter          = reporter;
                this.isDefault         = (attribute != null) && (attribute is DefaultCommandLineArgumentAttribute);

                if (this.IsCollection)
                {
                    this.collectionValues = new ArrayList();
                }

                Debug.Assert(
                    !string.IsNullOrEmpty(this.longName),
                    "The long name must be provided for the argument.");

                Debug.Assert(
                    !this.IsCollection || this.AllowMultiple,
                    "Collection arguments must have allow multiple");

                Debug.Assert(
                    !this.Unique || this.IsCollection,
                    "Unique only applicable to collection arguments");

                Debug.Assert(
                    IsValidElementType(this.Type) || IsCollectionType(this.Type),
                    "The argument type is not valid.");

                Debug.Assert(
                    (this.IsCollection && IsValidElementType(this.elementType)) || (!this.IsCollection && this.elementType == null),
                    "The argument type is not valid.");
            }
예제 #7
0
        /// <summary>
        /// Fills in the switchArguments and nonSwitchArguments data structures
        /// </summary>
        /// <param name="switchArguments">switch arguments - i.e. ones that have a -, --, or / in front, and their corresponding values</param>
        /// <param name="nonSwitchArguments">all non switch arguments</param>
        internal void Parse(Dictionary <string, List <string> > switchArguments, List <string> nonSwitchArguments)
        {
            bool parsing = true;

            while (this.currentIndex < this.args.Length)
            {
                try
                {
                    string arg        = this.args[this.currentIndex];
                    string orginalArg = arg;
                    CommandLineArgumentType argumentType = this.GetCommandLineArgumentType(ref arg);

                    if (argumentType == CommandLineArgumentType.EndSwitchMarker)
                    {
                        parsing = false;
                        this.currentIndex++;
                        continue;
                    }

                    List <string> valueList = new List <string>();
                    string        value     = string.Empty;

                    if (parsing && argumentType == CommandLineArgumentType.Switch)
                    {
                        string[] keyValue = this.SplitIntoKeyAndValue(arg);
                        string   key      = keyValue[0];
                        if (key.StartsWith(this.context.Settings.NoPrefix))
                        {
                            key   = key.Replace(this.context.Settings.NoPrefix, string.Empty);
                            value = false.ToString(CultureInfo.CurrentCulture);
                        }

                        ArgumentDescriptor ar = this.context.GetDescriptor(key);
                        if (ar == null || ar.IsHidden)
                        {
                            ++this.currentIndex;
                            throw new CommandLineException(key, keyValue.Length > 1 ? keyValue[1] : string.Empty, "Unknown command line switch: " + key);
                        }

                        // if target is a bool then:
                        // 1. if argument begins 'no-' it is always false, and should have no value
                        // 2. if argument does not begin 'no-', and has a value separated by a defined separator, it takes that value
                        // 3. it is true
                        if (ar.MemberType == typeof(bool))
                        {
                            if (string.IsNullOrWhiteSpace(value))
                            {
                                // I am unclear about localization of string representations of bool: see http://stackoverflow.com/a/20620119
                                value = keyValue.Length > 1 ? keyValue[1] : true.ToString(CultureInfo.CurrentCulture);
                            }
                            else if (keyValue.Length > 1)
                            {
                                // this is a no-... switch, not allowed to have a value
                                throw new CommandLineException(orginalArg, keyValue[1], "Value not allowed on a 'no-...' bool switch");
                            }
                        }
                        else
                        {
                            if (keyValue.Length > 1)
                            {
                                value = keyValue[1];
                            }
                            else if (this.currentIndex + 1 < this.args.Length)
                            {
                                string v = this.args[this.currentIndex + 1];
                                if (!this.LooksLikeASwitch(v))
                                {
                                    value = v;
                                    ++this.currentIndex;
                                }
                            }
                        }

                        while (true)
                        {
                            valueList.Add(value);
                            if (!ar.IsList || (this.currentIndex + 1 >= this.args.Length) ||
                                this.LooksLikeASwitch(this.args[this.currentIndex + 1]))
                            {
                                break;
                            }

                            value = this.args[++this.currentIndex];
                        }

                        switchArguments[key] = valueList;
                    }
                    else
                    {
                        nonSwitchArguments.Add(arg);
                    }

                    ++this.currentIndex;
                }
                catch (Exception ex)
                {
                    this.context.Exceptions.Add(ex);
                }
            }
        }
 /// <summary>
 /// Initializes a new instance of the <see cref="DefaultCommandLineArgumentAttribute"/> class.
 /// </summary>
 /// <param name="type"> Specifies the error checking to be done on the argument. </param>
 public DefaultCommandLineArgumentAttribute(CommandLineArgumentType type)
     : base(type)
 {
 }
 public CommandLineArgument(CommandLineArgumentType type, string value)
 {
     Type  = type;
     Value = value;
 }
 /// <summary>
 /// Initializes a new instance of the <see cref="CommandLineArgumentAttribute"/> class.
 /// </summary>
 /// <param name="type"> Specifies the error checking to be done on the argument. </param>
 public CommandLineArgumentAttribute(CommandLineArgumentType type)
 {
     this.type        = type;
     this.Description = null;
 }
 /// <summary>
 /// Allows control of command line parsing.
 /// </summary>
 /// <param name="type"> Specifies the error checking to be done on the argument. </param>
 public CommandLineArgumentAttribute(CommandLineArgumentType type)
 {
     this.type = type;
 }
예제 #12
0
        public static string ToArgument(this CommandLineArgumentType type)
        {
            string arg = $"-{type.ToString().ToLower()}";

            return(arg);
        }
 /// <summary>
 /// Indicates that this argument is the default argument.
 /// </summary>
 /// <param name="type"> Specifies the error checking to be done on the argument. </param>
 public DefaultCommandLineArgumentAttribute(CommandLineArgumentType type)
     : base(type)
 {
 }
예제 #14
0
 /// <summary>
 /// Allows control of command line parsing.
 /// </summary>
 /// <param name="type"> Specifies the error checking to be done on the argument. </param>
 public CommandLineArgumentAttribute(CommandLineArgumentType type)
 {
     this.Type = type;
 }
            public Argument(CommandLineArgumentAttribute attribute, FieldInfo field, ErrorReporter reporter)
            {
                this.longName = CommandLineArgumentParser.LongName(attribute, field);
                this.explicitShortName = CommandLineArgumentParser.ExplicitShortName(attribute);
                this.shortName = CommandLineArgumentParser.ShortName(attribute, field);
                this.elementType = ElementType(field);
                this.flags = Flags(attribute, field);
                this.field = field;
                this.seenValue = false;
                this.reporter = reporter;
                this.isDefault = attribute != null && attribute is DefaultCommandLineArgumentAttribute;
                this.description=attribute.Description;

                if (IsCollection)
                {
                    this.collectionValues = new ArrayList();
                }

                Debug.Assert(this.longName != null && this.longName.Length > 0);
                if (IsCollection && !AllowMultiple)
                    ThrowError("Collection arguments must have allow multiple");
                Debug.Assert(!Unique || IsCollection, "Unique only applicable to collection arguments");
                Debug.Assert(IsValidElementType(Type) ||
                    IsCollectionType(Type));
                Debug.Assert((IsCollection && IsValidElementType(elementType)) ||
                    (!IsCollection && elementType == null));
            }
예제 #16
0
        private void ParseCommandLine()
        {
            // Get array of arguments based on the system parsing algorithm.
            string[] args = this.Command.GetCommandLineArgs();
            if (args != null)
            {
                for (int i = 0; i < args.Length; ++i)
                {
                    //parseCommand
                    if (args[i].StartsWith("-", StringComparison.InvariantCultureIgnoreCase) || args[i].StartsWith("/", StringComparison.InvariantCultureIgnoreCase))
                    {
                        string[] param       = args[i].Split(new char[] { '=' }, 2);
                        string   commandName = param[0].Remove(0, 1);
                        string   value       = param.Length > 1 ? param[1].Trim() : string.Empty;

                        this.OnCommandLineParsing(commandName, value, CommandLineArgumentType.Command);
                    }
                    else
                    {
                        string[] param     = args[i].Split(new char[] { '=' }, 2);
                        string   parameter = param[0];
                        string   value     = param.Length > 1 ? param[1].Trim() : string.Empty;

                        bool handled = false;
                        bool secured = parameter.StartsWith("~", StringComparison.InvariantCultureIgnoreCase);
                        CommandLineArgumentType type = CommandLineArgumentType.Parameter;
                        if (secured)
                        {
                            type      = CommandLineArgumentType.SecuredParameter;
                            parameter = parameter.Remove(0, 1);
                        }

                        handled = this.OnCommandLineParsing(parameter, value, type);

                        if (!handled)
                        {
                            if (secured)
                            {
                                if (this.Engine.SecureStringVariables.Contains(parameter))
                                {
                                    SecureString secString = new SecureString();
                                    foreach (char c in value)
                                    {
                                        secString.AppendChar(c);
                                    }
                                    secString.MakeReadOnly();
                                    this.Engine.SecureStringVariables[parameter] = secString;
                                }
                            }
                            else
                            {
                                if (this.Engine.StringVariables.Contains(parameter))
                                {
                                    this.Engine.StringVariables[parameter] = value;
                                }
                            }
                        }
                    }
                }
            }
        }