예제 #1
0
        /// <summary>
        /// Parse command line options. Throws ArgumentException for invalid options or
        /// missing required arguments.
        /// </summary>
        /// <param name="args">The command line options.</param>
        /// <exception cref="ArgumentException">ArgumentException</exception>
        public void Parse(string[] args)
        {
            if (args.Length == 0)
            {
                Usage();
                Environment.Exit(1);
            }
            for (int i = 0; i < args.Length; ++i)
            {
                ProgramOption option = ProgramOption.Create(args[i]);
                switch (option.Key)
                {
                case "-v":
                case "--verbose":
                    verbose = true;
                    break;

                case "-h":
                case "--help":
                    Usage();
                    Environment.Exit(0);
                    break;

                case "-V":
                case "--version":
                    Version();
                    Environment.Exit(0);
                    break;

                //
                // Application reason:
                //
                case "-i":
                case "--logon":
                    reason = Reason.Login;
                    break;

                case "-o":
                case "--logout":
                    reason = Reason.Logout;
                    break;

                case "-l":
                case "--list":
                    reason = Reason.List;
                    break;

                case "-F":
                case "--close":
                    reason = Reason.Close;
                    break;

                //
                // Filter options:
                //
                case "--id":
                    if (option.HasValue)
                    {
                        filter.EventID = int.Parse(option.Value);
                    }
                    else
                    {
                        filter.EventID = int.Parse(args[++i]);
                    }
                    break;

                case "--start":
                    if (option.HasValue)
                    {
                        filter.StartTime = DateTime.Parse(option.Value);
                    }
                    else
                    {
                        filter.StartTime = DateTime.Parse(args[++i]);
                    }
                    break;

                case "--end":
                    if (option.HasValue)
                    {
                        filter.EndTime = DateTime.Parse(option.Value);
                    }
                    else
                    {
                        filter.EndTime = DateTime.Parse(args[++i]);
                    }
                    break;

                case "--comp":
                    if (option.HasValue)
                    {
                        filter.Workstation = option.Value;
                    }
                    else
                    {
                        filter.Workstation = args[++i];
                    }
                    break;

                case "--host":
                    if (option.HasValue)
                    {
                        filter.Hostname = option.Value;
                    }
                    else
                    {
                        filter.Hostname = args[++i];
                    }
                    break;

                case "--ip":
                case "--ipaddr":
                    if (option.HasValue)
                    {
                        filter.IpAddress = option.Value;
                    }
                    else
                    {
                        filter.IpAddress = args[++i];
                    }
                    break;

                case "--hw":
                case "--hwaddr":
                case "--mac":
                    if (option.HasValue)
                    {
                        filter.HwAddress = option.Value;
                    }
                    else
                    {
                        filter.HwAddress = args[++i];
                    }
                    break;

                case "--user":
                case "--username":
                    if (option.HasValue)
                    {
                        filter.Username = option.Value;
                    }
                    else
                    {
                        filter.Username = args[++i];
                    }
                    break;

                case "--domain":
                    if (option.HasValue)
                    {
                        filter.Domain = option.Value;
                    }
                    else
                    {
                        filter.Domain = args[++i];
                    }
                    break;

                case "--first":
                    if (option.HasValue)
                    {
                        filter.FirstID = int.Parse(option.Value);
                    }
                    else
                    {
                        filter.FirstID = int.Parse(args[++i]);
                    }
                    break;

                case "--last":
                    if (option.HasValue)
                    {
                        filter.LastID = int.Parse(option.Value);
                    }
                    else
                    {
                        filter.LastID = int.Parse(args[++i]);
                    }
                    break;

                case "-L":
                case "--limit":
                    if (option.HasValue)
                    {
                        filter.Limit = int.Parse(option.Value);
                    }
                    else
                    {
                        filter.Limit = int.Parse(args[++i]);
                    }
                    break;

                //
                // Match options:
                //
                case "-a":
                case "--active":
                    match = LogonEventMatch.Active;
                    break;

                case "-c":
                case "--closed":
                    match = LogonEventMatch.Closed;
                    break;

                case "--between":
                    match = LogonEventMatch.Between;
                    break;

                case "--before":
                    match = LogonEventMatch.Before;
                    break;

                case "--after":
                    match = LogonEventMatch.After;
                    break;

                case "-e":
                case "--exact":
                    match = LogonEventMatch.Exact;
                    break;

                case "-t":
                case "--this":
                {
                    Network network = new Network();
                    filter.HwAddress   = network.HwAddress;
                    filter.Workstation = network.Computer;
                }
                break;

                //
                // Formatting:
                //
                case "-T":
                case "--tabbed":
                    format = Format.Tabbed;
                    break;

                case "-C":
                case "--compact":
                    format = Format.Compact;
                    break;

                case "-H":
                case "--human":
                    format = Format.Human;
                    break;

                case "-X":
                case "--XML":
                case "--xml":
                    format = Format.XML;
                    break;

                //
                // Miscellanous:
                //
                case "-r":
                case "--register":
                    reason = Reason.Register;
                    break;

                case "-u":
                case "--uninstall":
                    reason = Reason.Uninstall;
                    break;

                //
                // Authentication:
                //
                case "-U":
                    if (credentials == null)
                    {
                        credentials = new ClientCredentials();
                    }
                    if (option.HasValue)
                    {
                        credentials.UserName.UserName = option.Value;
                    }
                    else
                    {
                        credentials.UserName.UserName = args[++i];
                    }
                    break;

                case "-P":
                    if (credentials == null)
                    {
                        credentials = new ClientCredentials();
                    }
                    if (option.HasValue)
                    {
                        credentials.UserName.Password = option.Value;
                    }
                    else
                    {
                        credentials.UserName.Password = args[++i];
                    }
                    break;

                case "-W":
                case "--Windows":
                    if (credentials == null)
                    {
                        credentials = new ClientCredentials();
                    }
                    break;

                default:
                    throw new ArgumentException("Unknown option '" + args[i] + "'");
                }
            }

            if (reason == Reason.Unknown)
            {
                throw new ArgumentException("Missing -l, -i or -o option, see --help");
            }
            if (filter.LastID != 0)
            {
                if (match != LogonEventMatch.Between)
                {
                    match = LogonEventMatch.Between;
                }
                if (filter.EventID != 0)
                {
                    filter.FirstID = filter.EventID;    // Make ID an alias for FirstID
                }
            }
            if (filter.FirstID != 0 && filter.EventID == 0)
            {
                filter.EventID = filter.FirstID;        // Make FirstID an alias for ID
            }
        }
