public void SimpleLong() { String[] args = new String[] { "--enable-a", "--bfile", "toast", "foo", "bar" }; ICommandLine cl = parser.Parse(options, args); Assert.IsTrue(cl.HasOption("a"), "Confirm -a is set"); Assert.IsTrue(cl.HasOption("b"), "Confirm -b is set"); Assert.IsTrue(cl.GetOptionValue("b").Value.Equals("toast"), "Confirm arg of -b"); Assert.IsTrue(cl.GetOptionValue("bfile").Value.Equals("toast"), "Confirm arg of --bfile"); Assert.IsTrue(cl.Arguments.Count() == 2, "Confirm size of extra args"); }
public static T GetValue <T>(this ICommandLine commandLine, string optionName, T defaultValue) { var optionValue = commandLine.GetOptionValue(optionName); if (optionValue == null) { return(defaultValue); } var value = optionValue.Value; if (String.IsNullOrEmpty(value)) { return(defaultValue); } object returnValue = value; if (!(returnValue is T)) { returnValue = Convert.ChangeType(returnValue, typeof(T), CultureInfo.InvariantCulture); } return((T)returnValue); }
public static IDictionary <string, string> GetOptionProperties(this ICommandLine commandLine, string opt) { IDictionary <string, string> props = new Dictionary <string, string>(); foreach (IOption option in commandLine.Options) { if (opt.Equals(option.Name) || opt.Equals(option.LongName)) { var value = commandLine.GetOptionValue(opt); IList <string> values = value.Values.ToList(); if (values.Count >= 2) { // use the first 2 arguments as the key/value pair props[values[0]] = values[1]; } else if (values.Count == 1) { // no explicit value, handle it as a bool props[values[0]] = "true"; } } } return(props); }
private static void SetOptionsToMember(MemberInfo member, Options options, ICommandLine cmdLine) { var optionName = member.Name; var attrs = member.GetCustomAttributes(typeof(OptionAttribute), false); if (attrs.Length > 0) { var attr = (OptionAttribute)attrs[0]; optionName = attr.Name; if (String.IsNullOrEmpty(optionName)) { optionName = attr.LongName; } if (String.IsNullOrEmpty(optionName)) { optionName = member.Name; } } if (cmdLine.HasOption(optionName)) { var value = cmdLine.GetOptionValue(optionName); } }
public void Ls() { Options options = new Options(); options.AddOption("a", "all", false, "do not hide entries starting with ."); options.AddOption("A", "almost-all", false, "do not list implied . and .."); options.AddOption("b", "escape", false, "print octal escapes for nongraphic characters"); options.AddOption(OptionBuilder.New().WithLongName("block-size") .WithDescription("use SIZE-byte blocks") .HasArgument() .WithArgumentName("SIZE") .Create()); options.AddOption("B", "ignore-backups", false, "do not list implied entried ending with ~"); options.AddOption("c", false, "with -lt: sort by, and show, ctime (time of last modification of file status information) with -l:show ctime and sort by name otherwise: sort by ctime"); options.AddOption("C", false, "list entries by columns"); String[] args = new String[] { "--block-size=10" }; // create the command line parser ICommandLineParser parser = new PosixParser(); ICommandLine line = parser.Parse(options, args); Assert.IsTrue(line.HasOption("block-size")); Assert.AreEqual("10", line.GetOptionValue("block-size").Value); }
public void ArgumentStartingWithHyphen() { String[] args = new String[] { "-b", "-foo" }; ICommandLine cl = parser.Parse(options, args); Assert.AreEqual("-foo", cl.GetOptionValue("b").Value); }
public void NegativeArgument() { String[] args = new String[] { "-b", "-1" }; ICommandLine cl = parser.Parse(options, args); Assert.AreEqual("-1", cl.GetOptionValue("b").Value); }
public void StopAtExpectedArg() { String[] args = new String[] { "-b", "foo" }; ICommandLine cl = parser.Parse(options, args, true); Assert.IsTrue(cl.HasOption('b'), "Confirm -b is set"); Assert.AreEqual("foo", cl.GetOptionValue('b').Value, "Confirm -b is set"); Assert.IsTrue(!cl.Arguments.Any(), "Confirm no extra args: " + cl.Arguments.Count()); }
public void OptionWithArgument() { String[] args = new String[] { "-attr", "p" }; ICommandLine cl = parser.Parse(options, args); Assert.IsFalse(cl.HasOption("p"), "Confirm -p is set"); Assert.IsTrue(cl.HasOption("attr"), "Confirm -attr is set"); Assert.IsTrue(cl.GetOptionValue("attr").Value.Equals("p"), "Confirm arg of -attr"); Assert.IsTrue(!cl.Arguments.Any(), "Confirm all arguments recognized"); }
public void SingleDash() { String[] args = new String[] { "--copt", "-b", "-", "-a", "-" }; ICommandLine cl = parser.Parse(options, args); Assert.IsTrue(cl.HasOption("a"), "Confirm -a is set"); Assert.IsTrue(cl.HasOption("b"), "Confirm -b is set"); Assert.IsTrue(cl.GetOptionValue("b").Value.Equals("-"), "Confirm arg of -b"); Assert.IsTrue(cl.Arguments.Count() == 1, "Confirm 1 extra arg: " + cl.Arguments.Count()); Assert.IsTrue(cl.Arguments.First().Equals("-"), "Confirm value of extra arg: " + cl.Arguments.First()); }
public static object GetParsedOptionValue(this ICommandLine commandLine, string optionName) { var res = commandLine.GetOptionValue(optionName); IOption option = commandLine.ResolveOption(optionName); if (option == null) { return(null); } OptionType type = option.Type; return((res == null) ? null : TypeHandler.CreateValue(res.Value, type)); }
public void LongWithEqualSingleDash() { if (style == ParserStyle.Basic) { return; } String[] args = new String[] { "-foo=bar" }; Options options = new Options(); options.AddOption(OptionBuilder.New().WithLongName("foo").HasArgument().Create('f')); ICommandLine cl = parser.Parse(options, args); Assert.AreEqual("bar", cl.GetOptionValue("foo").Value); }
public void Ant() { Options options = new Options(); options.AddOption("help", false, "print this message"); options.AddOption("projecthelp", false, "print project help information"); options.AddOption("version", false, "print the version information and exit"); options.AddOption("quiet", false, "be extra quiet"); options.AddOption("verbose", false, "be extra verbose"); options.AddOption("debug", false, "print debug information"); options.AddOption("logfile", true, "use given file for log"); options.AddOption("logger", true, "the class which is to perform the logging"); options.AddOption("listener", true, "add an instance of a class as a project listener"); options.AddOption("buildfile", true, "use given buildfile"); options.AddOption(OptionBuilder.New().WithDescription("use value for given property") .HasArguments() .WithValueSeparator() .Create('D')); //, null, true, , false, true ); options.AddOption("find", true, "search for buildfile towards the root of the filesystem and use it"); String[] args = new String[] { "-buildfile", "mybuild.xml", "-Dproperty=value", "-Dproperty1=value1", "-projecthelp" }; // use the GNU parser ICommandLineParser parser = new GnuParser(); ICommandLine line = parser.Parse(options, args); // check multiple values String[] opts = line.GetOptionValues("D"); Assert.AreEqual("property", opts[0]); Assert.AreEqual("value", opts[1]); Assert.AreEqual("property1", opts[2]); Assert.AreEqual("value1", opts[3]); // check single value Assert.AreEqual("mybuild.xml", line.GetOptionValue("buildfile").Value); // check option Assert.IsTrue(line.HasOption("projecthelp")); }
public void MultipleWithLong() { String[] args = new String[] { "--copt", "foobar", "--bfile", "toast" }; ICommandLine cl = parser.Parse(options, args, true); Assert.IsTrue(cl.HasOption("c"), "Confirm -c is set"); Assert.IsTrue(cl.Arguments.Count() == 3, "Confirm 3 extra args: " + cl.Arguments.Count()); cl = parser.Parse(options, cl.Arguments.ToArray()); Assert.IsTrue(!cl.HasOption("c"), "Confirm -c is not set"); Assert.IsTrue(cl.HasOption("b"), "Confirm -b is set"); Assert.IsTrue(cl.GetOptionValue("b").Value.Equals("toast"), "Confirm arg of -b"); Assert.IsTrue(cl.Arguments.Count() == 1, "Confirm 1 extra arg: " + cl.Arguments.Count()); Assert.IsTrue(cl.Arguments.First().Equals("foobar"), "Confirm value of extra arg: " + cl.Arguments.First()); }
public void Groovy() { Options options = new Options(); options.AddOption( OptionBuilder.New().WithLongName("define"). WithDescription("define a system property"). HasArgument(true). WithArgumentName("name=value"). Create('D')); options.AddOption( OptionBuilder.New().HasArgument(false) .WithDescription("usage information") .WithLongName("help") .Create('h')); options.AddOption( OptionBuilder.New().HasArgument(false) .WithDescription("debug mode will print out full stack traces") .WithLongName("debug") .Create('d')); options.AddOption( OptionBuilder.New().HasArgument(false) .WithDescription("display the Groovy and JVM versions") .WithLongName("version") .Create('v')); options.AddOption( OptionBuilder.New().WithArgumentName("charset") .HasArgument() .WithDescription("specify the encoding of the files") .WithLongName("encoding") .Create('c')); options.AddOption( OptionBuilder.New().WithArgumentName("script") .HasArgument() .WithDescription("specify a command line script") .Create('e')); options.AddOption( OptionBuilder.New().WithArgumentName("extension") .HasOptionalArg() .WithDescription("modify files in place; create backup if extension is given (e.g. \'.bak\')") .Create('i')); options.AddOption( OptionBuilder.New().HasArgument(false) .WithDescription("process files line by line using implicit 'line' variable") .Create('n')); options.AddOption( OptionBuilder.New().HasArgument(false) .WithDescription("process files line by line and print result (see also -n)") .Create('p')); options.AddOption( OptionBuilder.New().WithArgumentName("port") .HasOptionalArg() .WithDescription("listen on a port and process inbound lines") .Create('l')); options.AddOption( OptionBuilder.New().WithArgumentName("splitPattern") .HasOptionalArg() .WithDescription("split lines using splitPattern (default '\\s') using implicit 'split' variable") .WithLongName("autosplit") .Create('a')); Parser parser = new PosixParser(); ICommandLine line = parser.Parse(options, new String[] { "-e", "println 'hello'" }, true); Assert.IsTrue(line.HasOption('e')); Assert.AreEqual("println 'hello'", line.GetOptionValue('e').Value); }
public static string GetOptionValue(this ICommandLine commandLine, string optionName, string defaultValue) { var answer = commandLine.GetOptionValue(optionName); return(answer == null ? defaultValue : answer.Value); }
public static IOptionValue GetOptionValue(this ICommandLine commandLine, char optionName) { return(commandLine.GetOptionValue(optionName.ToString(CultureInfo.InvariantCulture))); }
private static void SetOptionsToMember(MemberInfo member, Options options, ICommandLine cmdLine) { var optionName = member.Name; var attrs = member.GetCustomAttributes(typeof (OptionAttribute), false); if (attrs.Length > 0) { var attr = (OptionAttribute) attrs[0]; optionName = attr.Name; if (String.IsNullOrEmpty(optionName)) optionName = attr.LongName; if (String.IsNullOrEmpty(optionName)) optionName = member.Name; } if (cmdLine.HasOption(optionName)) { var value = cmdLine.GetOptionValue(optionName); } }
public static string[] GetOptionValues(this ICommandLine commandLine, string optionName) { var optionValue = commandLine.GetOptionValue(optionName); return(optionValue == null ? null : optionValue.Values.ToArray()); }