コード例 #1
0
ファイル: Option.cs プロジェクト: ReVolly/docopt.net
        public static Option Parse(string optionDescription)
        {
            if (optionDescription == null) throw new ArgumentNullException("optionDescription");

            string shortName = null;
            string longName = null;
            var argCount = 0;
            var value = new ValueObject(false);
            var p = new StringPartition(optionDescription, DESC_SEPARATOR);
            var options = p.LeftString;
            var description = p.RightString;
            foreach (var s in options.Split(" \t,=".ToCharArray(), StringSplitOptions.RemoveEmptyEntries))
            {
                if (s.StartsWith("--"))
                    longName = s;
                else if (s.StartsWith("-"))
                {
                    shortName = s;
                }
                else
                {
                    argCount = 1;
                }
            }
            if (argCount > 0)
            {
                var r = new Regex(@"\[default: (.*)\]", RegexOptions.IgnoreCase);
                var m = r.Match(description);
                value = m.Success ? new ValueObject(m.Groups[1].Value) : null;
            }
            return new Option(shortName, longName, argCount, value);
        }
コード例 #2
0
ファイル: Docopt.cs プロジェクト: tirth/docopt.net
        internal static string FormalUsage(string exitUsage)
        {
            var section = new StringPartition(exitUsage, ":").RightString; // drop "usage:"
            var pu      = section.Split(new char[0], StringSplitOptions.RemoveEmptyEntries);
            var join    = new StringBuilder();

            join.Append("( ");
            for (var i = 1; i < pu.Length; i++)
            {
                var s = pu[i];
                if (i > 1)
                {
                    join.Append(" ");
                }
                join.Append(s == pu[0] ? ") | (" : s);
            }
            join.Append(" )");
            return(join.ToString());
        }
コード例 #3
0
        public static Option Parse(string optionDescription)
        {
            if (optionDescription == null)
            {
                throw new ArgumentNullException("optionDescription");
            }

            string shortName   = null;
            string longName    = null;
            var    argCount    = 0;
            var    value       = new ValueObject(false);
            var    p           = new StringPartition(optionDescription, DESC_SEPARATOR);
            var    options     = p.LeftString;
            var    description = p.RightString;

            foreach (var s in options.Split(" \t,=".ToCharArray(), StringSplitOptions.RemoveEmptyEntries))
            {
                if (s.StartsWith("--"))
                {
                    longName = s;
                }
                else if (s.StartsWith("-"))
                {
                    shortName = s;
                }
                else
                {
                    argCount = 1;
                }
            }
            if (argCount > 0)
            {
                var r = new Regex(@"\[default: (.*)\]", RegexOptions.IgnoreCase);
                var m = r.Match(description);
                value = m.Success ? new ValueObject(m.Groups[1].Value) : null;
            }
            return(new Option(shortName, longName, argCount, value));
        }
コード例 #4
0
ファイル: Docopt.cs プロジェクト: tirth/docopt.net
        internal static ICollection <Option> ParseDefaults(string doc)
        {
            var defaults = new List <Option>();

            foreach (var s in ParseSection("options:", doc))
            {
                // FIXME corner case "bla: options: --foo"

                var p           = new StringPartition(s, ":"); // get rid of "options:"
                var optionsText = p.RightString;
                var a           = Regex.Split("\n" + optionsText, @"\r?\n[ \t]*(-\S+?)");
                var split       = new List <string>();
                for (var i = 1; i < a.Length - 1; i += 2)
                {
                    var s1 = a[i];
                    var s2 = a[i + 1];
                    split.Add(s1 + s2);
                }
                var options = split.Where(x => x.StartsWith("-")).Select(x => Option.Parse(x));
                defaults.AddRange(options);
            }
            return(defaults);
        }
コード例 #5
0
        internal static ICollection<Option> ParseDefaults(string doc)
        {
            var defaults = new List<Option>();
            foreach (var s in ParseSection("options:", doc))
            {
                // FIXME corner case "bla: options: --foo"   

                var p = new StringPartition(s, ":"); // get rid of "options:"
                var optionsText = p.RightString;
                var a = Regex.Split("\n" + optionsText, @"\r?\n[ \t]*(-\S+?)");
                var split = new List<string>();
                for (int i = 1; i < a.Length - 1; i += 2)
                {
                    var s1 = a[i];
                    var s2 = a[i + 1];
                    split.Add(s1 + s2);
                }
                var options = split.Where(x => x.StartsWith("-")).Select(x => Option.Parse(x));
                defaults.AddRange(options);
            }
            return defaults;
        }
