Exemple #1
0
        public static void InlineGenerate(InlineGenerateOptions options, IConsoleWriter console)
        {
            var cliDefinition = new CliDefinitionList()
            {
                Lines = new List <string>()
                {
                    "using CommandLine;",
                    "using System;",
                    "using System.Collections.Generic;"
                },
                Definitions = new List <CliDefinition>()
                {
                    new CliDefinition()
                    {
                        Namespace  = options.Namespace ?? "CommandLineOptions",
                        ClassName  = options.Class ?? "CommandLineOptions",
                        Definition = options.Definition
                    }
                }
            };

            try
            {
                var definition       = cliDefinition.Definitions.First();
                var parsedDefinition = ParsedDefinition.Parse(definition);
                definition.Items = parsedDefinition.ParsedItems.Select(it => it.ToDefinitionItem()).ToList();
            }
            catch { }
            WriteFile(cliDefinition, "inlineCli.json");
            Generate(cliDefinition, options.OutputPath, options.OverwriteFiles, options.Verbose, console);
        }
Exemple #2
0
        public void Parse()
        {
            var def = new CliDefinitionList()
            {
                Definitions = new List <CliDefinition>()
                {
                    new CliDefinition()
                    {
                        Definition = "verb -b",
                        Items      = new List <CliDefinitionItem>()
                        {
                            new CliDefinitionItem()
                            {
                                Verb = "verb"
                            },
                            new CliDefinitionItem()
                            {
                                Option = "b"
                            }
                        }
                    }
                }
            };
            var consoleWriter = new TestConsoleWriter();
            var result        = ParsedDefinitionList.Parse(def, consoleWriter);

            Assert.NotNull(result);
        }
Exemple #3
0
        static void WriteFile(CliDefinitionList cliDefinitionSet, string fileName)
        {
            var json = JsonConvert.SerializeObject(cliDefinitionSet, new JsonSerializerSettings()
            {
                NullValueHandling = NullValueHandling.Ignore,
                Formatting        = Formatting.Indented,
                ContractResolver  = new CamelCasePropertyNamesContractResolver()
            });

            File.WriteAllText(fileName, json);
        }
Exemple #4
0
        public static void Generate(
            CliDefinitionList cliDefinition,
            string outputPath,
            bool overwriteFiles,
            bool verbose,
            IConsoleWriter console)
        {
            ParsedDefinitionList parsedDefinitionSet;

            try
            {
                parsedDefinitionSet = ParsedDefinitionList.Parse(cliDefinition, console);
            }
            catch (Exception exc)
            {
                console.WriteError($"ERROR: Could not parse the definition.");
                console.WriteError(exc.Message);
                return;
            }

            try
            {
                var generator = new CliGenerator();
                generator.Generate(parsedDefinitionSet,
                                   new GeneratorSettings()
                {
                    OverwriteFiles = overwriteFiles,
                    Verbose        = verbose,
                    OutputPath     = outputPath
                },
                                   console);
            }
            catch (Exception exc)
            {
                console.WriteError($"ERROR: Could not generate command line classes.");
                console.WriteError(exc.Message);
                return;
            }
        }
