本文整理汇总了C#中OptionSet.AddVariable方法的典型用法代码示例。如果您正苦于以下问题:C# OptionSet.AddVariable方法的具体用法?C# OptionSet.AddVariable怎么用?C# OptionSet.AddVariable使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类OptionSet
的用法示例。
在下文中一共展示了OptionSet.AddVariable方法的6个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: Should_get_simple_variables
public void Should_get_simple_variables()
{
var p = new OptionSet();
var name = p.AddVariable<string>("n", "");
var age = p.AddVariable<int>("a", "");
var myArgs = "-n FindThisString -a:23".Split(' ');
p.Parse(myArgs);
Assert.AreEqual(23, age);
Assert.AreEqual("FindThisString", name);
}
示例2: Should_Get_Simple_Variables
public void Should_Get_Simple_Variables()
{
var optionSet = new OptionSet();
var name = optionSet.AddVariable<string>("n", "");
var age = optionSet.AddVariable<int>("a", "");
/* TODO: The splitskies are clever, but are also error prone. We're assuming
* by this that the string is as it appears at the command line, when this is
* not the case. This is as the text appears after the command line parser is
* through presenting the args to the application. */
var args = "-n FindThisString -a:23".Split(' ');
optionSet.Parse(args);
Assert.AreEqual(23, age);
Assert.AreEqual("FindThisString", name);
}
示例3: Should_throw_exception_of_setting_variable_twice
public void Should_throw_exception_of_setting_variable_twice()
{
var exceptionHappened = false;
var p = new OptionSet();
var n = p.AddVariable<string>("n", "");
var myArgs = "-n:Ryan -n:Jer".Split(' ');
try
{
p.Parse(myArgs);
}
catch (OptionException ex)
{
if (ex.OptionName == "n=")
exceptionHappened = true;
}
Assert.IsTrue(exceptionHappened);
}
示例4: ParseOptions
private static CmdOptions ParseOptions(string[] args)
{
var options = new OptionSet();
var lex = options.AddSwitch("l|lex", "Run the lexer and output the tokens");
var printTree = options.AddSwitch("t|tree", "Print the parse tree");
var validate = options.AddSwitch("v|validate", "Run compilation and produce errors, but don't emit anything");
var outputPath = options.AddVariable<string>("o|output", "Path to write output to");
var filePatterns = options.Parse(args);
var workingDirectory = Directory.GetCurrentDirectory();
var sourcePaths = filePatterns.SelectMany(pattern => Directory.GetFiles(workingDirectory, pattern)).ToArray();
#region Single Action
if(lex && printTree)
{
Console.WriteLine("Can't lex and print tree at same time");
return null;
}
if(lex && validate)
{
Console.WriteLine("Can't lex and validate at same time");
return null;
}
if(printTree && validate)
{
Console.WriteLine("Can't print tree and validate at same time");
return null;
}
#endregion
if(sourcePaths.Length == 0)
Console.WriteLine("Must specify at least one source file");
if(sourcePaths.Any(sourcePath => !HasAdamantExtension(sourcePath)))
Console.WriteLine("All source files must have Adamant Extension (*.adam)");
if(lex)
{
return new CmdOptions()
{
Action = CmdAction.Lex,
SourcePaths = sourcePaths,
OutputPath = outputPath.Value,
};
}
if(printTree)
{
return new CmdOptions()
{
Action = CmdAction.PrintTree,
SourcePaths = sourcePaths,
OutputPath = outputPath.Value,
};
}
if(validate)
{
return new CmdOptions()
{
Action = CmdAction.Validate,
SourcePaths = sourcePaths,
OutputPath = outputPath.Value,
};
}
Console.WriteLine("Must specify an action. One of -l -t -v");
return null;
}
示例5: ParseOptions
private static CmdOptions ParseOptions(string[] args)
{
var options = new OptionSet();
var tokenize = options.AddSwitch("tokenize", "Rather than compiling, run the lexer and output the tokens");
var printTree = options.AddSwitch("tree", "Print the parse tree");
var outputPath = options.AddVariable<string>("o|output", "Path to write output to");
var files = options.Parse(args);
if(files.Count > 1)
{
Console.WriteLine("Can only specify one file");
return null;
}
var filePath = files.FirstOrDefault();
if(tokenize && printTree)
{
Console.WriteLine("Can't tokenize and print tree at same time");
return null;
}
if((tokenize || printTree) && IsAdamantCode(filePath))
{
Console.WriteLine("When tokenizing or printing parse tree, must specify an adamant source file (*.adam)");
return null;
}
if(tokenize)
{
return new CmdOptions()
{
FilePath = filePath,
Action = CmdAction.PrintTree,
OutputPath = outputPath.Value,
};
}
if(printTree)
{
return new CmdOptions()
{
FilePath = filePath,
Action = CmdAction.Tokenize,
OutputPath = outputPath.Value,
};
}
// Some form of compile
if(Directory.Exists(filePath)) // directory path
filePath = Path.Combine(filePath, ProjectFileName);
if(Path.GetExtension(filePath) == ".vson")
{
if(Path.GetFileName(filePath) != ProjectFileName)
{
Console.WriteLine($"project file must be named {ProjectFileName}");
return null;
}
if(outputPath.Value != null)
{
Console.WriteLine($"When compiling {ProjectFileName} file don't specify output path");
return null;
}
return new CmdOptions()
{
FilePath = filePath,
Action = CmdAction.Forge,
};
}
else
{
if(!IsAdamantCode(filePath))
{
Console.WriteLine("When compiling, must specify an adamant source file (*.adam)");
return null;
}
return new CmdOptions()
{
FilePath = filePath,
Action = CmdAction.Compile,
OutputPath = outputPath.Value,
};
}
}
示例6: Should_Not_Throw_Exception_Multiset_Variable
public void Should_Not_Throw_Exception_Multiset_Variable()
{
var optionSet = new OptionSet();
var n = optionSet.AddVariable<string>("n", "");
//TODO: Screaming for an NUnit-test-case-coverage.
var args = "-n:Noah -n:Moses -n:David".Split(' ');
try
{
optionSet.Parse(args);
}
catch (OptionException oex)
{
Assert.Fail("Unexpected exception: {0}", oex);
}
}