コード例 #6
0
 private static IEnumerable<Pattern> ParseLong(Tokens tokens, ICollection<Option> options)
 {
     // long ::= '--' chars [ ( ' ' | '=' ) chars ] ;
     var p = new StringPartition(tokens.Move(), "=");
     var longName = p.LeftString;
     Debug.Assert(longName.StartsWith("--"));
     var value = (p.NoSeparatorFound) ? null : new ValueObject(p.RightString);
     var similar = options.Where(o => o.LongName == longName).ToList();
     if (tokens.ThrowsInputError && similar.Count == 0)
     {
         // If not exact match
         similar =
             options.Where(o => !String.IsNullOrEmpty(o.LongName) && o.LongName.StartsWith(longName)).ToList();
     }
     if (similar.Count > 1)
     {
         // Might be simply specified ambiguously 2+ times?
         throw tokens.CreateException(string.Format("{0} is not a unique prefix: {1}?", longName,
                                                    string.Join(", ", similar.Select(o => o.LongName))));
     }
     Option option = null;
     if (similar.Count < 1)
     {
         var argCount = p.Separator == "=" ? 1 : 0;
         option = new Option(null, longName, argCount);
         options.Add(option);
         if (tokens.ThrowsInputError)
             option = new Option(null, longName, argCount, argCount != 0 ? value : new ValueObject(true));
     }
     else
     {
         option = new Option(similar[0].ShortName, similar[0].LongName, similar[0].ArgCount, similar[0].Value);
         if (option.ArgCount == 0)
         {
             if (value != null)
                 throw tokens.CreateException(option.LongName + " must not have an argument");
         }
         else
         {
             if (value == null)
             {
                 if (tokens.Current() == null || tokens.Current() == "--")
                     throw tokens.CreateException(option.LongName + " requires an argument");
                 value = new ValueObject(tokens.Move());
             }
         }
         if (tokens.ThrowsInputError)
             option.Value = value ?? new ValueObject(true);
     }
     return new[] {option};
 }
コード例 #7
0
 internal static string FormalUsage(string exitUsage)
 {
     var section = new StringPartition(exitUsage, ":").RightString; // drop "usage:"
     var pu = section.Split(new char[0], StringSplitOptions.RemoveEmptyEntries);
     var join = new StringBuilder();
     join.Append("( ");
     for (int i = 1; i < pu.Length; i++)
     {
         var s = pu[i];
         if (i > 1) join.Append(" ");
         join.Append((s == pu[0]) ? ") | (" : s);
     }
     join.Append(" )");
     return join.ToString();
 }
コード例 #8
0
ファイル: Docopt.cs プロジェクト: tirth/docopt.net
        private static IEnumerable <Pattern> ParseLong(Tokens tokens, ICollection <Option> options)
        {
            // long ::= '--' chars [ ( ' ' | '=' ) chars ] ;
            var p        = new StringPartition(tokens.Move(), "=");
            var longName = p.LeftString;

            Debug.Assert(longName.StartsWith("--"));
            var value   = (p.NoSeparatorFound) ? null : new ValueObject(p.RightString);
            var similar = options.Where(o => o.LongName == longName).ToList();

            if (tokens.ThrowsInputError && similar.Count == 0)
            {
                // If not exact match
                similar =
                    options.Where(o => !string.IsNullOrEmpty(o.LongName) && o.LongName.StartsWith(longName)).ToList();
            }
            if (similar.Count > 1)
            {
                // Might be simply specified ambiguously 2+ times?
                throw tokens.CreateException($"{longName} is not a unique prefix: {string.Join(", ", similar.Select(o => o.LongName))}?");
            }
            Option option = null;

            if (similar.Count < 1)
            {
                var argCount = p.Separator == "=" ? 1 : 0;
                option = new Option(null, longName, argCount);
                options.Add(option);
                if (tokens.ThrowsInputError)
                {
                    option = new Option(null, longName, argCount, argCount != 0 ? value : new ValueObject(true));
                }
            }
            else
            {
                option = new Option(similar[0].ShortName, similar[0].LongName, similar[0].ArgCount, similar[0].Value);
                if (option.ArgCount == 0)
                {
                    if (value != null)
                    {
                        throw tokens.CreateException(option.LongName + " must not have an argument");
                    }
                }
                else
                {
                    if (value == null)
                    {
                        if (tokens.Current() == null || tokens.Current() == "--")
                        {
                            throw tokens.CreateException(option.LongName + " requires an argument");
                        }
                        value = new ValueObject(tokens.Move());
                    }
                }
                if (tokens.ThrowsInputError)
                {
                    option.Value = value ?? new ValueObject(true);
                }
            }
            return(new[] { option });
        }