Пример #1
0
        public void ShowHelp_WithMexicanLocale_ShowsSpanishHelpText()
        {
            using (LocalizationExtensions.WithUiCulture(new CultureInfo("es-MX")))
            {
                // Arrange
                var expected = @"Forma de uso: clipr [ -h|--help ] [ --version ] [ --turnonthepower ] [ -s S ] [ -c C ] FILETOADD
Argumentos dependientes de la posición:
 FileToAdd         Archivo para añadir a la cosa.

Argumentos opcionales:
 --turnonthepower  Establecer el nivel de potencia de 9001.
 -c                Un contador fresco.
 -h, --help        Muestra esta ayuda
 -s                Fecha de inicio.
 --version         Muestra la versión del ejecutable";

                var opt    = new LocalizationOptions();
                var parser = new CliParser <LocalizationOptions>(opt);
                var help   = new AutomaticHelpGenerator <LocalizationOptions>();

                // Act
                var actual = help.GetHelp(parser.Config);

                // Assert
                Assert.AreEqual(expected, actual);
            }
        }
Пример #2
0
        public void ShowHelp_WithAmericanLocale_ShowsEnglishHelpText()
        {
            using (LocalizationExtensions.WithUiCulture(new CultureInfo("en-US")))
            {
                // Arrange
                var expected = @"Usage: clipr [ -h|--help ] [ --version ] [ --turnonthepower ] [ -s S ] [ -c C ] FILETOADD
Positional Arguments:
 FileToAdd         File to add to the thing.

Optional Arguments:
 --turnonthepower  Set power level to 9001.
 -c                A cool counter.
 -h, --help        Display this help document.
 -s                Start date.
 --version         Displays the version of the current executable.";

                var opt    = new LocalizationOptions();
                var parser = new CliParser <LocalizationOptions>(opt);
                var help   = new AutomaticHelpGenerator <LocalizationOptions>(); // TODO simplify work required to get help info

                // Act
                var actual = help.GetHelp(parser.Config);

                // Assert
                Assert.AreEqual(expected, actual);
            }
        }
Пример #3
0
        static void Main()
        {
            FluentWithVerb("-n3 add oranges.txt".Split());
            FluentConditional("http", "-u http://file".Split());
            //DictBackendMethodConfig("-n frank".Split());
            //CustomDateTime("-d 20140730 2013-09-10".Split());
            ParseRequiredNamedArgument("-c -d 10/13/2010".Split());

            var parser = new CliParser <RequiredNamedArgument>(new RequiredNamedArgument());
            var help   = new AutomaticHelpGenerator <RequiredNamedArgument>();

            Console.WriteLine(help.GetHelp(parser.Config));
        }
Пример #4
0
        public void Help_With40CharacterDisplayWidth_PrintsUpTo40Characters()
        {
            const string expected = " -n, --name  Lorem ipsum dolor sit amet,\r";

            var opt    = new OptionsWithLongDescription();
            var parser = new CliParser <OptionsWithLongDescription>(opt);
            var gen    = new AutomaticHelpGenerator <OptionsWithLongDescription>
            {
                DisplayWidth = 40
            };


            var help = gen.GetHelp(parser.Config).Split('\n');

            Assert.AreEqual(expected, help[3]);
        }
        public static string GetValues <TOptions>(this AutomaticHelpGenerator <TOptions> help, TOptions settings)
        {
            var sb = new StringBuilder();

            sb.AppendLine("Current settings:");

            var properties = typeof(TOptions).GetProperties();
            var options    = properties.Where(p => p.IsDefined(typeof(ArgumentAttribute), true));

            foreach (var option in options)
            {
                var value = option.GetValue(settings);
                sb.AppendLine($" {option.Name}: {value}");
            }

            return(sb.ToString());
        }
        public static Arguments ParseArguments(string[] rawArgs)
        {
            var args   = new Arguments();
            var parser = new CliParser <Arguments>(args);
            var help   = new AutomaticHelpGenerator <Arguments>();

            try
            {
                parser.Parse(rawArgs);
                return(args);
            }
            catch (Exception)
            {
                Cli.Write(help.GetHelp(parser.Config), ConsoleColor.Yellow);
                throw;
            }
        }
Пример #7
0
        public void ParseNumber_WithSpanishLocaleAndSpanishNumberFormat_ParsesNumberCorrectly()
        {
            using (LocalizationExtensions.WithUiCulture(new CultureInfo("es-ES")))
            {
                // Arrange
                var expected = 2.3;

                var opt    = new LocalizationOptions();
                var parser = new CliParser <LocalizationOptions>(opt);
                var help   = new AutomaticHelpGenerator <LocalizationOptions>();

                // Act
                parser.Parse("-c 2,3 file.txt".Split());

                // Assert
                Assert.AreEqual(expected, opt.MyCounter);
            }
        }
