本文整理汇总了C#中CommandLine.CommandLineParser.AddOption方法的典型用法代码示例。如果您正苦于以下问题:C# CommandLineParser.AddOption方法的具体用法?C# CommandLineParser.AddOption怎么用?C# CommandLineParser.AddOption使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类CommandLine.CommandLineParser
的用法示例。
在下文中一共展示了CommandLineParser.AddOption方法的10个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: Example3
static void Example3(string[] args)
{
CommandLineParser parser = new CommandLineParser();
int intValue = 0;
CommandLineIntOption intOption = new CommandLineIntOption("number");
intOption.MinValue = 100;
intOption.MaxValue = 200;
intOption.ParameterType = ParameterType.Optional;
intOption.Delegate = x => intValue = x.Value;
parser.AddOption(intOption);
parser.Parse(args);
Console.WriteLine("Value: " + intValue);
}
示例2: GetGnuTimeParser
static CommandLineParser GetGnuTimeParser()
{
CommandLineParser parser = new CommandLineParser();
var format = new CommandLineStringOption("format", "f");
format.Help = "Specify output format, possibly overriding the format specified in the environment variable TIME.";
format.ExpectedValue = "format";
parser.AddOption(format);
var portability = new CommandLineBoolOption("portability", "p");
portability.Help = "Use the portable output format.";
parser.AddOption(portability);
var output = new CommandLineStringOption("output", "o");
output.Help = "Do not send the results to stderr, but overwrite the specified file.";
output.ExpectedValue = "file";
parser.AddOption(output);
var append = new CommandLineBoolOption("append", "a");
output.Help = "(Used together with -o.) Do not overwrite but append.";
parser.AddOption(append);
var verbose = new CommandLineBoolOption("verbose", "v");
verbose.Help = "Give very verbose output about all the program knows about.";
parser.AddOption(verbose);
var help = new CommandLineBoolOption("help");
help.Help = "Print a usage message on standard output and exit successfully.";
parser.AddOption(help);
var version = new CommandLineBoolOption("version", "V");
version.Help = "Print version information on standard output, then exit successfully.";
parser.AddOption(version);
return parser;
}
示例3: Main
static void Main(string[] args)
{
TestGnuTime();
TestValueType();
TestRequiredAndOptionalParameters();
TestExceptions();
TestOptionalParameters();
TestTerminator();
TestParsingException();
TestConfigExceptions();
TestRequiredOption();
//args = new string[] { "--listen", "--port", "80", "--hello=world" };
args = new string[] { "-p", "80", "-l", "naky", "navic" };
//args = new string[] { "-lp80" };
//args = new string[] { "--port=100000" };
//args = new string[] { "--hello=world" };
CommandLineParser parser = new CommandLineParser();
CommandLineIntOption p = new CommandLineIntOption("port", "p");
p.MaxValue = 65535;
p.Delegate = x => Console.WriteLine("Já jsem delegát: " + x.Value);
parser.AddOption(p);
CommandLineBoolOption l = new CommandLineBoolOption(name: "listen", shortName: "l");
parser.AddOption(l);
CommandLineStringOption s = new CommandLineStringOption("hello");
s.AllowedValues.AddRange(new string[] { "abc", "def", "ghi" });
parser.AddOption(s);
List<string> extraParameters;
//try
{
// run the parser
extraParameters = parser.Parse(args);
}
/*catch (ParsingException e)
{
Console.WriteLine(e.Message);
return;
}*/
/*catch (OptionNotFoundException e)
{
Console.WriteLine(e.Message);
return;
}*/
Console.WriteLine("p.Value: " + p.Value);
Console.WriteLine("l.Present: " + l.Present);
Console.WriteLine("s.Value: " + s.Value);
Console.WriteLine("extraParameters: " + string.Join(",", extraParameters));
}
示例4: TestValueType
static void TestValueType()
{
CommandLineParser parser = new CommandLineParser();
CommandLineIntOption intOption = new CommandLineIntOption("int", "i");
parser.AddOption(intOption);
var otherParams = parser.Parse(new string[] { "-i", "10" });
TestAssert(intOption.Value == 10);
TestAssert(intOption.Value.GetType() == typeof(int));
}
示例5: TestTerminator
static void TestTerminator()
{
CommandLineParser parser = new CommandLineParser();
CommandLineStringOption strOption = new CommandLineStringOption("str", "s");
strOption.ParameterType = ParameterType.Optional;
parser.AddOption(strOption);
var otherParams = parser.Parse(new string[] { "-s", "ahoj", "--", "-s" });
Console.WriteLine(strOption.Value);
TestAssert(otherParams.Count == 1);
TestAssert(otherParams[0] == "-s");
}
示例6: TestRequiredOption
static void TestRequiredOption()
{
{
CommandLineParser parser = new CommandLineParser();
try
{
CommandLineStringOption strOption = new CommandLineStringOption("str", "s1");
TestAssert(false);
}
catch (ConfigurationException)
{
TestAssert(true);
}
}
{
CommandLineParser parser = new CommandLineParser();
CommandLineStringOption strOption = new CommandLineStringOption("str", "s");
strOption.Required = true;
parser.AddOption(strOption);
try
{
strOption.ShortName = "s1";
TestAssert(false);
}
catch (ConfigurationException)
{
TestAssert(true);
}
}
{
CommandLineParser parser = new CommandLineParser();
CommandLineStringOption strOption = new CommandLineStringOption("str", "s");
strOption.Required = true;
parser.AddOption(strOption);
try
{
strOption.Name = "";
TestAssert(false);
}
catch (ConfigurationException)
{
TestAssert(true);
}
}
{
CommandLineParser parser = new CommandLineParser();
CommandLineStringOption strOption = new CommandLineStringOption("str", "s");
strOption.Required = true;
parser.AddOption(strOption);
try
{
strOption.ShortName = "";
TestAssert(false);
}
catch (ConfigurationException)
{
TestAssert(true);
}
}
}
示例7: TestRequiredAndOptionalParameters
static void TestRequiredAndOptionalParameters()
{
CommandLineParser p = new CommandLineParser();
var s = new CommandLineStringOption("string", "s");
s.ParameterType = ParameterType.Optional;
p.AddOption(s);
var b = new CommandLineBoolOption("bool", "b");
p.AddOption(b);
var args = new string[] { "--string", "dalsi", "hodnota" };
var extraParameters = p.Parse(args);
TestAssert(s.Value == "dalsi");
TestAssert(b.Present == false);
TestAssert(extraParameters.Count == 1);
TestAssert(extraParameters[0] == "hodnota");
args = new string[] { "--string", "-b", "dalsi", "hodnota" };
extraParameters = p.Parse(args);
TestAssert(s.Value == "-b");
TestAssert(b.Present == false);
TestAssert(extraParameters.Count == 2);
TestAssert(extraParameters[0] == "dalsi");
TestAssert(extraParameters[1] == "hodnota");
args = new string[] { "-b", "--string", "dalsi", "hodnota" };
extraParameters = p.Parse(args);
TestAssert(s.Value == "dalsi");
TestAssert(b.Present == true);
TestAssert(extraParameters.Count == 1);
TestAssert(extraParameters[0] == "hodnota");
}
示例8: TestParsingException
static void TestParsingException()
{
CommandLineParser parser = new CommandLineParser();
CommandLineStringOption strOption = new CommandLineStringOption("str", "s");
strOption.ParameterType = ParameterType.Required;
parser.AddOption(strOption);
try
{
var otherParams = parser.Parse(new string[] { "-s" });
TestAssert(false);
}
catch (ParsingException p)
{
TestAssert(p.Option.Name == "str");
}
}
示例9: TestExceptions
static void TestExceptions()
{
CommandLineParser p = new CommandLineParser();
/*
var s = new CommandLineBoolOption("bool");
try
{
s.ParameterType = ParameterType.Optional;
TestAssert(false);
}
catch (Exception e)
{
TestAssert(e.GetType() == typeof(ParsingException));
}
try
{
s.ParameterType = ParameterType.Required;
TestAssert(false);
}
catch (Exception e)
{
TestAssert(e.GetType() == typeof(ParsingException));
}
try
{
s.ParameterType = (ParameterType)10;
TestAssert(false);
}
catch (Exception e)
{
TestAssert(e.GetType() == typeof(ParsingException));
}
try
{
s.ParameterType = (ParameterType)10;
TestAssert(false);
}
catch (Exception e)
{
TestAssert(e.GetType() == typeof(ParsingException));
}
p.AddOption(s);
try
{
p.AddOption(s);
TestAssert(false);
}
catch (Exception e)
{
TestAssert(e.GetType() == typeof(ParsingException));
}
*/
{
p = new CommandLineParser();
CommandLineBoolOption b1 = new CommandLineBoolOption("bool1", "q");
CommandLineBoolOption b2 = new CommandLineBoolOption("bool2", "w");
p.AddOption(b1);
p.AddOption(b2);
try
{
b2.Name = "bool1"; // exception
//TestAssert(false);
}
catch (Exception e)
{
TestAssert(e.GetType() == typeof(ConfigurationException));
}
try
{
b2.ShortName = "q"; // exception
//TestAssert(false);
}
catch (Exception e)
{
TestAssert(e.GetType() == typeof(ConfigurationException));
}
b2.Name = "bool3";
p.Parse(new string[] { "--bool3" });
TestAssert(b1.Present == false);
TestAssert(b2.Present == true);
}
{
p = new CommandLineParser();
CommandLineBoolOption b1 = new CommandLineBoolOption("bool1", "q");
CommandLineBoolOption b2 = new CommandLineBoolOption("bool2", "q");
p.AddOption(b1);
try
//.........这里部分代码省略.........
示例10: Time
public Time(string[] args)
{
CommandLineParser parser = new CommandLineParser();
var format = new CommandLineStringOption("format", "f");
format.Help = "Specify output format, possibly overriding the format specified in the environment variable TIME.";
format.ExpectedValue = "format";
parser.AddOption(format);
format.ShortName = "X";
var portability = new CommandLineBoolOption("portability", "p");
portability.Help = "Use the portable output format.";
//parser.AddOption(portability);
var output = new CommandLineStringOption("output", "o");
output.Help = "Do not send the results to stderr, but overwrite the specified file.";
output.ExpectedValue = "file";
output.Required = true;
//parser.AddOption(output);
var append = new CommandLineBoolOption("append", "a");
append.Help = "(Used together with -o.) Do not overwrite but append.";
//parser.AddOption(append);
var verbose = new CommandLineBoolOption("verbose", "v");
verbose.Help = "Give very verbose output about all the program knows about.";
verbose.Required = true;
//verbose.ParameterType = ParameterType.Required;
//parser.AddOption(verbose);
var help = new CommandLineBoolOption("help");
help.Help = "Print a usage message on standard output and exit successfully.";
//parser.AddOption(help);
var version = new CommandLineBoolOption("version", "V");
version.Help = "Print version information on standard output, then exit successfully.";
//parser.AddOption(version);
List<string> extraParameters;
try
{
extraParameters = parser.Parse(args);
}
catch (ParsingException ex)
{
if (ex.Option != null)
{
Console.WriteLine("An error occurred in parameter " + ex.Option.Name);
}
Console.WriteLine("Message: " + ex.Message);
return;
}
if ((args.Length == 0) || (help.Present))
{
parser.PrintHelp();
return;
}
Console.WriteLine("Format: " + format.Value);
Console.WriteLine("Verbose: " + verbose.Present);
Console.WriteLine("Output: " + output.Value);
}