Пример #1
0
        public static ListDirectoryOptionType GetOptionType(string option)
        {
            ListDirectoryOptionType optionType = ListDirectoryOptionType.None;

            foreach (var pair in Options)
            {
                foreach (var item in pair.Value)
                {
                    if (item == option)
                    {
                        optionType = pair.Key;
                        break;
                    }
                }
            }

            return(optionType);
        }
Пример #2
0
        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);
        }