python、go、shell、c、命令行參數(shù)解析

用的半生不熟的Linux環(huán)境,看看幾種語(yǔ)言的命令行參數(shù)解析~


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)

非正式代碼,忽略語(yǔ)法錯(cuò)誤。。。


2. golang

使用flag包

package main

?import (

? ?"flag"

? ? "fmt"

? ?)


? ?func main(){

? ? ? ? var src string

? ? ? ? var memo string

? ? ? ? var level int

? ? ? ? //定義flag參數(shù)

? ? ? ? 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

參數(shù)解析:

? ? ${0} --> hello.sh

? ? ${1} --> a

? ? ${2} --> c

? ? ${3} --> d

? ? ${4} --> e

? ? ${5} --> f

shell腳本的參數(shù)解析,直接獲取參數(shù)位置使用即可...

notice: 獲取參數(shù)位置時(shí)請(qǐng)加上{}? 當(dāng)參數(shù)的數(shù)量到2位數(shù)時(shí), $10 ---> $1 拼接 0,這樣就會(huì)獲取錯(cuò)誤的參數(shù)啦?。?!


4. C

第一種:

main(int argc,char *argv[ ])

1.argc為整數(shù)

2.argv為指針的指針(可理解為:char **argv or: char *argv[] or: char argv[][] ? ,argv是一個(gè)指針數(shù)組)

 注:main()括號(hào)內(nèi)是固定的寫法。

?#include <stdio.h>

? 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;

?}

執(zhí)行:./test 1 2 3

4

./test

1

2

3

第二種:

頭文件 #include <unistd.h>

定義函數(shù):int getopt(int argc, char * const argv[], const char * optstring);

函數(shù)說明:getopt()用來分析命令行參數(shù)。

1、參數(shù)argc 和argv 是由main()傳遞的參數(shù)個(gè)數(shù)和內(nèi)容。

2、參數(shù)optstring 則代表欲處理的選項(xiàng)字符串。

此函數(shù)會(huì)返回在argv 中下一個(gè)的選項(xiàng)字母,此字母會(huì)對(duì)應(yīng)參數(shù)optstring 中的字母。

如果選項(xiàng)字符串里的字母后接著冒號(hào)":",則表示還有相關(guān)的參數(shù),全域變量optarg 即會(huì)指向此額外參數(shù)。

如果getopt()找不到符合的參數(shù)則會(huì)印出錯(cuò)信息,并將全域變量optopt 設(shè)為"?"字符, 如果不希望getopt()印出錯(cuò)信息,則只要將全域變量opterr 設(shè)為0 即可。

返回值:如果找到符合的參數(shù)則返回此參數(shù)字母, 如果參數(shù)不包含在參數(shù)optstring 的選項(xiàng)字母則返回"?"字符,分析結(jié)束則返回-1.

范例

#include <stdio.h>

#include <unistd.h>

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);

}

執(zhí)行:

$. /getopt -b

option b:b

$. /getopt -c

other option:c

$. /getopt -a

other option :?

$. /getopt -a12345

option a:'12345'


哈哈:平常工作只用python,shell,go和C的命令行參數(shù)解析純屬好奇,也了解下~~~

?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容