Пример #8
0
        public void ParseDate_WithMexicanLocaleAndMexicanDateFormat_ParsesDateCorrectly()
        {
            using (LocalizationExtensions.WithUiCulture(new CultureInfo("es-MX")))
            {
                // Arrange
                var expected = new DateTime(2016, 3, 12);

                var opt    = new LocalizationOptions();
                var parser = new CliParser <LocalizationOptions>(opt);
                var help   = new AutomaticHelpGenerator <LocalizationOptions>();

                // Act
                parser.Parse("-s 12/3/2016 file.txt".Split());

                // Assert
                Assert.AreEqual(expected, opt.StartDate);
            }
        }
Пример #9
0
        public void ParseDate_WithSpanishLocaleAndSpanishDateFormat_ParsesDateCorrectly()
        {
            using (LocalizationExtensions.WithUiCulture(new CultureInfo("es-ES")))
            {
                // Arrange
                var expected = new DateTime(2016, 3, 12);

                var opt = new LocalizationOptions();
                var parser = new CliParser<LocalizationOptions>(opt);
                var help = new AutomaticHelpGenerator<LocalizationOptions>();

                // Act
                parser.Parse("-s 12/3/2016 file.txt".Split());

                // Assert
                Assert.AreEqual(expected, opt.StartDate);
            }
        }
Пример #10
0
            public ICommand CreateCommandFromOptions()
            {
                if (_options.ListDevices != null)
                {
                    return(CreateListDevicesCommand());
                }
                if (_options.Synchronize != null)
                {
                    return(CreateSynchronizeCommand());
                }

                var usageHelper = new AutomaticHelpGenerator <Options>();
                var usage       = usageHelper.GetUsage(); //TODO uplne nefunguje

                _reportMessage(new SimpleCommandMessage(usage, CommandMessageType.Error));
                Environment.Exit(-1);
                throw new ArgumentException("Shouldn't happen. Command line options validated, but cannot create a command.");
            }
Пример #11
0
        public void Help_WithRequiredNamedArgumentAndValue_PlacesSpaceBetweenArgumentAndMetaVar()
        {
            const string expected = @"Usage: clipr [ -h|--help ] [ --version ] -a|--optiona A [ -b|--optionb B ]
Required Arguments:
 -a, --optiona  The A option.

Optional Arguments:
 -b, --optionb  The B option.
 -h, --help     Display this help document.
 --version      Displays the version of the current executable.";

            var opt    = new OptionsWithRequiredValue();
            var parser = new CliParser <OptionsWithRequiredValue>(opt);
            var gen    = new AutomaticHelpGenerator <OptionsWithRequiredValue>();

            var help = gen.GetHelp(parser.Config);

            Assert.AreEqual(expected, help);
        }
Пример #12
0
        public void Help_WithVerbs_AddsVerbDescriptionsToHelpOutput()
        {
            const string expected = @"Usage: clipr [ -h|--help ] [ --version ] <command>
Optional Arguments:
 -h, --help  Display this help document.
 --version   Displays the version of the current executable.

Commands:
 verb1       A verb for names
 verb2       A verb for ages";

            var opt    = new OptionsWithVerbs();
            var parser = new CliParser <OptionsWithVerbs>(opt);
            var gen    = new AutomaticHelpGenerator <OptionsWithVerbs>();

            var help = gen.GetHelp(parser.Config);

            Assert.AreEqual(expected, help);
        }
Пример #13
0
        public void Help_WithRequiredNamedArgument_ShowsArgumentInRequiredSection()
        {
            const string expected = @"Usage: clipr [ -h|--help ] [ --version ] -c|--confirm

 This is a set of options.

Required Arguments:
 -c, --confirm  Confirms that the action is intended.

Optional Arguments:
 -h, --help     Display this help document.
 --version      Displays the version of the current executable.";

            var opt    = new RequiredNamedOptions();
            var parser = new CliParser <RequiredNamedOptions>(opt);
            var gen    = new AutomaticHelpGenerator <RequiredNamedOptions>();

            var help = gen.GetHelp(parser.Config);

            Assert.AreEqual(expected, help);
        }
Пример #14
0
        private static Options Configure(string[] args)
        {
            var options = CliParser.StrictParse <Options>(args);
            var parser  = new CliParser <Options>(options);
            var help    = new AutomaticHelpGenerator <Options>();

            var usage = help.GetUsage(parser.Config);

            Console.ForegroundColor = ConsoleColor.DarkYellow;
            Console.WriteLine(usage);
            Console.ResetColor();
            Console.WriteLine();

            var current = help.GetValues(options);

            Console.ForegroundColor = ConsoleColor.White;
            Console.WriteLine(current);
            Console.ResetColor();
            Console.WriteLine();

            return(options);
        }
