Exemplo n.º 1
0
        static void Main(
            string[] args
            )
        {
            ArgumentParser arguments = new ArgumentParser(
                String.Join(
                    " ",
                    args
                    )
                );

            // Print help if no arguments/invalid arguments/h flag
            if (
                !arguments.AllArgumentsValid() ||
                arguments.HasValidFlag("h") ||
                arguments.NoValidArguments()
                )
            {
                PrintAndExit(
                    HelpText
                    );
            }
            // Need input file output file arguments
            if (
                !(
                    arguments.HasValidFlag("i") &&
                    arguments.HasValidFlag("o")
                    )
                )
            {
                PrintErrorAndExit(
                    InvalidInputsText
                    );
            }
            // Attempt to read in file, error if cannot
            string inputFilePath = arguments.ValueForFlag(
                "i"
                );

            string[] input = ReadFileIfExists(
                inputFilePath
                );
            if (input.Length == 0)
            {
                PrintErrorAndExit(
                    MissingInputFileText
                    );
            }
            // Check whether output file exists
            string outputFilePath = arguments.ValueForFlag(
                "o"
                );

            if (
                File.Exists(
                    outputFilePath
                    ) &&
                !arguments.HasValidFlag(
                    "f"
                    )
                )
            {
                PrintErrorAndExit(
                    OutputFileExistsText
                    );
            }
            // Otherwise, convert and attempt to write out
            MarkdownParser markdownParser = new MarkdownParser(
                input
                );

            if (!markdownParser.Success)
            {
                PrintErrorAndExit(
                    CouldNotConvertToHtmlText
                    );
            }
            try
            {
                File.WriteAllLines(
                    outputFilePath,
                    new string[]
                {
                    markdownParser.ToHtml()
                }
                    );
            } catch (DirectoryNotFoundException) {
                PrintErrorAndExit(
                    OutputFileDirectoryDoesNotExistText
                    );
            } catch (IOException) {
                PrintErrorAndExit(
                    CouldNotWriteOutputFileText
                    );
            } catch (UnauthorizedAccessException) {
                PrintErrorAndExit(
                    CouldNotWriteOutputFileText
                    );
            }
        }