命令行参数获取
2023-05-25 本文已影响0人
jacinzhang
c/objc
int main(int argc, const char * argv[]) {
@autoreleasepool {
for(int i = 0; i < argc; i++) {
NSLog(@"参数:%s", argv[i]);
}
}
return 0;
}
swift
// 编译为可执行文件 swiftc cli.swift -o output
// 直接执行 swift cli.swift hello world
import Foundation
let arguments = CommandLine.arguments
// 遍历命令行参数
for (index, argument) in arguments.enumerated() {
print("Argument \(index): \(argument)")
}
shell
#!/bin/bash
echo "$0"
echo "当前目录:$(dirname "$0")"
echo "\$1: $1"
echo "\$2: $2"
for arg in "$@"
do
echo "遍历获取,参数: ${arg}"
done
node
// 遍历命令行参数
for (let i = 0; i < process.argv.length; i++) {
console.log(`Argument ${i}: ${process.argv[i]}`);
}
python
import sys
def main():
# 获取命令行参数
arguments = sys.argv
# 打印命令行参数
for arg in arguments:
print(arg)