我正在创建一个执行自定义文件生成的命令行程序。它允许用户提供带有变量及其值的.txt文件,或者通过一系列提示运行。我想知道是否有一种更有效,更清晰的方式来提示用户完成一系列问题。
/**
* InitializeVariables
* InitializeVariables will set a variable to its associated value
*
* Parameters:
* variableName - the variable we want to set
* variableValue - the value of the variable
*
* Returns:
* True if there were no exceptions detected, False otherwise.
**/
private bool InitializeVariable(string variableName, string variableValue)
{
try
{
switch (variableName)
{
case "CLIENT_ID":
this.CLIENT_ID = variableValue;
return true;
case "PEO_CLIENT":
this.IS_PEO = Convert.ToBoolean(variableValue);
return true;
case "INCLUDE_IC":
this.INCLUDE_IC = Convert.ToBoolean(variableValue);
return true;
case "UNIT_LIST":
this.UNIT_LIST = new List<string>();
var uni_list = variableValue.Split(',');
foreach (var uni in uni_list)
{
this.UNIT_LIST.Add(uni.Trim());
}
return true;
....
default:
// We don't care about extra anything else
return true;
}
}
catch (FormatException fe)
{
Console.WriteLine("*** FORMAT EXCEPTION ***");
Console.WriteLine("The value '"+variableValue+ "' is not valid for the variable '"+variableName+ "'.");
return false;
}
}
和PromptUser方法
/**
* PromptUser
* PromptUser will ask the user a series of questions related to the Employee Generation File
**/
private void PromptUser()
{
while (true)
{
Console.Write("Client ID: ");
//this.CLIENT_ID = Console.ReadLine();
if (!InitializeVariable("CLIENT_ID", Console.ReadLine().Trim())) break;
Console.Write("Is this a PEO Client (True or False)? ");
if (!InitializeVariable("PEO_CLIENT", Console.ReadLine().Trim())) break;
Console.Write("Do you want to include Independent Contractors (True or False)? ");
if (!InitializeVariable("INCLUDE_ICS", Console.ReadLine().Trim())) break;
....
}
}
我觉得这不是一个非常干净的方法来完成这项任务。我还想通过提供特定命令以及使用此实现来为用户提供在任何时候取消提示的选项,这是每行的另一个if语句。
我的一些想法是问题的集合(数组?列表?),我们循环遍历每一个。
答案 0 :(得分:0)
我看到的一个选项(只是概述;因为我对C#并不太重视):
A)要显示给用户的文本
B)一个回调方法,它接受用户提供的字符串,并知道周围调用的哪个字段要更新(以及如何做)。