<commit_before>﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text.RegularExpressions;

namespace Microsoft.Pc
{
    public class CommandLineOptions
    {
        public string outputDir { get; set; }
        public CompilerOutput compilerOutput { get; set; } = CompilerOutput.C;
        public List<string> inputFileNames { get; set; } = new List<string>();
        public string projectName { get; set; }

        public static bool ParseArguments(IEnumerable<string> args, out CommandLineOptions options)
        {
            var output = new StandardOutput();
            options = new CommandLineOptions();
            
            var commandLineFileNames = new List<string>();
            string targetName = null;
            foreach (string x in args)
            {
                string arg = x;
                string colonArg = null;
                if (arg[0] == '-')
                {
                    int colonIndex = arg.IndexOf(':');
                    if (colonIndex >= 0)
                    {
                        arg = x.Substring(0, colonIndex);
                        colonArg = x.Substring(colonIndex + 1);
                    }

                    switch (arg.Substring(1).ToLowerInvariant())
                    {
                        case "t":
                        case "target":
                            if (colonArg == null)
                            {
                                output.WriteMessage("Missing target name", SeverityKind.Error);
                            }
                            else if (targetName == null)
                            {
                                targetName = colonArg;
                            }
                            else
                            {
                                output.WriteMessage("Only one target must be specified", SeverityKind.Error);
                            }

                            break;

                        case "g":
                        case "generate":
                            switch (colonArg)
                            {
                                case null:
                                    output.WriteMessage("Missing generation argument, expecting generate:[C,P#]", SeverityKind.Error);
                                    return false;
                                case "C":
                                    options.compilerOutput = CompilerOutput.C;
                                    break;
                                case "P#":
                                    options.compilerOutput = CompilerOutput.PSharp;
                                    break;
                                default:
                                    output.WriteMessage($"Unrecognized generate option '{colonArg}', expecting C or P#", SeverityKind.Error);
                                    return false;
                            }

                            break;

                        case "o":
                        case "outputdir":
                            if (colonArg == null)
                            {
                                output.WriteMessage("Must supply path for output directory", SeverityKind.Error);
                                return false;
                            }
                            //check if the path is valid
                            if (!Directory.Exists(Path.GetFullPath(colonArg)))
                            {
                                output.WriteMessage("Must supply path for output directory", SeverityKind.Error);
                                return false;
                            }
                            options.outputDir = Path.GetFullPath(colonArg);
                            break;

                        default:
                            commandLineFileNames.Add(arg);
                            output.WriteMessage($"Unknown Command {arg.Substring(1)}", SeverityKind.Error);
                            return false;
                    }
                }
                else
                {
                    commandLineFileNames.Add(arg);
                }
            }

            // Each command line file name must be a legal P file name
            foreach (string inputFileName in commandLineFileNames)
            {
                if (IsLegalPFile(inputFileName, out string fullPathName))
                {
                    options.inputFileNames.Add(fullPathName);
                }
                else
                {
                    output.WriteMessage($"Illegal P file name {fullPathName} or file {fullPathName} not found", SeverityKind.Error);
                }
            }

            if (options.inputFileNames.Count == 0)
            {
                output.WriteMessage("At least one .p file must be provided", SeverityKind.Error);
                return false;
            }

            string projectName = targetName ?? Path.GetFileNameWithoutExtension(options.inputFileNames[0]);
            if (!IsLegalUnitName(projectName))
            {
                output.WriteMessage($"{projectName} is not a legal protject name", SeverityKind.Error);
                return false;
            }
            options.projectName = projectName;

            if (options.outputDir == null)
            {
                options.outputDir = Directory.GetCurrentDirectory();
            }

            return true;
        }

        private static bool IsLegalUnitName(string unitFileName)
        {
            return Regex.IsMatch(unitFileName, "^[A-Za-z_][A-Za-z_0-9]*$");
        }

        private static bool IsLegalPFile(string fileName, out string fullPathName)
        {
            fullPathName = null;
            if (fileName.Length <= 2 || !fileName.EndsWith(".p"))
            {
                return false;
            }

            fullPathName = Path.GetFullPath(fileName);
            if (IsFileSystemCaseInsensitive)
            {
                fullPathName = fullPathName.ToLowerInvariant();
            }

            return true;
        }
<commit_msg>check that the passed file exits<commit_after>﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text.RegularExpressions;

namespace Microsoft.Pc
{
    public class CommandLineOptions
    {
        public string outputDir { get; set; }
        public CompilerOutput compilerOutput { get; set; } = CompilerOutput.C;
        public List<string> inputFileNames { get; set; } = new List<string>();
        public string projectName { get; set; }