예제 #2
0
        /// <summary>
        /// Parse command line options. Throws ArgumentException for invalid options or
        /// missing required arguments.
        /// </summary>
        /// <param name="args">The command line options.</param>
        /// <exception cref="ArgumentException">ArgumentException</exception>
        public void Parse(string[] args)
        {
            for (int i = 0; i < args.Length; ++i)
            {
                ProgramOption option = ProgramOption.Create(args[i]);
                switch (option.Key)
                {
                case "-h":
                case "--help":
                    Usage();
                    Environment.Exit(0);
                    break;

                case "-V":
                case "--version":
                    Version();
                    Environment.Exit(0);
                    break;

                //
                // Miscellanous:
                //
                case "-i":
                case "--install":
                    reason = Reason.Install;
                    break;

                case "-r":
                case "--remove":
                    reason = Reason.Remove;
                    break;

                //
                // Authentication:
                //
                case "-U":
                    if (credentials == null)
                    {
                        credentials = new ClientCredentials();
                    }
                    if (option.HasValue)
                    {
                        credentials.UserName.UserName = option.Value;
                    }
                    else
                    {
                        credentials.UserName.UserName = args[++i];
                    }
                    break;

                case "-P":
                    if (credentials == null)
                    {
                        credentials = new ClientCredentials();
                    }
                    if (option.HasValue)
                    {
                        credentials.UserName.Password = option.Value;
                    }
                    else
                    {
                        credentials.UserName.Password = args[++i];
                    }
                    break;

                case "-W":
                case "--Windows":
                    if (credentials == null)
                    {
                        credentials = new ClientCredentials();
                    }
                    break;

                default:
                    throw new ArgumentException("Unknown option '" + args[i] + "'");
                }
            }
        }