public override void Execute()
        {
            base.Execute();

            List <string>      singleOptionList = ListDirectoryOptions.GetSingleOptions();
            CommandLineOptions cloptions        = CommandLineParser.Parse(Arguments.ToArray <string>(), singleOptionList.ToArray());

            options = ParseOptions(cloptions);
            CheckOptions(options);

            if (options.IsSetHelp)
            {
                RaiseCommandLineUsage(this, ListDirectoryOptions.Usage);
            }
            else if (options.IsSetVersion)
            {
                RaiseCommandLineUsage(this, Version);
            }
            else
            {
                StartListDirectory();
            }

            Terminate();
        }
 private static void CheckOptions(ListDirectoryCommandLineOptions checkedOptions)
 {
     if (!checkedOptions.IsSetHelp && !checkedOptions.IsSetVersion)
     {
         if (string.IsNullOrEmpty(checkedOptions.InputDirectory))
         {
             throw new CommandLineException(string.Format(CultureInfo.CurrentCulture,
                                                          "Option used in invalid context -- {0}", "must specify a directory."));
         }
     }
 }
        public override void Execute()
        {
            base.Execute();

              List<string> singleOptionList = ListDirectoryOptions.GetSingleOptions();
              CommandLineOptions cloptions = CommandLineParser.Parse(Arguments.ToArray<string>(), singleOptionList.ToArray());
              options = ParseOptions(cloptions);
              CheckOptions(options);

              if (options.IsSetHelp)
              {
            RaiseCommandLineUsage(this, ListDirectoryOptions.Usage);
              }
              else if (options.IsSetVersion)
              {
            RaiseCommandLineUsage(this, Version);
              }
              else
              {
            StartListDirectory();
              }

              Terminate();
        }
        private static ListDirectoryCommandLineOptions ParseOptions(CommandLineOptions commandLineOptions)
        {
            if (commandLineOptions == null)
            {
                throw new CommandLineException(string.Format(CultureInfo.CurrentCulture,
                                                             "Option used in invalid context -- {0}", "must specify a option."));
            }

            ListDirectoryCommandLineOptions targetOptions = new ListDirectoryCommandLineOptions();

            if (commandLineOptions.Arguments.Count >= 0)
            {
                foreach (var arg in commandLineOptions.Arguments.Keys)
                {
                    ListDirectoryOptionType optionType = ListDirectoryOptions.GetOptionType(arg);
                    if (optionType == ListDirectoryOptionType.None)
                    {
                        throw new CommandLineException(
                                  string.Format(CultureInfo.CurrentCulture, "Option used in invalid context -- {0}",
                                                string.Format(CultureInfo.CurrentCulture, "cannot parse the command line argument : [{0}].", arg)));
                    }

                    switch (optionType)
                    {
                    case ListDirectoryOptionType.InputDirectory:
                        targetOptions.InputDirectory = commandLineOptions.Arguments[arg];
                        break;

                    case ListDirectoryOptionType.Directory:
                        targetOptions.IsSetDirectory = true;
                        break;

                    case ListDirectoryOptionType.File:
                        targetOptions.IsSetFile = true;
                        break;

                    case ListDirectoryOptionType.List:
                        targetOptions.IsSetList = true;
                        break;

                    case ListDirectoryOptionType.Help:
                        targetOptions.IsSetHelp = true;
                        break;

                    case ListDirectoryOptionType.Version:
                        targetOptions.IsSetVersion = true;
                        break;
                    }
                }
            }

            if (commandLineOptions.Parameters.Count > 0)
            {
                if (string.IsNullOrEmpty(targetOptions.InputDirectory))
                {
                    targetOptions.InputDirectory = commandLineOptions.Parameters.First();
                }
            }

            // set default the current directory
            if (string.IsNullOrEmpty(targetOptions.InputDirectory))
            {
                targetOptions.InputDirectory = @".";
            }

            // set default options
            if (!targetOptions.IsSetDirectory && !targetOptions.IsSetFile)
            {
                targetOptions.IsSetDirectory = true;
                targetOptions.IsSetFile      = true;
            }

            return(targetOptions);
        }
 private static void CheckOptions(ListDirectoryCommandLineOptions checkedOptions)
 {
     if (!checkedOptions.IsSetHelp && !checkedOptions.IsSetVersion)
       {
     if (string.IsNullOrEmpty(checkedOptions.InputDirectory))
     {
       throw new CommandLineException(string.Format(CultureInfo.CurrentCulture,
     "Option used in invalid context -- {0}", "must specify a directory."));
     }
       }
 }
        private static ListDirectoryCommandLineOptions ParseOptions(CommandLineOptions commandLineOptions)
        {
            if (commandLineOptions == null)
            throw new CommandLineException(string.Format(CultureInfo.CurrentCulture,
              "Option used in invalid context -- {0}", "must specify a option."));

              ListDirectoryCommandLineOptions targetOptions = new ListDirectoryCommandLineOptions();

              if (commandLineOptions.Arguments.Count >= 0)
              {
            foreach (var arg in commandLineOptions.Arguments.Keys)
            {
              ListDirectoryOptionType optionType = ListDirectoryOptions.GetOptionType(arg);
              if (optionType == ListDirectoryOptionType.None)
            throw new CommandLineException(
              string.Format(CultureInfo.CurrentCulture, "Option used in invalid context -- {0}",
              string.Format(CultureInfo.CurrentCulture, "cannot parse the command line argument : [{0}].", arg)));

              switch (optionType)
              {
            case ListDirectoryOptionType.InputDirectory:
              targetOptions.InputDirectory = commandLineOptions.Arguments[arg];
              break;
            case ListDirectoryOptionType.Directory:
              targetOptions.IsSetDirectory = true;
              break;
            case ListDirectoryOptionType.File:
              targetOptions.IsSetFile = true;
              break;
            case ListDirectoryOptionType.List:
              targetOptions.IsSetList = true;
              break;
            case ListDirectoryOptionType.Help:
              targetOptions.IsSetHelp = true;
              break;
            case ListDirectoryOptionType.Version:
              targetOptions.IsSetVersion = true;
              break;
              }
            }
              }

              if (commandLineOptions.Parameters.Count > 0)
              {
            if (string.IsNullOrEmpty(targetOptions.InputDirectory))
            {
              targetOptions.InputDirectory = commandLineOptions.Parameters.First();
            }
              }

              // set default the current directory
              if (string.IsNullOrEmpty(targetOptions.InputDirectory))
              {
            targetOptions.InputDirectory = @".";
              }

              // set default options
              if (!targetOptions.IsSetDirectory && !targetOptions.IsSetFile)
              {
            targetOptions.IsSetDirectory = true;
            targetOptions.IsSetFile = true;
              }

              return targetOptions;
        }