public void AddSwitch(SwitchInfo arg)
        {
            bool valid = false;

            // short switch
            if (!String.IsNullOrEmpty(arg.ShortSwitch))
            {
                string s = arg.ShortSwitch;
                if (!s.StartsWith("-"))
                {
                    s = "-" + s;
                }
                AddSwitch(s, arg);
                valid = true;
            }

            // long switch
            if (!String.IsNullOrEmpty(arg.LongSwitch))
            {
                string s = arg.LongSwitch;
                if (!s.StartsWith("--"))
                {
                    s = "--" + s;
                }
                AddSwitch(s, arg);
                valid = true;
            }

            if (valid)
            {
                m_args.Add(arg);
            }
        }
		public void AddSwitch(SwitchInfo arg)
		{
			bool valid = false;

			// short switch
			if (!String.IsNullOrEmpty(arg.ShortSwitch))
			{
				string s = arg.ShortSwitch;
				if (!s.StartsWith("-"))
					s = "-" + s;
				AddSwitch(s, arg);
				valid = true;
			}

			// long switch
			if (!String.IsNullOrEmpty(arg.LongSwitch))
			{
				string s = arg.LongSwitch;
				if (!s.StartsWith("--"))
					s = "--" + s;
				AddSwitch(s, arg);
				valid = true;
			}

			if (valid)
				m_args.Add(arg);
		}
 private void AddSwitch(string s, SwitchInfo arg)
 {
     if (m_dict.ContainsKey(s))
     {
         throw new ArgumentException("Duplicate switch: " + s);
     }
     m_dict.Add(s, arg);
 }
Exemple #4
0
        private static string FormatSwitchForHelp(SwitchInfo arg)
        {
            StringBuilder buf = new StringBuilder();

            // work out the value placeholder
            string valueDesc = null;

            if (arg.Type == typeof(uint?))
            {
                if (arg.ValueDescription == null)
                {
                    valueDesc = "int";
                }
                else
                {
                    valueDesc = arg.ValueDescription;
                }
            }
            else if (arg.Type != typeof(bool))
            {
                if (arg.ValueDescription == null)
                {
                    valueDesc = "string";
                }
                else
                {
                    valueDesc = arg.ValueDescription;
                }
            }

            if (!String.IsNullOrEmpty(arg.ShortSwitch))
            {
                buf.Append(arg.ShortSwitch);
                if (valueDesc != null)
                {
                    buf.AppendFormat(" <{0}>", valueDesc);
                }
            }

            if (!String.IsNullOrEmpty(arg.LongSwitch))
            {
                if (buf.Length > 0)
                {
                    buf.Append(',');
                }
                buf.Append(arg.LongSwitch);
                if (valueDesc != null)
                {
                    buf.AppendFormat(" <{0}>", valueDesc);
                }
            }

            return(buf.ToString());
        }