Exemple #5
0
        public static CliDefinitionList Create()
        {
            var definition = new CliDefinitionList()
            {
                Description = "Sample command line definition",
                Namespace   = "Sample.CommandLine",
                Lines       = new List <string>()
                {
                    "using System;"
                },
                Definitions = new List <CliDefinition>()
                {
                    new CliDefinition()
                    {
                        Definition = "[optional_verb] <value> [<optional_value>]",
                        Namespace  = "Sample.CommandLine.Verbs",
                        ClassName  = "OptionalVerbCommandLine",
                        Items      = new List <CliDefinitionItem>()
                        {
                            new CliDefinitionItem()
                            {
                                Verb     = "optional_verb",
                                HelpText = "Optional verb."
                            },
                            new CliDefinitionItem()
                            {
                                ValueName = "value",
                                HelpText  = "Some value that must be present"
                            },
                            new CliDefinitionItem()
                            {
                                ValueName = "optional_value",
                                HelpText  = "Some value that may be omitted. There can be at most one optional value and it must be the last value"
                            }
                        }
                    },
                    new CliDefinition()
                    {
                        Definition = "verb1|verb2 <value> [<optional_value]",
                        Namespace  = "Sample.CommandLine.Verbs",
                        ClassName  = "Verb1_CommandLine|Verb2_CommandLine",
                        Items      = new List <CliDefinitionItem>()
                        {
                            new CliDefinitionItem()
                            {
                                Verb     = "verb1",
                                HelpText = "Do some action",
                            },
                            new CliDefinitionItem()
                            {
                                Verb     = "verb2",
                                HelpText = "Do some other action",
                            },
                            new CliDefinitionItem()
                            {
                                ValueName = "value",
                                HelpText  = "Simple string parameter",
                            },
                            new CliDefinitionItem()
                            {
                                ValueName = "optional_value",
                                HelpText  = "Optional string parameter",
                            }
                        }
                    },
                    new CliDefinition()
                    {
                        Definition = "--a_switch|-a --b_option|-b <parameter> [--c_optional_option|-c <parameter>]",
                        ClassName  = "CommandLine",
                        Items      = new List <CliDefinitionItem>()
                        {
                            new CliDefinitionItem()
                            {
                                Option   = "a_switch|a",
                                HelpText = "Command line switch. True if present."
                            },
                            new CliDefinitionItem()
                            {
                                Option       = "b_option|b",
                                HelpText     = "Mandatory named option",
                                PropertyName = "OptionB"
                            },
                            new CliDefinitionItem()
                            {
                                Option   = "c_optional_option|c",
                                HelpText = "Named option that could be missing."
                            }
                        }
                    }
                }
            };

            return(definition);
        }
Exemple #6
0
        public static CliDefinitionList Create()
        {
            var definition = new CliDefinitionList()
            {
                Namespace   = "LoxSmoke.Cli.CommandLineOptions",
                Description = "Command line options classes.",
                Lines       = new List <string>()
                {
                    "using CommandLine;",
                    "using System;",
                    "using System.Collections.Generic;",
                    "using System.Text;"
                },
                Definitions = new List <CliDefinition>()
                {
                    new CliDefinition()
                    {
                        ClassName  = "SampleOptions",
                        Definition = "sample [<file_name>] [--force|-f] [--name|-n <sample_name>]",
                        Items      = new List <CliDefinitionItem>()
                        {
                            new CliDefinitionItem()
                            {
                                Verb     = "sample",
                                HelpText = "Generate the sample command line definition file."
                            },
                            new CliDefinitionItem()
                            {
                                ValueName = "file_name",
                                HelpText  = "Optional command line definition sample file name."
                            },
                            new CliDefinitionItem()
                            {
                                Option       = "force|f",
                                HelpText     = "Overwrite the sample definition file if file with the same name already exists",
                                PropertyName = "OverwriteSampleFile"
                            },
                            new CliDefinitionItem()
                            {
                                Option   = "name|n",
                                HelpText = "The name of the sample. Available names: default, cli"
                            }
                        }
                    },
                    new CliDefinition()
                    {
                        ClassName  = "InlineGenerateOptions",
                        Definition = "inline <definition> [<output>] [--namespace|-n <namespace>] [--class|-c <class>] [--force|-f] [--verbose|-v]",
                        Items      = new List <CliDefinitionItem>()
                        {
                            new CliDefinitionItem()
                            {
                                Verb     = "inline",
                                HelpText = "Generate command line options class without using definition file."
                            },
                            new CliDefinitionItem()
                            {
                                ValueName = "definition",
                                HelpText  = "Command line definition string."
                            },
                            new CliDefinitionItem()
                            {
                                ValueName    = "output",
                                PropertyName = "OutputPath",
                                HelpText     = "Output path for generated file."
                            },
                            new CliDefinitionItem()
                            {
                                Option   = "namespace|n",
                                HelpText = "The namespace of the command line options class."
                            },
                            new CliDefinitionItem()
                            {
                                Option   = "class|c",
                                HelpText = "The name of the command line options class."
                            },
                            new CliDefinitionItem()
                            {
                                Option       = "force|f",
                                HelpText     = "Overwrite already existing class files.",
                                PropertyName = "OverwriteFiles"
                            },
                            new CliDefinitionItem()
                            {
                                Option   = "verbose|v",
                                HelpText = "Show detailed info when generating code."
                            }
                        }
                    },
                    new CliDefinition()
                    {
                        ClassName  = "GenerateOptions",
                        Definition = "generate <file> [<output>] [--force|-f] [--verbose|-v]",
                        Items      = new List <CliDefinitionItem>()
                        {
                            new CliDefinitionItem()
                            {
                                Verb     = "generate",
                                HelpText = "Generate command line options classes from definition file"
                            },
                            new CliDefinitionItem()
                            {
                                ValueName = "file",
                                HelpText  = "The input file containing command line definition."
                            },
                            new CliDefinitionItem()
                            {
                                ValueName    = "output",
                                PropertyName = "OutputPath",
                                HelpText     = "Output path for generated file(s)."
                            },
                            new CliDefinitionItem()
                            {
                                Option       = "force|f",
                                HelpText     = "Overwrite already existing class files.",
                                PropertyName = "OverwriteFiles"
                            },
                            new CliDefinitionItem()
                            {
                                Option   = "verbose|v",
                                HelpText = "Show detailed info when generating code."
                            }
                        }
                    }
                }
            };

            return(definition);
        }
