Beispiel #1
0
        // Set a parameter value
        public bool SetParameterValue(string pName, string pVal)
        {
            var ret = false;

            foreach (FieldInfo fi in this.GetType().GetFields())
            {
                foreach (Attribute attr in Attribute.GetCustomAttributes(fi))
                {
                    ConfigParam cp = attr as ConfigParam;
                    if (cp != null)
                    {
                        if (cp.name == pName)
                        {
                            fi.SetValue(this, ParamBlock.ConvertToObj(cp.valueType, pVal));
                            ret = true;
                            break;
                        }
                    }
                }
                if (ret)
                {
                    break;
                }
            }
            return(ret);
        }
Beispiel #2
0
        // Store the value for the parameter.
        // If we accept the value as a good value for the parameter, return 1 else 0.
        // A 'good value' is one that does not start with '-' or is not after a boolean parameter.
        // Return the number of parameters to advance the parameter line. That means, return
        //    a zero of we didn't used the next parameter and a 1 if the next parameter
        //    was used as a value so don't consider it the next parameter.
        private int AddCommandLineParameter(string pParm, string val)
        {
            // System.Console.WriteLine(String.Format("AddCommandLineParameter: parm={0}, val={1}", pParm, val));
            int    ret  = 1; // start off assuming the next token is the value we're setting
            string parm = pParm.ToLower();

            // Strip leading hyphens
            while (parm[0] == '-')
            {
                parm = parm.Substring(1);
            }

            // If the boolean parameter starts with "no", turn it off rather than on.
            string positiveAssertion = "true";

            if (parm.Length > 2 && parm[0] == 'n' && parm[1] == 'o')
            {
                string maybeParm = parm.Substring(2);
                if (TryGetParameterInfo(parm, out ConfigParam bcp, out FieldInfo bfi))
                {
                    if (bcp.valueType == typeof(Boolean))
                    {
                        // The parameter without the 'no' exists and is a boolean
                        positiveAssertion = "false";
                        parm = maybeParm;
                    }
                }
            }

            // If the next token starts with a parameter mark, it's not really a value
            if (val == null)
            {
                ret = 0;    // the next token is not used here to set the value
            }
            else
            {
                if (val[0] == '-')
                {
                    val = null; // don't use the next token as a value
                    ret = 0;    // the next token is not used here to set the value
                }
            }

            if (TryGetParameterInfo(parm, out ConfigParam cp, out FieldInfo fi))
            {
                // If the parameter is a boolean type and the next value is not a parameter,
                //      don't try to take up the next value.
                // This handles boolean flags.
                // If there is a value next (val != null) and that value is not the
                //    values 'true' or 'false' or 't' or 'f', then ignore the next value
                //    as not belonging to this flag. THis allows (and the logic above)
                //    allows:
                //        "--flag --otherFlag ...",
                //        "--flag something ...",
                //        "--flag true --otherFlag ...",
                //        "--noflag --otherflag ...",
                //        etc
                if (cp.valueType == typeof(Boolean))
                {
                    if (val != null)
                    {
                        string valL = val.ToLower();
                        if (valL != "true" && valL != "t" && valL != "false" && valL != "f")
                        {
                            // The value is not associated with this boolean so ignore it
                            val = null; // don't use the val token
                            ret = 0;    // the next token is not used here to set the value
                        }
                    }
                    if (val == null)
                    {
                        // If the value is assumed, use the value based on the optional 'no'
                        val = positiveAssertion;
                    }
                }
                // Set the named parameter to the passed value
                fi.SetValue(this, ParamBlock.ConvertToObj(cp.valueType, val));
            }