Advtools.AdvInterceptor.Options.ParseCommandLine C# (CSharp) Method

ParseCommandLine() public method

Parse the command-line arguments
public ParseCommandLine ( string args ) : bool
args string Command-line arguments
return bool
        public bool ParseCommandLine(string[] args)
        {
            // Help requested? (by default, no)
            bool help = false;

            // Definition of the command-line arguments and how to set the related configuration data
            var options = new OptionSet()
            {
                { "intercept=", "Intercept or forward requests",    (bool o) => Intercept = o },
                { "log=", "Level of messages to log",               (LogLevel o) => Level = o },
                { "config=", "Configuration file",                  (string o) => ConfigFile = o },
                { "default", "Generate a default configuration",    o => DefaultConfig = o != null },
                { "h|help", "Show this message",                    o => help = o != null }
            };

            try
            {
                // Parse the command-line arguments (thanks NDesk!)
                List<string> extra = options.Parse(args);
                // If there are some arguments not parsed, no name of pipe or an invalid port number, show the help
                if(extra.Count > 0)
                {
                    Console.WriteLine("Unknow parameter: {0}", extra[0]);
                    Console.WriteLine();
                    ShowHelp(options);
                    return false; // Do not continue the application
                }
            }
            catch(OptionException e)
            {
                // Something wrong with the arguments
                Console.WriteLine(e.Message);
                Console.WriteLine("Try 'ADVinterceptor --help' for more information.");
                return false;
            }

            // Show the help?
            if(help)
            {
                ShowHelp(options);
                return false;
            }

            // Continue the application
            return true;
        }

Usage Example

Example #1
0
 private static Options ParseCommandLine(string[] args)
 {
     Options options = new Options();
     if(!options.ParseCommandLine(args))
         return null;
     return options;
 }
All Usage Examples Of Advtools.AdvInterceptor.Options::ParseCommandLine