Exemple #1
0
        void AppendSwitch(CommandLineSwitch @switch, Dictionary <string, CommandLineSwitch> result, int index)
        {
            CommandLineSwitch existingSwitch;

            if (!result.TryGetValue(@switch.Name, out existingSwitch))
            {
                this.CurrentSwitch       = @switch.Clone();
                this.CurrentSwitch.Index = index;
            }
            else
            {
                if (existingSwitch.IsMultiple)
                {
                    this.CurrentSwitch = existingSwitch;
                }
                else
                {
                    this.CurrentSwitch       = @switch.Clone();
                    this.CurrentSwitch.Index = index;
                }
            }

            AppendValueToSwitch(result, this.CurrentSwitch, @switch.Value);
            if ([email protected])
            {
                this.CurrentSwitch = null;
            }
        }
Exemple #2
0
        public CommandLineSwitch Clone()
        {
            CommandLineSwitch clone = new CommandLineSwitch();

            clone.CopyFrom(this);
            return(clone);
        }
Exemple #3
0
        string GetSwitchesString(CommandLineSwitch sw)
        {
            string result = "--" + sw.Name;

            if (!String.IsNullOrEmpty(sw.Alias))
            {
                result += ", -" + sw.Alias;
            }
            return(result);
        }
Exemple #4
0
 void CopyFrom(CommandLineSwitch other)
 {
     this.Name        = other.Name;
     this.Alias       = other.Alias;
     this.Description = other.Description;
     //this.Type  = other.Type;
     this.IsMultiple      = other.IsMultiple;
     this.DefaultValue    = other.DefaultValue;
     this.IsDefaultOption = other.IsDefaultOption;
     this.ExpectValue     = other.ExpectValue;
     this.Value           = other.Value;
     this.Index           = other.Index;
 }
Exemple #5
0
        CommandLineSwitch CreateSwitch(string name, string value, CommandLineConfiguration configuration)
        {
            CommandLineSwitch result = FindSwitch(configuration, name);

            if (result != null)
            {
                result = result.Clone();
            }
            else
            {
                result      = new CommandLineSwitch();
                result.Name = name;
            }
            result.Value = value;
            return(result);
        }
Exemple #6
0
        public Dictionary <string, CommandLineSwitch> Parse(string[] args, CommandLineConfiguration configuration)
        {
            StringComparer comparer = configuration != null && configuration.IsCaseSensitive ? StringComparer.InvariantCulture: StringComparer.InvariantCultureIgnoreCase;
            Dictionary <string, CommandLineSwitch> result = new Dictionary <string, CommandLineSwitch>(comparer);

            if (args == null || args.Length <= 0 || configuration == null /* || configuration.Count <= 0*/)
            {
                return(result);
            }


            this.CurrentSwitch = null;

            int count = args.Length;

            for (int i = 0; i < count; i++)
            {
                CommandLineSwitch @switch = TryParseSwitch(args[i], configuration);
                if (@switch != null)
                {
                    AppendSwitch(@switch, result, i);
                }
                else
                {
                    AppendValue(args[i], result, i);
                }
            }

            if (UnclaimedValues.Count > 0)
            {
                CommandLineSwitch defaultOption = DetectDefaultOptionName(configuration);
                if (defaultOption != null)
                {
                    CommandLineSwitch item = defaultOption.Clone();
                    item.Values = UnclaimedValues;
                    item.Index  = UnclaimedFirstIndex;
                    result[defaultOption.Name] = item;
                    //result[defaultOption.Name] = unclaimedValues;
                }
            }

            return(result);
        }
Exemple #7
0
        static RunnerParameters AnalyzeCommandLineSwitches(Dictionary <string, CommandLineSwitch> switches, string[] args)
        {
            RunnerParameters result = new RunnerParameters();

            if (!switches.ContainsKey("exec") && !switches.ContainsKey("pid"))
            {
                Logger.ErrorFormat("Please specify target process ID or command line");
                return(null);
            }

            if (switches.ContainsKey("exec"))
            {
                CommandLineSwitch @switch = switches["exec"];
                result.TargetProcessCommandLine = @switch.Values[0];
                result.TargetProcessArgs        = CreateTargetProcessArguments(args, @switch.Index + 1);
            }
            if (switches.ContainsKey("pid"))
            {
                CommandLineSwitch @switch = switches["pid"];
                string            pidText = @switch.Value;
                int pid = TryParsePid(pidText);
                if (pid == 0)
                {
                    Logger.ErrorFormat("Unable to parse target process Id: {0}", pidText);
                    return(null);
                }
                result.Pid = pid;
            }
            result.IsWinforms = switches.ContainsKey("win");
            result.IsWpf      = switches.ContainsKey("wpf");
            if (result.IsWinforms == result.IsWpf)
            {
                Logger.ErrorFormat("Please specify target process type: --win or --wpf");
                return(null);
            }
            return(result);
        }
Exemple #8
0
        string GetArgUsage(CommandLineSwitch commandLineSwitch)
        {
            string name = commandLineSwitch.Name;

            if (commandLineSwitch.ExpectValue)
            {
                if (commandLineSwitch.IsDefaultOption)
                {
                    name = "<" + name + ">";
                }
                if (commandLineSwitch.IsMultiple)
                {
                    return(String.Format("[--{0}=<value1>, ...]", name));
                }
                else
                {
                    return(String.Format("[--{0}=<value>]", name));
                }
            }
            else
            {
                return(String.Format("[--{0}]", name));
            }
        }
Exemple #9
0
 void AppendValueToSwitch(Dictionary <string, CommandLineSwitch> result, CommandLineSwitch @switch, string value)
 {
     if (@switch.IsMultiple)
     {
         //List<object> values;
         List <string> values;
         if (!result.ContainsKey(@switch.Name))
         {
             values         = new List <string>();
             @switch.Value  = null;
             @switch.Values = values;
             //values = new List<object>();
             //result[@switch.Name] = values;
             result[@switch.Name] = @switch;
         }
         else
         {
             values = result[@switch.Name].Values;
             //values = (List<object>)result[@switch.Name];
         }
         if (!String.IsNullOrEmpty(value))
         {
             values.Add(value);
         }
     }
     else
     {
         @switch.Value        = value;
         result[@switch.Name] = @switch;
         if (!String.IsNullOrEmpty(value))
         {
             this.CurrentSwitch = null;
         }
     }
     //result[@switch.Name] = value;
 }