JAVA
public class BooksTest { public static void main(String[] args) { int i; for (i = 0; i < args.length; i++) { System.out.printf("args[%d] = %s ", i, args[i]); } } }
编译:
javac BooksTest.java && java BooksTest jerry elaine kramer george
输出结果:
args[0] = jerry args[1] = elaine args[2] = kramer args[3] = george
注意不包含文件名,也就是说:args[0]不是BooksTest,这点与python,C不一样
PYTHON
import sys if len(sys.argv) > 1: print(sys.argv) for i in range(len(sys.argv)): print(sys.argv[i]) else: print((sys.argv)) print(sys.argv[0]) # 或者用下面的方式遍历list # for item in sys.argv: # print(item)
运行1:
python3 01_sys_argv.py
输出结果:
['01_sys_argv.py'] 01_sys_argv.py
运行2:
python3 01_sys_argv.py jerry elaine kramer george
输出结果
['01_sys_argv.py', 'jerry', 'elaine', 'kramer', 'george'] 01_sys_argv.py jerry elaine kramer george
C
argc = argument count
argv = argument vector
#include <stdio.h> int main(int argc, const char * argv[]) { int i; for (i = 0; i < argc; i++) { printf("argv[%d] = %s ", i, argv[i]); } return 0; }
编译后运行1:
./a.out
结果:
argv[0] = ./a.out
运行2:
./a.out jerry elaine george kramer
argv[0] = ./a.out argv[1] = jerry argv[2] = elaine argv[3] = george argv[4] = kramer
总结:C有单独的计数器argc,
Pyhton有len(sys.argv)
Java有argv.length