Contents
	</div>
	
	<h5 id="command"><a href="#command" class="headerlink" title="command"></a>command</h5><figure class="highlight plain"><table><tbody><tr><td class="gutter"><pre><span class="line">1</span><br/><span class="line">2</span><br/><span class="line">3</span><br/><span class="line">4</span><br/><span class="line">5</span><br/><span class="line">6</span><br/><span class="line">7</span><br/><span class="line">8</span><br/><span class="line">9</span><br/><span class="line">10</span><br/><span class="line">11</span><br/><span class="line">12</span><br/><span class="line">13</span><br/><span class="line">14</span><br/><span class="line">15</span><br/><span class="line">16</span><br/><span class="line">17</span><br/><span class="line">18</span><br/><span class="line">19</span><br/><span class="line">20</span><br/><span class="line">21</span><br/><span class="line">22</span><br/><span class="line">23</span><br/><span class="line">24</span><br/><span class="line">25</span><br/><span class="line">26</span><br/><span class="line">27</span><br/><span class="line">28</span><br/><span class="line">29</span><br/><span class="line">30</span><br/><span class="line">31</span><br/><span class="line">32</span><br/><span class="line">33</span><br/><span class="line">34</span><br/><span class="line">35</span><br/><span class="line">36</span><br/><span class="line">37</span><br/><span class="line">38</span><br/><span class="line">39</span><br/><span class="line">40</span><br/><span class="line">41</span><br/><span class="line">42</span><br/><span class="line">43</span><br/><span class="line">44</span><br/><span class="line">45</span><br/><span class="line">46</span><br/><span class="line">47</span><br/><span class="line">48</span><br/><span class="line">49</span><br/><span class="line">50</span><br/><span class="line">51</span><br/><span class="line">52</span><br/><span class="line">53</span><br/><span class="line">54</span><br/><span class="line">55</span><br/><span class="line">56</span><br/><span class="line">57</span><br/><span class="line">58</span><br/><span class="line">59</span><br/><span class="line">60</span><br/><span class="line">61</span><br/><span class="line">62</span><br/><span class="line">63</span><br/><span class="line">64</span><br/><span class="line">65</span><br/><span class="line">66</span><br/><span class="line">67</span><br/><span class="line">68</span><br/><span class="line">69</span><br/><span class="line">70</span><br/><span class="line">71</span><br/><span class="line">72</span><br/><span class="line">73</span><br/><span class="line">74</span><br/><span class="line">75</span><br/><span class="line">76</span><br/><span class="line">77</span><br/><span class="line">78</span><br/><span class="line">79</span><br/><span class="line">80</span><br/><span class="line">81</span><br/><span class="line">82</span><br/><span class="line">83</span><br/><span class="line">84</span><br/><span class="line">85</span><br/><span class="line">86</span><br/><span class="line">87</span><br/><span class="line">88</span><br/><span class="line">89</span><br/><span class="line">90</span><br/><span class="line">91</span><br/><span class="line">92</span><br/><span class="line">93</span><br/><span class="line">94</span><br/><span class="line">95</span><br/><span class="line">96</span><br/><span class="line">97</span><br/><span class="line">98</span><br/><span class="line">99</span><br/><span class="line">100</span><br/><span class="line">101</span><br/></pre></td><td class="code"><pre><span class="line">command_func = {}</span><br/><span class="line"></span><br/><span class="line">function command_func.help()</span><br/><span class="line">	print(&#34;{&#34;i&#34;, &#34;input&#34;, &#34;require&#34;, &#34;GetFirstInputFile&#34;}&#34;)</span><br/><span class="line">	print(&#34;{&#34;i&#34;, &#34;input&#34;, &#34; no&#34;, &#34;GetFirstInputFile&#34;}&#34;)</span><br/><span class="line">	print(&#34;separator is &#34;#&#34;!&#34;)</span><br/><span class="line">end</span><br/><span class="line"></span><br/><span class="line">--将长参数和短参数分开  存到tab中</span><br/><span class="line">function command_func.argToTab(arg)</span><br/><span class="line">	local tabArg = {}</span><br/><span class="line">	local argTabKey = nil</span><br/><span class="line">	for i,v in ipairs(arg) do</span><br/><span class="line">		cFirstChar = string.sub(v, 1, 1)</span><br/><span class="line">		if cFirstChar == &#34;-&#34; then</span><br/><span class="line">			argTabKey = v</span><br/><span class="line">		elseif argTabKey ~= nil then</span><br/><span class="line">			if tabArg[argTabKey] == nil then</span><br/><span class="line">				tabArg[argTabKey] = v</span><br/><span class="line">			else</span><br/><span class="line">				tabArg[argTabKey] =  tabArg[argTabKey] .. &#34;#&#34; ..v</span><br/><span class="line">			end</span><br/><span class="line">		end</span><br/><span class="line">	end</span><br/><span class="line">	return tabArg</span><br/><span class="line">end</span><br/><span class="line"></span><br/><span class="line">function command_func.integrationSameMeanArg(tabArg, commandList)</span><br/><span class="line">	if next(tabArg) == nil or next(commandList) == nil then</span><br/><span class="line">		print(&#34;command is error&#34;)</span><br/><span class="line">		return nil</span><br/><span class="line">	end</span><br/><span class="line">	for commandListKey,commandListValue in ipairs(commandList) do</span><br/><span class="line">		flag = false</span><br/><span class="line">		local allCommand = nil</span><br/><span class="line">		for tabArgKey,tabArgValue in pairs(tabArg) do</span><br/><span class="line">			if tabArgKey == commandListValue[1] or tabArgKey == commandListValue[2] then			</span><br/><span class="line">				allCommand = tabArgValue</span><br/><span class="line">			end</span><br/><span class="line">		end</span><br/><span class="line">		tabArg[commandListValue[1]] = allCommand</span><br/><span class="line">		tabArg[commandListValue[2]] = nil</span><br/><span class="line">	end</span><br/><span class="line">	return tabArg</span><br/><span class="line">end</span><br/><span class="line"></span><br/><span class="line">function command_func.comandAnalysis(arg, commandList)</span><br/><span class="line">	local argMatchcommandList = false</span><br/><span class="line"></span><br/><span class="line">	local tabArg =  command_func.argToTab(arg)</span><br/><span class="line">	if next(tabArg) == nil then</span><br/><span class="line">		print(&#34;command is empty&#34;)</span><br/><span class="line">		return false</span><br/><span class="line">	end</span><br/><span class="line"></span><br/><span class="line">	local commandList = command_func.processCommandListAndCheckLegality(commandList)</span><br/><span class="line">	if commandList == nil then</span><br/><span class="line">		return false</span><br/><span class="line">	end</span><br/><span class="line"></span><br/><span class="line">	tabArg = command_func.integrationSameMeanArg(tabArg, commandList)</span><br/><span class="line">	if tabArg == nil then</span><br/><span class="line">		return false</span><br/><span class="line">	end</span><br/><span class="line"></span><br/><span class="line">	for commandListKey, commandListValue in ipairs(commandList) do</span><br/><span class="line">		argMatchcommandList = false</span><br/><span class="line">		for tabArgKey, tabArgValue in pairs(tabArg) do</span><br/><span class="line">			if tabArgKey == commandListValue[1] or tabArgKey == commandListValue[2] then</span><br/><span class="line">				commandListValue[4](tabArgValue)</span><br/><span class="line">				argMatchcommandList = true</span><br/><span class="line">			end</span><br/><span class="line">		end</span><br/><span class="line"></span><br/><span class="line">		if commandListValue[3] == &#34;require&#34; and argMatchcommandList == false then					</span><br/><span class="line">			print(&#34;command or commandList is not match!&#34;)</span><br/><span class="line">			return false</span><br/><span class="line">		end</span><br/><span class="line">	end</span><br/><span class="line"></span><br/><span class="line">	return true</span><br/><span class="line">end</span><br/><span class="line"></span><br/><span class="line">function  command_func.processCommandListAndCheckLegality(commandList)</span><br/><span class="line">	local commandList1 = commandList</span><br/><span class="line">	if next(commandList1) == nil then</span><br/><span class="line">		print(&#34;commandList is empty&#34;)</span><br/><span class="line">		return nil</span><br/><span class="line">	end</span><br/><span class="line">	for i,v in ipairs(commandList1) do</span><br/><span class="line">		v[1] = &#34;-&#34; .. v[1]</span><br/><span class="line">		v[2] = &#34;--&#34; .. v[2]</span><br/><span class="line">		if v[4] == nil then</span><br/><span class="line">			return nil</span><br/><span class="line">		end</span><br/><span class="line">	end</span><br/><span class="line">	return commandList1</span><br/><span class="line">end</span><br/><span class="line"></span><br/><span class="line"></span><br/><span class="line">return command_func</span><br/></pre></td></tr></tbody></table></figure>
Test
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
require("command")
local function getFirstFile(firstFileName)
print(firstFileName)
end
local function getCompareFile(compareFileName)
print(compareFileName)
end
local function getSecondFile(secondFileName)
print(secondFileName)
end
argList =
{
{"i", "input", "require", getFirstFile},
{"o", "output", "require", getCompareFile},
{"s", "second", "no", getSecondFile},
}

local result = command_func.comandAnalysis(arg, argList)
if result == false then
command_func.help()
end
compare
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
strFristFile = arg[1]
strSecondFile = arg[2]
strCompareFile = arg[3]

--根据命令行参数输入相关的文件命令
if strFristFile == nil or strSecondFile == nil then
print("You do not enter the comparison file A or B / A and B, please check the command line parameters!")
return 0
end

filePointerFrist = io.open(strFristFile, "r")
filePointerSencond = io.open(strSecondFile, "r")
if strCompareFile ~= nil then
compareFilePointer = io.open(strCompareFile, "w")
else
compareFilePointer = io.open("CompareFile.txt", "w")
end

--判断文件是否为空 或者说文件是否打开成功
if filePointerFrist == nil and filePointerSencond == nil then
print("All files are empty!")
return 0
elseif filePointerFrist == nil then
print(strFristFile, "文件指针为nil!")
return 0
elseif filePointerSencond == nil then
print(strSecondFile, "文件指针为nil!")
return 0
end

--读文件并做比较
local strFirstLine = filePointerFrist:read("*l")
local strSecondLine = filePointerSencond:read("*l")
while strFirstLine ~= nil and strSecondLine ~= nil do
if strFirstLine ~= strSecondLine then
compareFilePointer:write(strFristFile, "显示:"", strFirstLine, " " 和", strSecondFile, "显示为:" ", strSecondLine, " " 不同n")
end
strFirstLine = filePointerFrist:read("*l")
strSecondLine = filePointerSencond:read("*l")
end

--文件行数不一样的情况
if strFirstLine == nil and strSecondLine == nil then
print("file is null/ line is equal!")
elseif strFirstLine == nil then
tempLine = strSecondLine;
strSecondLine = filePointerSencond:read("*a")
--Write = tempLine..strSecondLine
strWrite = table.concat({tempLine, strSecondLine})
compareFilePointer:write(strFristFile, "为空,和", strSecondFile, "显示为:" ", strWrite, " " 不同n")
elseif strSecondLine == nil then
tempLine = strFirstLine
strFirstLine = filePointerFrist:read("*a")
--strWrite = tempLine..strFirstLine
strWrite = table.concat({tempLine, strFirstLine})
compareFilePointer:write(strSecondFile, "为空,和", strFristFile, "显示为:"", strWrite, " " 不同n")
end

filePointerFrist:close()
filePointerSencond:close()
compareFilePointer:close()