【发布时间】:2013-04-22 05:15:12
【问题描述】:
我使用ManyConsole 作为控制台应用程序的命令行命令和选项解析器。所有的命令都定义为从ConsoleCommand派生的命令类,然后实现特定的任务。我定义了一个中间基类,ParkPayConsoleCommand 派生自该类:
abstract class ParkPayConsoleCommand: ConsoleCommand
{
protected readonly ParkPayDbContext DbContext = new ParkPayDbContext();
}
然后我所有的命令类都派生自我的基类,并享受内置的DbContext,例如:
class ReadStartsCommand : ParkPayConsoleCommand
{
public ReadStartsCommand()
{
_commandTrace = new TraceSource(CommandName, SourceLevels.All);
IsCommand("read-starts", "Processes imported vehicle entry movements to create new VehiclePresence records with start date-times based on those movements");
HasRequiredOption("b|batchId:", "The Id of the VehicleMovementBatch used to create new vehicle presences.", b => _batchIdOption = b);
}
public override int Run(string[] remainingArguments)
{
// Do the business of the command.
return (int)ExitCodes.Success;
}
}
如上所示,每个命令类都命名和描述自身,并在其构造函数中定义其命令行选项,这是一个 ManyConsole 约定。通常,当我运行诸如上面的ReadStartsCommand 之类的命令时,它只会向控制台写入正在运行的命令,而不是我传递的选项。
然而,当我将ParkPayConsoleCommand.DbContext 设为公开,不受保护时,它会输出字符串
DbContext : ParkPay.Model.Context.ParkPayDbContext
到控制台末尾的运行命令的名称和描述。当DbContext 没有在任何地方定义为命令选项本身时,为什么它会这样做。这可能看起来微不足道,但本质上我问的是一个非常重要的“元问题”,那就是:ManyConsole 是否将其命令类的所有公共属性隐式解释为命令选项,即使它们没有明确声明为命令选项?
【问题讨论】:
标签: c# command-line-interface console-application ndesk.options manyconsole