Пример #15
0
        static void PrintLocalizedHelp()
        {
            var culture = Thread.CurrentThread.CurrentUICulture;

            var opt    = new LocalizationOptions();
            var parser = new CliParser <LocalizationOptions>(opt);
            var help   = new AutomaticHelpGenerator <LocalizationOptions>();

            parser.Parse("--turnonthepower -s 3/12/2016 -c 2.3 file1.txt".Split());
            Console.WriteLine(opt.StartDate.Month); // 3
            Console.WriteLine(opt.MyCounter);       // 2.3

            Console.WriteLine(help.GetHelp(parser.Config));

            Thread.CurrentThread.CurrentUICulture = new CultureInfo("es-MX");

            var optMx    = new LocalizationOptions();
            var parserMx = new CliParser <LocalizationOptions>(optMx);

            parserMx.Parse("--turnonthepower -s 12/3/2016 -c 2.3 file1.txt".Split());
            Console.WriteLine(optMx.StartDate.Month); // 3
            Console.WriteLine(optMx.MyCounter);       // 2.3

            Console.WriteLine(help.GetHelp(parserMx.Config));

            Thread.CurrentThread.CurrentUICulture = new CultureInfo("es-ES");

            var optEs    = new LocalizationOptions();
            var parserEs = new CliParser <LocalizationOptions>(optEs);

            parserEs.Parse("--turnonthepower -s 12/3/2016 -c 2,3 file1.txt".Split());
            Console.WriteLine(optEs.StartDate.Month); // 3
            Console.WriteLine(optEs.MyCounter);       // 2.3

            Console.WriteLine(help.GetHelp(parserEs.Config));

            Thread.CurrentThread.CurrentUICulture = culture;
        }
Пример #16
0
        public void ParseNumber_WithMexicanLocaleAndMexicanNumberFormat_ParsesNumberCorrectly()
        {
            using (LocalizationExtensions.WithUiCulture(new CultureInfo("es-MX")))
            {
                // Arrange
                var expected = 2.3;

                var opt = new LocalizationOptions();
                var parser = new CliParser<LocalizationOptions>(opt);
                var help = new AutomaticHelpGenerator<LocalizationOptions>();

                // Act
                parser.Parse("-c 2.3 file.txt".Split());

                // Assert
                Assert.AreEqual(expected, opt.MyCounter);
            }
        }
Пример #17
0
 public void GenerateUsage_WithStaticEnum_ListsEnumValues()
 {
     var generator = new AutomaticHelpGenerator <StaticEnumOptions>();
     // TODO
 }
Пример #18
0
        public void ShowHelp_WithMexicanLocale_ShowsSpanishHelpText()
        {
            using (LocalizationExtensions.WithUiCulture(new CultureInfo("es-MX")))
            {
                // Arrange
                var expected = @"Uso: clipr [ -h|--help ] [ --version ] [ --turnonthepower ] [ -s S ] [ -c C ] FILETOADD
            Los Argumentos Posicionales:
             FileToAdd         Archivo para añadir a la cosa.

            Los Argumentos Opcionales:
             --turnonthepower  Establecer el nivel de potencia de 9001.
             -c                Un contador fresco.
             -h, --help        Mostrar este documento de ayuda.
             -s                Fecha de inicio.
             --version         Muestra la versión del ejecutable actual.";

                var opt = new LocalizationOptions();
                var parser = new CliParser<LocalizationOptions>(opt);
                var help = new AutomaticHelpGenerator<LocalizationOptions>();

                // Act
                var actual = help.GetHelp(parser.Config);

                // Assert
                Assert.AreEqual(expected, actual);
            }
        }
Пример #19
0
        public void ShowHelp_WithAmericanLocale_ShowsEnglishHelpText()
        {
            using (LocalizationExtensions.WithUiCulture(new CultureInfo("en-US")))
            {
                // Arrange
                var expected = @"Usage: clipr [ -h|--help ] [ --version ] [ --turnonthepower ] [ -s S ] [ -c C ] FILETOADD
            Positional Arguments:
             FileToAdd         File to add to the thing.

            Optional Arguments:
             --turnonthepower  Set power level to 9001.
             -c                A cool counter.
             -h, --help        Display this help document.
             -s                Start date.
             --version         Displays the version of the current executable.";

                var opt = new LocalizationOptions();
                var parser = new CliParser<LocalizationOptions>(opt);
                var help = new AutomaticHelpGenerator<LocalizationOptions>();  // TODO simplify work required to get help info

                // Act
                var actual = help.GetHelp(parser.Config);

                // Assert
                Assert.AreEqual(expected, actual);
            }
        }