| | |
| | | using System;
|
| | | using System.Collections.Generic;
|
| | | using System.Diagnostics;
|
| | | using System.IO;
|
| | | using System.Linq;
|
| | | using System.Text;
|
| | | using System.Text.RegularExpressions;
|
| | | using System.IO;
|
| | |
|
| | | namespace QtVsTools.Core.CommandLine
|
| | | {
|
| | |
| | |
|
| | | public class Parser
|
| | | {
|
| | |
|
| | | List<Option> commandLineOptionList = new List<Option>();
|
| | | Dictionary<string, int> nameHash = new Dictionary<string, int>();
|
| | | Dictionary<int, List<string>> optionValuesHash = new Dictionary<int, List<string>>();
|
| | | List<string> optionNames = new List<string>();
|
| | | List<string> positionalArgumentList = new List<string>();
|
| | | List<string> unknownOptionNames = new List<string>();
|
| | | readonly List<Option> commandLineOptionList = new List<Option>();
|
| | | readonly Dictionary<string, int> nameHash = new Dictionary<string, int>();
|
| | | readonly Dictionary<int, List<string>> optionValuesHash = new Dictionary<int, List<string>>();
|
| | | readonly List<string> optionNames = new List<string>();
|
| | | readonly List<string> positionalArgumentList = new List<string>();
|
| | | readonly List<string> unknownOptionNames = new List<string>();
|
| | | bool needsParsing = true;
|
| | |
|
| | | public enum SingleDashWordOptionMode
|
| | |
| | |
|
| | | IEnumerable<string> Aliases(string optionName)
|
| | | {
|
| | | int optionIndex;
|
| | | if (!nameHash.TryGetValue(optionName, out optionIndex)) {
|
| | | if (!nameHash.TryGetValue(optionName, out int optionIndex)) {
|
| | | return new List<string>();
|
| | | }
|
| | | return commandLineOptionList[optionIndex].Names;
|
| | |
| | | IEnumerator<string> argumentEnumerator, ref bool atEnd)
|
| | | {
|
| | | const char assignChar = '=';
|
| | | int optionOffset;
|
| | | if (nameHash.TryGetValue(optionName, out optionOffset)) {
|
| | | if (nameHash.TryGetValue(optionName, out int optionOffset)) {
|
| | | int assignPos = argument.IndexOf(assignChar);
|
| | | bool withValue = !string.IsNullOrEmpty(
|
| | | commandLineOptionList[optionOffset].ValueName);
|
| | |
| | | var optFilePath = macros.ExpandString(argData.Substring(1));
|
| | | string[] additionalArgs = File.ReadAllLines(
|
| | | Path.Combine(workingDir, optFilePath));
|
| | | if (additionalArgs != null) {
|
| | | if (additionalArgs.Length != 0) {
|
| | | var additionalArgsString = string.Join(" ", additionalArgs
|
| | | .Select(x => "\"" + x.Replace("\"", "\\\"") + "\""));
|
| | | arguments.AddRange(TokenizeArgs(additionalArgsString, macros));
|
| | |
| | | if (!RegisterFoundOption(optionName)) {
|
| | | error = true;
|
| | | } else {
|
| | | int optionOffset;
|
| | | Trace.Assert(nameHash.TryGetValue(
|
| | | optionName,
|
| | | out optionOffset));
|
| | | out int optionOffset));
|
| | | bool withValue = !string.IsNullOrEmpty(
|
| | | commandLineOptionList[optionOffset].ValueName);
|
| | | if (withValue) {
|
| | |
| | | if (argument.Length > 2) {
|
| | | string possibleShortOptionStyleName = argument.Substring(1, 1);
|
| | |
|
| | | int shortOptionIdx;
|
| | | if (nameHash.TryGetValue(
|
| | | possibleShortOptionStyleName,
|
| | | out shortOptionIdx)) {
|
| | | out int shortOptionIdx)) {
|
| | | var arg = commandLineOptionList[shortOptionIdx];
|
| | | if ((arg.Flags & Option.Flag.ShortOptionStyle) != 0) {
|
| | | RegisterFoundOption(possibleShortOptionStyleName);
|
| | |
| | | public IEnumerable<string> Values(string optionName)
|
| | | {
|
| | | CheckParsed("Values");
|
| | | int optionOffset;
|
| | | if (nameHash.TryGetValue(optionName, out optionOffset)) {
|
| | | if (nameHash.TryGetValue(optionName, out int optionOffset)) {
|
| | | var values = optionValuesHash[optionOffset];
|
| | | return values;
|
| | | }
|
| | |
| | | public IEnumerable<string> Names
|
| | | {
|
| | | get;
|
| | | private set;
|
| | | }
|
| | |
|
| | | public string ValueName
|
| | |
| | |
|
| | | static class Lexer
|
| | | {
|
| | | static Regex lexer = new Regex(
|
| | | static readonly Regex lexer = new Regex(
|
| | | /* Newline */ @"(\n)" +
|
| | | /* Unquoted */ @"|((?:(?:[^\s\""])|(?:(?<=\\)\""))+)" +
|
| | | /* Quoted */ @"|(?:\""((?:(?:[^\""])|(?:(?<=\\)\""))+)\"")" +
|