        public static bool ParseArguments(IEnumerable<string> args, out CommandLineOptions options)
        {
            var output = new StandardOutput();
            options = new CommandLineOptions();
            
            var commandLineFileNames = new List<string>();
            string targetName = null;
            foreach (string x in args)
            {
                string arg = x;
                string colonArg = null;
                if (arg[0] == '-')
                {
                    int colonIndex = arg.IndexOf(':');
                    if (colonIndex >= 0)
                    {
                        arg = x.Substring(0, colonIndex);
                        colonArg = x.Substring(colonIndex + 1);
                    }

                    switch (arg.Substring(1).ToLowerInvariant())
                    {
                        case "t":
                        case "target":
                            if (colonArg == null)
                            {
                                output.WriteMessage("Missing target name", SeverityKind.Error);
                            }
                            else if (targetName == null)
                            {
                                targetName = colonArg;
                            }
                            else
                            {
                                output.WriteMessage("Only one target must be specified", SeverityKind.Error);
                            }

                            break;

                        case "g":
                        case "generate":
                            switch (colonArg)
                            {
                                case null:
                                    output.WriteMessage("Missing generation argument, expecting generate:[C,P#]", SeverityKind.Error);
                                    return false;
                                case "C":
                                    options.compilerOutput = CompilerOutput.C;
                                    break;
                                case "P#":
                                    options.compilerOutput = CompilerOutput.PSharp;
                                    break;
                                default:
                                    output.WriteMessage($"Unrecognized generate option '{colonArg}', expecting C or P#", SeverityKind.Error);
                                    return false;
                            }

                            break;

                        case "o":
                        case "outputdir":
                            if (colonArg == null)
                            {
                                output.WriteMessage("Must supply path for output directory", SeverityKind.Error);
                                return false;
                            }
                            //check if the path is valid
                            if (!Directory.Exists(Path.GetFullPath(colonArg)))
                            {
                                output.WriteMessage("Must supply path for output directory", SeverityKind.Error);
                                return false;
                            }
                            options.outputDir = Path.GetFullPath(colonArg);
                            break;

                        default:
                            commandLineFileNames.Add(arg);
                            output.WriteMessage($"Unknown Command {arg.Substring(1)}", SeverityKind.Error);
                            return false;
                    }
                }
                else
                {
                    commandLineFileNames.Add(arg);
                }
            }

            // Each command line file name must be a legal P file name
            foreach (string inputFileName in commandLineFileNames)
            {
                if (IsLegalPFile(inputFileName, out string fullPathName))
                {
                    options.inputFileNames.Add(fullPathName);
                }
                else
                {
                    output.WriteMessage($"Illegal P file name {fullPathName} or file {fullPathName} not found", SeverityKind.Error);
                }
            }

            if (options.inputFileNames.Count == 0)
            {
                output.WriteMessage("At least one .p file must be provided", SeverityKind.Error);
                return false;
            }

            string projectName = targetName ?? Path.GetFileNameWithoutExtension(options.inputFileNames[0]);
            if (!IsLegalUnitName(projectName))
            {
                output.WriteMessage($"{projectName} is not a legal protject name", SeverityKind.Error);
                return false;
            }
            options.projectName = projectName;

            if (options.outputDir == null)
            {
                options.outputDir = Directory.GetCurrentDirectory();
            }

            return true;
        }

        private static bool IsLegalUnitName(string unitFileName)
        {
            return Regex.IsMatch(unitFileName, "^[A-Za-z_][A-Za-z_0-9]*$");
        }

        private static bool IsLegalPFile(string fileName, out string fullPathName)
        {
            fullPathName = null;
            if (fileName.Length <= 2 || !fileName.EndsWith(".p") || !File.Exists(Path.GetFullPath(fileName)))
            {
                return false;
            }

            fullPathName = Path.GetFullPath(fileName);
            if (IsFileSystemCaseInsensitive)
            {
                fullPathName = fullPathName.ToLowerInvariant();
            }

            return true;
        }

{'fraction_non_alphanumeric': 0.07473539629958795, 'fraction_numerical': 0.0017774905065847944, 'mean_word_length': 0.9798464491362764, 'pattern_counts': {'":': 16, '<': 13, '<?xml version=': 0, '>': 13, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 16, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4656727', 'n_tokens_mistral': 2723, 'n_tokens_neox': 2554, 'n_words': 614}