本文整理汇总了C#中OptionSet.AddSwitch方法的典型用法代码示例。如果您正苦于以下问题:C# OptionSet.AddSwitch方法的具体用法?C# OptionSet.AddSwitch怎么用?C# OptionSet.AddSwitch使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类OptionSet
的用法示例。
在下文中一共展示了OptionSet.AddSwitch方法的4个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: 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;
}
示例2: HelpInfo
/// <summary>
/// Constructor.
/// </summary>
/// <param name="optionSet">An OptionSet.</param>
/// <param name="prototype">The HelpPrototype.</param>
/// <param name="description"></param>
internal HelpInfo(OptionSet optionSet, string prototype, string description = null)
{
var variablePrototype = prototype;
_help = optionSet.AddSwitch(variablePrototype, description);
}
示例3: 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,
};
}
}
示例4: Should_Detect_Switches
public void Should_Detect_Switches()
{
var optionSet = new OptionSet();
var n = optionSet.AddSwitch("n", "");
var a = optionSet.AddSwitch("a", "");
var b = optionSet.AddSwitch("b", "");
var args = "-n -a".Split(' ');
optionSet.Parse(args);
//Actual, expected.
Action<bool, bool> verify = Assert.AreEqual;
verify(n, true);
verify(n.Enabled, true);
verify(a, true);
verify(a.Enabled, true);
verify(b, false);
verify(b.Enabled, false);
}