• [C,Java,Python]Command Line Argument: argv, argc, sys.argv, args


    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

  • 相关阅读:
    rails enum用于存储数据
    single-table inheritance 单表继承
    imageable.touch
    jbuilder的set!方法重构接口
    Two Strings Are Anagrams
    java项目导入IntelliJ IDEA
    mac 下载安装 IntelliJ IDEA Tomcat
    Merge k Sorted Lists Leetcode Java
    ruby on rails validates uniqueness
    使用update!导致的更新时候的错误信息不显示 ruby on rails
  • 原文地址:https://www.cnblogs.com/profesor/p/13052215.html
Copyright © 2020-2023  润新知