Exemple #7
0
        public static ParsedDefinitionList Parse(CliDefinitionList definitionSet, IConsoleWriter console)
        {
            var parsedDefinitionSet = new ParsedDefinitionList()
            {
                Description = definitionSet.Description,
                Namespace   = definitionSet.Namespace ?? "CommandLine.Options",
                ClassName   = definitionSet.ClassName ?? "CliProgram",
                Lines       = definitionSet.Lines
            };
            var index = 0;

            foreach (var definition in definitionSet.Definitions)
            {
                if (definition.Items == null)
                {
                    definition.Items = new List <CliDefinitionItem>();
                }
                index++;
                definition.Items.ForEach(item => item.Normalize());
                var parsedDefinition = ParsedDefinition.Parse(definition);
                parsedDefinitionSet.Definitions.Add(parsedDefinition);
                foreach (var item in definition.Items)
                {
                    var parsedItem = parsedDefinition.ParsedItems.FirstOrDefault(i => i.SameItem(item));
                    if (parsedItem == null)
                    {
                        Warning(console, definition, index, item.NameString, "Unused item description.");
                        continue;
                    }
                    parsedItem.DefinitionItem = item;
                }
                foreach (var parsedItem in parsedDefinition.ParsedItems.Where(ci => ci.DefinitionItem == null))
                {
                    Warning(console, definition, index, parsedItem.ToString(), "Missing item description");
                }
                foreach (var parsedItemGroup in parsedDefinition.ParsedItems.GroupBy(it => it.ToString()).Where(g => g.Count() > 1))
                {
                    Warning(console, definition, index, parsedItemGroup.First().ToString(), "Item in command line is defined more than once");
                }
                foreach (var itemGroup in definition.Items.GroupBy(it => it.NameString).Where(g => g.Count() > 1))
                {
                    Warning(console, definition, index, itemGroup.First().NameString, "Item description defined more than once");
                }
            }
            if (definitionSet.Definitions.Count > 1)
            {
                // Check if more than one command line with no verbs exists.
                if (definitionSet.Definitions.Any(it => !it.Items.Any(x => !string.IsNullOrEmpty(x.Verb))))
                {
                    throw new ParseError("There should be either only one definition or all definitions should contain verbs.");
                }
                // Check if different definitions use unique verbs
                var verbs = new HashSet <string>();
                foreach (var definition in definitionSet.Definitions)
                {
                    foreach (var def in definition.Items.Where(it => !string.IsNullOrEmpty(it.Verb)))
                    {
                        if (verbs.Contains(def.Verb))
                        {
                            throw new ParseError($"Verb \'{def.Verb}\' exists in more than one definition. Same verbs cannot be used for different definitions.");
                        }
                    }
                }
            }
            return(parsedDefinitionSet);
        }