python、go、shell、c、命令行参数解析

栏目: Python · 发布时间: 6年前

内容简介:用的半生不熟的Linux环境,看看几种语言的命令行参数解析~python demo.py -u tom -g users -o output.log --time-out 300使用getopt.getopt()模块

用的半生不熟的 Linux 环境,看看几种语言的命令行参数解析~

1. python

python demo.py -u tom -g users -o output.log --time-out 300

使用getopt.getopt()模块

import getopt

import sys

try:

args, opts = getopt.getopt(sys.argv[0], "u:g:o:", "time_out=")

except Exception as ex:

print(str(ex))

for  key,value in opts:

if key == "-u":

username = value

elif key == "-g":

group = value

elif key == "-o":

log_file == value

elif key == "--time-out":

time_out = value

else:

print("Unrecognized parameter %s" % key)

非正式代码,忽略语法错误。。。

2. golang

使用flag包

package main

import (

"flag"

"fmt"

)

func main(){

var src string

var memo string

var level int

//定义flag参数

flag.StringVar(&src, "src", "", "source file")

flag.IntVar(&level, "level", 3, "debug level")

flag.StringVar(&memo, "memo", "", "the memory")

//解析

flag.Parse()

//显示帮助信息

flag.Usage()

fmt.Printf("src=%s, level=%d, memory=%s\n", src, level, memo)

}

go run args.go -level 1 -memo 256 -src source

-level int

debug level

-memo string

the memoey

-src string

source file

src=source, level=1, memory=256

3. shell

sh hello.sh a b c d e f

参数解析:

${0} --> hello.sh

${1} --> a

${2} --> c

${3} --> d

${4} --> e

${5} --> f

shell脚本的参数解析,直接获取参数位置使用即可...

notice: 获取参数位置时请加上{}  当参数的数量到2位数时, $10 ---> $1 拼接 0,这样就会获取错误的参数啦!!!

4. C

第一种:

main(int argc,char *argv[ ])

1.argc为整数

2.argv为指针的指针(可理解为:char **argv or: char *argv[] or: char argv[][]   ,argv是一个指针数组)

注:main()括号内是固定的写法。

#include

int main(int argc, char **args) {

int i = 0;

printf("%d\n", argc);

for (i = 0; i < argc; ++i) {

printf("%s\n", args[i]);

}

return 0;

}

执行:./test 1 2 3

4

./test

1

2

3

第二种:

头文件 #include

定义函数:int getopt(int argc, char * const argv[], const char * optstring);

函数说明:getopt()用来分析命令行参数。

1、参数argc 和argv 是由main()传递的参数个数和内容。

2、参数optstring 则代表欲处理的选项字符串。

此函数会返回在argv 中下一个的选项字母,此字母会对应参数optstring 中的字母。

如果选项字符串里的字母后接着冒号":",则表示还有相关的参数,全域变量optarg 即会指向此额外参数。

如果getopt()找不到符合的参数则会印出错信息,并将全域变量optopt 设为"?"字符, 如果不希望getopt()印出错信息,则只要将全域变量opterr 设为0 即可。

返回值:如果找到符合的参数则返回此参数字母, 如果参数不包含在参数optstring 的选项字母则返回"?"字符,分析结束则返回-1.

范例

#include

#include

int main(int argc, char **argv)

{

int ch;

opterr = 0;

while((ch = getopt(argc, argv, "a:bcde")) != -1)

switch(ch)

{

case 'a':

printf("option a:'%s'\n", optarg);  break;

case 'b':

printf("option b :b\n");  break;

default:

printf("other option :%c\n", ch);

}

printf("optopt +%c\n", optopt);

}

执行:

$. /getopt -b

option b:b

$. /getopt -c

other option:c

$. /getopt -a

other option :?

$. /getopt -a12345

option a:'12345'

哈哈:平常工作只用python,shell,go和C的命令行参数解析纯属好奇,也了解下~~~


以上所述就是小编给大家介绍的《python、go、shell、c、命令行参数解析》,希望对大家有所帮助,如果大家有任何疑问请给我留言,小编会及时回复大家的。在此也非常感谢大家对 码农网 的支持!

查看所有标签

猜你喜欢:

本站部分资源来源于网络,本站转载出于传递更多信息之目的,版权归原作者或者来源机构所有,如转载稿涉及版权问题,请联系我们

闪魂FLASH8网站建设实录

闪魂FLASH8网站建设实录

马谧铤 / 中国林业 / 2006-7 / 46.00元

《闪魂FLASH8网站建设实录》旨在提供以Flash(Flash 8.0为创作工具)为技术核心的整套互动网站的开发思路,其中包括了网站策划、平面设计、程序设计等实用的互联网应用技术。内容包括Photoshop CS2设计,FIash 8创作和ActionScript应用程序开发的操作流程。在技术学习的过程中.大家还将体会到顶级互动网站设计、网站建设的设计流程和思路。《闪魂FLASH8网站建设实录》......一起来看看 《闪魂FLASH8网站建设实录》 这本书的介绍吧!

JS 压缩/解压工具
JS 压缩/解压工具

在线压缩/解压 JS 代码

Base64 编码/解码
Base64 编码/解码

Base64 编码/解码

HEX CMYK 转换工具
HEX CMYK 转换工具

HEX CMYK 互转工具