通过命令行打印传递的参数(ARGV)

Contents

输出结果

-a
-b
-c

输入

 -a -b -c

编程代码示例

C

#include <stdio.h>
int main(int argc , char *argv[]){
    int i;
    for(i = 1;i < argc;i++) {
        printf("%s\n", argv[i]);
    }
}

C++

#include <iostream>
using namespace std;
int main(int argc, char* argv[]){
    for(int i=0; i<argc; i++) {
        std::cout << argv[i] << endl;
    }
    return 0;
}

C#

using System;
public class argv {
    public static void Main(string[] args)  {
        foreach (string argv in args) {
            Console.WriteLine(argv);
        }
    }
}

Go

package main
import (
    "fmt"
    "os"
)

func main() {
    for i := range os.Args {
        fmt.Printf("%s\n", os.Args[i])
    }
}

Java

class argv {
    public static void main(String argv[]){
        for ( int i = 0; i < argv.length; i++){
            System.out.println(argv[i]);
        }
    }
}

JavaScript

for (var i=2;i < process.argv.length;i++) {
    console.log(process.argv[i]);
}

PHP

<?php
for ($i=1; $i<count($argv); $i++) {
    print($argv[$i]."\n");
}

Perl

foreach my $argv (@ARGV) {
    print $argv."\n";
}

Python

import sys
for item in sys.argv[1:]:
    print(item)

Ruby

ARGV.each_with_index do |arg, i|
    puts "#{arg}"
end

Rust

use std::env;
fn main() {
    for arg in env::args() {
        println!("{}", arg);
    }
}

Shell

for var in "$@"
do
    echo "$var"
done