📄 commandarg.html
字号:
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<link rel="stylesheet" href="css/stdlayout.css" type="text/css">
<link rel="stylesheet" href="css/print.css" type="text/css">
<meta content="text/html; charset=gb2312" http-equiv="content-type">
<title>命令列引数</title>
</head>
<body>
<h3><a href="http://caterpillar.onlyfun.net/GossipCN/index.html">From
Gossip@caterpillar</a></h3>
<h1><a href="CppGossip.html">C++
Gossip: 命令列引数</a></h1>
程式在执行时,可以附加一些引数以指定执行不同的功能,例如:<br>
<div style="margin-left: 40px;"><span style="font-weight: bold; font-family: Courier New,Courier,monospace;">copy -r ./temp ./tmp</span><br>
</div>
<br>
其中copy是程式名称,而-r、./temp、./tmp都是在程式执行时附加给程式的一些引数,这种程式执行方式在以文字画面为主的程式中相当常见,
称之为“命令列引数”(Command-line argument)。 <br>
<br>
在之前的程式中,您的main()函式在括号中总是空白的,而如要使用命令列引数,可以在括号中如下填写:<br>
<div style="margin-left: 40px;"><span style="font-weight: bold; font-family: Courier New,Courier,monospace;">int main(int argc, char
*argv[]) { </span><br style="font-weight: bold; font-family: Courier New,Courier,monospace;">
<span style="font-weight: bold; font-family: Courier New,Courier,monospace;"> ....
</span><br style="font-weight: bold; font-family: Courier New,Courier,monospace;">
<span style="font-weight: bold; font-family: Courier New,Courier,monospace;">}</span><br>
</div>
<br>
参数argc是一个整数,它记录命令列引数的个数,这个数目包括了程式名称,例如下面的执行指令,argc将记录为5:<br>
<div style="margin-left: 40px;"><span style="font-weight: bold; font-family: Courier New,Courier,monospace;">app hello.c -o -c
project.p</span><br>
</div>
<br>
每个引数之间以空白作区隔,上面的命令包括程式名称共会有五个引数,即app、hello.c、-o、-c、project.p,这五个引数分别储存在使
用指标的字串阵列之中,即char *argv[]中。<br>
<br>
程式在执行后,argc的值一定会大于1,因为至少包括了程式名称,而argv则分别储存了引数的字串,例如上面的例子中,argv[0]是"app"、
argv[1]是"hello.c"、argv[2]是"-o"、argv[3]是"-c"、argv[4]是"project.p"。 <br>
<br>
在命令列引数的使用中,如果您的输入是一个包括空白的字串,则使用" "来包括,例如: <br>
<div style="margin-left: 40px;"><span style="font-weight: bold; font-family: Courier New,Courier,monospace;">show "This is a string!"</span><br>
</div>
<br>
如此argv[0]是程式名称"show",而argv[1]的字串就会是"This is a string!"。 <br>
<br>
接下来介绍一些处理命令列引数的技巧,由于命令列引数是储存在阵列中,取出这些引数的最好方式当然就是使用for回圈,而会通常使用一个前导字元,例如'
-'来指定引数的选项功能,由于argv是个字元阵列,自然的您可以使用switch来比对前导字元,例如:<br>
<div style="margin-left: 40px;"><span style="font-weight: bold; font-family: Courier New,Courier,monospace;">for(int i = 1; i <
argc; i++) { </span><br style="font-weight: bold; font-family: Courier New,Courier,monospace;">
<span style="font-weight: bold; font-family: Courier New,Courier,monospace;"> char
*arg = argv[i]; </span><br style="font-weight: bold; font-family: Courier New,Courier,monospace;">
<br style="font-weight: bold; font-family: Courier New,Courier,monospace;">
<span style="font-weight: bold; font-family: Courier New,Courier,monospace;">
switch(arg[0]) { </span><br style="font-weight: bold; font-family: Courier New,Courier,monospace;">
<span style="font-weight: bold; font-family: Courier New,Courier,monospace;">
case '-': </span><br style="font-weight: bold; font-family: Courier New,Courier,monospace;">
<span style="font-weight: bold; font-family: Courier New,Courier,monospace;">
// 处理参数,设定执行选项,例如-o、-p、-r等等 </span><br style="font-weight: bold; font-family: Courier New,Courier,monospace;">
<span style="font-weight: bold; font-family: Courier New,Courier,monospace;">
default: </span><br style="font-weight: bold; font-family: Courier New,Courier,monospace;">
<span style="font-weight: bold; font-family: Courier New,Courier,monospace;">
// 执行对应功能 </span><br style="font-weight: bold; font-family: Courier New,Courier,monospace;">
<span style="font-weight: bold; font-family: Courier New,Courier,monospace;"> } </span><br style="font-weight: bold; font-family: Courier New,Courier,monospace;">
<span style="font-weight: bold; font-family: Courier New,Courier,monospace;">}</span><br>
</div>
<br>
在判断执行选项的case中,您可以进一步检查第二个字元,例如: <br>
<div style="margin-left: 40px;"><span style="font-weight: bold; font-family: Courier New,Courier,monospace;">switch(arg[1]) { </span><br style="font-weight: bold; font-family: Courier New,Courier,monospace;">
<span style="font-weight: bold; font-family: Courier New,Courier,monospace;"> case
'o': </span><br style="font-weight: bold; font-family: Courier New,Courier,monospace;">
<span style="font-weight: bold; font-family: Courier New,Courier,monospace;">
// 选项o的处理 </span><br style="font-weight: bold; font-family: Courier New,Courier,monospace;">
<span style="font-weight: bold; font-family: Courier New,Courier,monospace;">
break; </span><br style="font-weight: bold; font-family: Courier New,Courier,monospace;">
<span style="font-weight: bold; font-family: Courier New,Courier,monospace;"> case
'p': </span><br style="font-weight: bold; font-family: Courier New,Courier,monospace;">
<span style="font-weight: bold; font-family: Courier New,Courier,monospace;">
// 选项p的处理 </span><br style="font-weight: bold; font-family: Courier New,Courier,monospace;">
<span style="font-weight: bold; font-family: Courier New,Courier,monospace;">
break; </span><br style="font-weight: bold; font-family: Courier New,Courier,monospace;">
<span style="font-weight: bold; font-family: Courier New,Courier,monospace;"> case
'r': </span><br style="font-weight: bold; font-family: Courier New,Courier,monospace;">
<span style="font-weight: bold; font-family: Courier New,Courier,monospace;">
// 选项r的处理 </span><br style="font-weight: bold; font-family: Courier New,Courier,monospace;">
<span style="font-weight: bold; font-family: Courier New,Courier,monospace;">
break; </span><br style="font-weight: bold; font-family: Courier New,Courier,monospace;">
<span style="font-weight: bold; font-family: Courier New,Courier,monospace;">
default: </span><br style="font-weight: bold; font-family: Courier New,Courier,monospace;">
<span style="font-weight: bold; font-family: Courier New,Courier,monospace;">
// 选项错误处理或其它处理 </span><br style="font-weight: bold; font-family: Courier New,Courier,monospace;">
<span style="font-weight: bold; font-family: Courier New,Courier,monospace;">}</span><br>
</div>
<br>
以上是命令列引数处理时的大致流程,当然不同的程式会有不同的处理方式,不过大致上不离以上的架构。<br>
<br>
<br>
</body>
</html>
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -