Example #1
0
        public ProgramArguments(string[] args, ArgumentPredicate argumentValidator)
        {
            if (args.Length == 0)
            {
                flags     = null;
                variables = null;
            }
            else
            {
                flags     = new HashSet <string>();
                variables = new Dictionary <string, string>(4);

                HashSet <string> identifiers = new HashSet <string>();
                Action <string>  setError    = err => Error = err;

                for (int index = 0; index < args.Length; index++)
                {
                    if (args[index][0] == '-' && args.Length > 1)
                    {
                        string name       = args[index].Substring(1);
                        string identifier = name.IndexOf(':') == -1 ? name : name.Substring(0, name.IndexOf(':'));

                        bool isVariable = index < args.Length - 1 && args[index + 1][0] != '-';

                        if (!identifiers.Add(identifier))
                        {
                            Error = Lang.Get["ErrorInvalidArgsDuplicateIdentifier", identifier];
                            break;
                        }

                        if (!argumentValidator(isVariable ? new Argument(name, args[index + 1]) : new Argument(name), setError))
                        {
                            break;
                        }

                        if (isVariable)
                        {
                            variables[name] = args[index + 1];
                        }
                        else
                        {
                            flags.Add(name);
                        }
                    }
                }
            }
        }
        public ProgramArguments(string[] args, ArgumentPredicate argumentValidator)
        {
            if (args.Length == 0){
                flags = null;
                variables = null;
            }
            else{
                flags = new HashSet<string>();
                variables = new Dictionary<string, string>(4);

                HashSet<string> identifiers = new HashSet<string>();
                Action<string> setError = err => Error = err;

                for(int index = 0; index < args.Length; index++){
                    if (args[index][0] == '-' && args.Length > 1){
                        string name = args[index].Substring(1);
                        string identifier = name.IndexOf(':') == -1 ? name : name.Substring(0, name.IndexOf(':'));

                        bool isVariable = index < args.Length-1 && args[index+1][0] != '-';

                        if (!identifiers.Add(identifier)){
                            Error = Lang.Get["ErrorInvalidArgsDuplicateIdentifier", identifier];
                            break;
                        }

                        if (!argumentValidator(isVariable ? new Argument(name, args[index+1]) : new Argument(name), setError)){
                            break;
                        }

                        if (isVariable){
                            variables[name] = args[index+1];
                        }
                        else{
                            flags.Add(name);
                        }
                    }
                }
            }
        }