2012-08-17 38 views
7

Tôi đang sử dụng mô-đun argparse trong Python để phân tích các tham số được nhập trong giao diện dòng lệnh. Tôi có gọi add_argument sau để một đối tượng subparser:Python argparse: metavar và action = store_true cùng nhau

submit_parser.add_argument('-pv','--provision',metavar='PROVISION', dest='PROVISION', 
           help='provision system', 
           action='store_true', default=False, required=False) 

tôi nhận được lỗi này:

Traceback (most recent call last): 
    File "./scripts/tp4", line 94, in <module> 
    main() 
    File "./scripts/tp4", line 74, in main 
    modloader.loadModules(sub_parsers) 
    File "/usr/lib/python2.6/site-packages/tp4/cli/Moduleloader.py", line 66, in loadModules 
    registered_modules[module_name].setSubparserArgs(module_sub_parser) 
    File "/usr/lib/python2.6/site-packages/tp4/cli/modules/AutotestModule.py", line 135, in setSubparserArgs 
    action='store_true', default=False, required=False) 
    File "/usr/share/tp4/cli/zip/argparse.zip/argparse.py", line 1302, in add_argument 
    TypeError: __init__() got an unexpected keyword argument 'metavar' 

Nếu tôi xóa tác vụ hoặc metavar thông số, nó hoạt động. Tại sao cả hai không thể ở bên nhau? Không có gì về hạn chế này trong tài liệu argparse tại http://docs.python.org/dev/library/argparse.html.

Cảm ơn trước sự giúp đỡ nào

+0

phiên bản python .... là điều quan trọng cần kiểm tra .... – avasal

+0

Không phải bạn đã hỏi, nhưng nếu 'action' là' 'store_true'', giá trị mặc định đã là 'False' (và nếu hành động là ''store_false'' giá trị mặc định là' True'). Nó khá thuận tiện, mặc dù khác với 'optparse' (mặc định là' None' trong cả hai trường hợp). – torek

Trả lời

13

Một metavar chỉ có ý nghĩa cho các đối số vị trí (nghĩ tên tập tin ở phần cuối của dòng lệnh) hoặc khi một cuộc tranh cãi có lý lẽ của riêng nó (như --input-files foo.txt bar.txt).

Đối số --provision của bạn là cờ vì bạn đặt action thành store_true. Nó không có bất kỳ đối số nào (tức là, nargs không được đặt). Như vậy, nó không có ý nghĩa để có một metavar.

Từ the argparse documentation:

When ArgumentParser generates help messages, it need some way to refer to each expected argument. By default, ArgumentParser objects use the dest value as the “name” of each object. By default, for positional argument actions, the dest value is used directly, and for optional argument actions, the dest value is uppercased. So, a single positional argument with dest='bar' will be referred to as bar. A single optional argument --foo that should be followed by a single command-line argument will be referred to as FOO.

+3

Cảm ơn bạn đã giải thích. Tuy nhiên, thông tin này không rõ ràng trong tài liệu argparse. –

+3

Đối với tài liệu python thưa thớt, tôi luôn nhận được câu trả lời từ trang web PYMOTW, http://pymotw.com/2/argparse/ – qneill

+2

Tôi đã sử dụng 'metavar =' khi những gì tôi thực sự muốn là 'dest ='. – aldo

-2

Để trích xuất một câu trả lời từ @pwc, bạn cần phải sử dụng dest thay vì metavar.