Đoạn mã sau, bằng cách sử dụng subparsers của argparse, thất bại trên Python 3 nhưng chạy như mong đợi trong Python 2. Sau khi so sánh các tài liệu, tôi vẫn không thể nói tại sao.Tại sao mã argparse này hoạt động khác nhau giữa Python 2 và 3?
#!/usr/bin/env python
from __future__ import print_function
from argparse import ArgumentParser
def action(args):
print(args)
if __name__ == '__main__':
std = ArgumentParser(add_help=False)
std.add_argument('standard')
ap = ArgumentParser()
sp = ap.add_subparsers()
cmd = sp.add_parser('subcommand', parents=[std], description='Do subcommand')
cmd.add_argument('arg')
cmd.set_defaults(do=action)
args = ap.parse_args()
args.do(args)
Kết quả từ Python 2.7.6 là:
[email protected]$ python test.py
usage: test.py [-h] {subcommand} ...
test.py: error: too few arguments
Trong Python 3.3.5, tôi nhận được:
[email protected]$ python3 test.py
Traceback (most recent call last):
File "test.py", line 21, in <module>
args.do(args)
AttributeError: 'Namespace' object has no attribute 'do'
Lưu ý rằng nếu bạn sử dụng một subparser: 'args = cmd.parse_args()' nó sẽ hoạt động. – alecxe
Mã này dường như cung cấp cho cùng một lỗi cho tôi trên Python 2.7.4. Nhiều khả năng bạn đang chạy phiên bản tệp sai hoặc một cái gì đó. Nó không nên hoạt động. Hãy thử lại cẩn thận hơn. – bosnjak
Khi tôi gõ mã của bạn tại một thông dịch viên Python 2.7.6, tôi nhận được cùng một lỗi tại dòng 'args = ap.parse_args()'. –