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、命令行参数解析》,希望对大家有所帮助,如果大家有任何疑问请给我留言,小编会及时回复大家的。在此也非常感谢大家对 码农网 的支持!

查看所有标签

猜你喜欢:

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

Big Java Late Objects

Big Java Late Objects

Horstmann, Cay S. / 2012-2 / 896.00元

The introductory programming course is difficult. Many students fail to succeed or have trouble in the course because they don't understand the material and do not practice programming sufficiently. ......一起来看看 《Big Java Late Objects》 这本书的介绍吧!

JSON 在线解析
JSON 在线解析

在线 JSON 格式化工具

正则表达式在线测试
正则表达式在线测试

正则表达式在线测试

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

HEX CMYK 互转工具