Exemple #5
0
        protected SwitchesDefBase()
        {
            this.Args = new SwitchCollection(this);

            foreach (PropertyInfo prop in this.GetType().GetProperties())
            {
                // ignore ExtraArguments and properties without setters
                if (prop.Name == "ExtraArguments" || prop.GetSetMethod() == null)
                {
                    continue;
                }

                // check for SwitchDef attribute
                var attr = prop.GetAttribute <SwitchDefAttribute>();
                if (attr != null)
                {
                    if (prop.PropertyType == typeof(bool) || prop.PropertyType == typeof(string) ||
                        prop.PropertyType == typeof(uint?) || prop.PropertyType.Implements <IList <string> >())
                    {
                        var arg = new SwitchInfo(prop);
                        if (attr.ShortSwitch != null)
                        {
                            arg.ShortSwitch = attr.ShortSwitch;
                        }
                        if (attr.LongSwitch != null)
                        {
                            arg.LongSwitch = attr.LongSwitch;
                        }
                        arg.Description      = attr.Description;
                        arg.ValueDescription = attr.ValueDescription;

                        if (prop.HasAttribute <SwitchHiddenAttribute>())
                        {
                            arg.Hidden = true;
                        }

                        Args.AddSwitch(arg);
                    }
                    else
                    {
                        throw new ArgumentException(String.Format("Argument {0} is marked as a commandline arg, but is not a supported type", prop.Name));
                    }
                }
            }
        }
        public void Set(string s, object value)
        {
            SwitchInfo arg = null;

            if (!m_dict.TryGetValue(s, out arg))
            {
                throw new CommandLineArgsException("Unrecognised switch: " + s);
            }

            try
            {
                if (arg.Property.PropertyType.Implements <IList <string> >())
                {
                    IList <string> list = (IList <string>)arg.Property.GetValue(m_def, null);
                    if (list == null)
                    {
                        list = (IList <string>)Activator.CreateInstance(arg.Property.PropertyType);
                        arg.Property.SetValue(m_def, list, null);
                    }
                    list.Add((string)value);
                }
                else
                {
                    arg.Property.SetValue(m_def, value, null);
                }
            }
            catch (TargetInvocationException tie)
            {
                if (tie.InnerException == null)
                {
                    throw;
                }
                else
                {
                    throw tie.InnerException;
                }
            }
        }
		protected SwitchesDefBase()
		{
			this.Args = new SwitchCollection(this);

			foreach (PropertyInfo prop in this.GetType().GetProperties())
			{
				// ignore ExtraArguments and properties without setters
				if (prop.Name == "ExtraArguments" || prop.GetSetMethod() == null)
					continue;

				// check for SwitchDef attribute
				var attr = prop.GetAttribute<SwitchDefAttribute>();
				if (attr != null)
				{
					if (prop.PropertyType == typeof(bool) || prop.PropertyType == typeof(string) ||
							prop.PropertyType == typeof(uint?) || prop.PropertyType.Implements<IList<string>>())
					{
						var arg = new SwitchInfo(prop);
						if (attr.ShortSwitch != null)
							arg.ShortSwitch = attr.ShortSwitch;
						if (attr.LongSwitch != null)
							arg.LongSwitch = attr.LongSwitch;
						arg.Description = attr.Description;
						arg.ValueDescription = attr.ValueDescription;

						if (prop.HasAttribute<SwitchHiddenAttribute>())
							arg.Hidden = true;

						Args.AddSwitch(arg);
					}
					else
					{
						throw new ArgumentException(String.Format("Argument {0} is marked as a commandline arg, but is not a supported type", prop.Name));
					}
				}
			}
		}
		private void AddSwitch(string s, SwitchInfo arg)
		{
			if (m_dict.ContainsKey(s))
				throw new ArgumentException("Duplicate switch: " + s);
			m_dict.Add(s, arg);
		}
		private static string FormatSwitchForHelp(SwitchInfo arg)
		{
			StringBuilder buf = new StringBuilder();

			// work out the value placeholder
			string valueDesc = null;
			if (arg.Type == typeof(uint?))
			{
				if (arg.ValueDescription == null)
					valueDesc = "int";
				else
					valueDesc = arg.ValueDescription;
			}
			else if (arg.Type != typeof(bool))
			{
				if (arg.ValueDescription == null)
					valueDesc = "string";
				else
					valueDesc = arg.ValueDescription;
			}

			if (!String.IsNullOrEmpty(arg.ShortSwitch))
			{
				buf.Append(arg.ShortSwitch);
				if (valueDesc != null)
					buf.AppendFormat(" <{0}>", valueDesc);
			}

			if (!String.IsNullOrEmpty(arg.LongSwitch))
			{
				if (buf.Length > 0)
					buf.Append(',');
				buf.Append(arg.LongSwitch);
				if (valueDesc != null)
					buf.AppendFormat(" <{0}>", valueDesc);
			}

			return buf.ToString();
		}