コード例 #1
0
ファイル: ArgumentHandler.cs プロジェクト: Earlz/monosump
        public Commands ParseCommands(string[] args)
        {
            if(args.Length==0)
            {
                PrintHelp();
                return null;
            }
            var commands=new Commands();
            var state=CurrentState.Bare;
            bool resetState=true;
            foreach(var arg in args)
            {
                resetState=true;
                if(state == CurrentState.Bare)
                {
                    switch(arg)
                    {
                    case "--help":
                        PrintHelp();
                        return null;
                    case "--identify":
                        commands.Identify=true;
                        break;
                    case "--json":
                        commands.JsonOut=true;
                        break;
                    case "--verbose":
                        commands.Verbose=true;
                        break;
                    case "--trigger":
                        state=CurrentState.Trigger;
                        resetState=false;
                        break;
                    case "--frequency":
                        state=CurrentState.Frequency;
                        resetState=false;
                        break;
                    case "--samples":
                        state=CurrentState.Samples;
                        resetState=false;
                        break;
                    case "--config":
                        state=CurrentState.Config;
                        resetState=false;
                        break;
                    default:
                        if(commands.DeviceName==null)
                        {
                            commands.DeviceName=arg;
                        }
                        else if(commands.DataOut==null)
                        {
                            commands.DataOut=arg;
                        }
                        else
                        {
                            throw new ApplicationException("unexpected extra argument after dataout");
                        }
                        break;
                    }
                }
                else if(state == CurrentState.Trigger)
                {
                    int n, v;
                    try
                    {
                    n=int.Parse(arg.Split(new char[]{'='})[0]);
                    v=int.Parse(arg.Split(new char[]{'='})[1]);
                    }catch
                    {
                        throw new ArgumentParseException("--trigger parameter is wrong. Should look like `N=V` where N and V are integers, like --trigger 10=1");
                    }
                    if(n>31 || n<0)
                    {
                        throw new ArgumentParseException("The channel number for a trigger can be no greater than 31 and no less than 0");
                    }
                    commands.Triggers.Add(n, v);
                }
                else if(state==CurrentState.Frequency)
                {
                    int f=0;
                    if(!int.TryParse(arg, out f))
                    {
                        throw new ArgumentParseException("--frequency parameter is wrong. Should look like F where F is an integer, like --frequency 1000");
                    }
                    commands.Frequency=f;
                }
                else if(state==CurrentState.Samples)
                {
                    int n=0;
                    if(!int.TryParse(arg, out n))
                    {
                        throw new ArgumentParseException("--samples parameter is wrong. Should look like N where N is an integer, like --samples 100");
                    }
                    commands.Samples=n;
                }
                else if(state==CurrentState.Config)
                {
                    commands.ConfigFile=arg;
                }
                else
                {
                    throw new NotImplementedException("that's not suppose to happen :/");
                }

                if(resetState)
                {
                    state=CurrentState.Bare;
                }
            }
            if(state!=CurrentState.Bare)
            {
                Console.WriteLine("Warning: probably an error parsing the last parameter on the command line");
            }
            if(commands.DeviceName==null)
            {
                throw new ArgumentParseException("No device name was given. See --help");
            }
            return commands;
        }