Python命令模組argparse學習筆記(一)

來源:互聯網
上載者:User

標籤:運行   衝突   argparse   學習   thread   圖片   重複   fromfile   desc   

首先是關於-h/--help參數的設定

description:位於help資訊前,可用於描述help
prog:描述help資訊中程式的名稱
epilog:位於help資訊後
usage:描述程式的用途
add_help:預設為True,設為False後,就不能顯示help資訊了,執行-h/--help將會報錯
conflict_handler:解決參數衝突
prefix_chars:參數首碼,預設為"-"
fromfile_prefix_chars:設定前置詞字元,放在檔案名稱之前,對檔案裡的參數進行讀取和執行
argument_default:參數的全域預設值

description/epilog
# -*- coding:utf-8 -*-__author__ = "MuT6 Sch01aR"import argparseparser = argparse.ArgumentParser(description="The Help Of Python")parser.add_argument("-t","--thread",help="Thread Run",action="store_true")args = parser.parse_args()if args.thread:    print(args)else:    print("Error")

執行參數-h

運行結果

prog/usage
# -*- coding:utf-8 -*-__author__ = "MuT6 Sch01aR"import argparseparser = argparse.ArgumentParser(description="The Help Of Python",epilog="End Of Help",usage="Python Run Thread")parser.add_argument("-t","--thread",help="Thread Run",action="store_true")args = parser.parse_args()if args.thread:    print(args)else:    print("Error")

 運行結果

預設的為

如果沒有設定prog和usage則顯示預設的,prog和usage都設定的話,顯示usage的

add_help
# -*- coding:utf-8 -*-__author__ = "MuT6 Sch01aR"import argparseparser = argparse.ArgumentParser(description="The Help Of Python",epilog="End Of Help",add_help=False)parser.add_argument("-t","--thread",help="Thread Run",action="store_true")args = parser.parse_args()if args.thread:    print(args)else:    print("Error")

 運行結果

conflict_handler

當有參數重複的時候,程式會報錯,把conflict_handler設定為resovle就可以解決

# -*- coding:utf-8 -*-__author__ = "MuT6 Sch01aR"import argparseparser = argparse.ArgumentParser(description="The Help Of Python",epilog="End Of Help")parser.add_argument("-t","--thread",help="Thread Run",action="store_true")parser.add_argument("-t","--thread",help="Thread Run(2)",action="store_true")args = parser.parse_args()if args.thread:    print(args)else:    print("Error")

 運行,報錯

給argparse.ArgumentParser()添加conflict_handler="resolve"

# -*- coding:utf-8 -*-__author__ = "MuT6 Sch01aR"import argparseparser = argparse.ArgumentParser(description="The Help Of Python",epilog="End Of Help",conflict_handler="resolve")parser.add_argument("-t","--thread",help="Thread Run",action="store_true")parser.add_argument("-t","--thread",help="Thread Run(2)",action="store_true")args = parser.parse_args()if args.thread:    print(args)else:    print("Error")

 運行結果

原先的-t/--thread參數被覆蓋

Python命令模組argparse學習筆記(一)

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.