美文网首页
命令行参数获取

命令行参数获取

作者: jacinzhang | 来源:发表于2023-05-25 17:50 被阅读0次

    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)
    
    

    相关文章

      网友评论

          本文标题:命令行参数获取

          本文链接:https://www.haomeiwen.com/subject/nrvgcdtx.html