argparse with multiple optional flags in one dash

懵懂的女人 提交于 2020-12-31 05:58:25

问题


Is it possible to associate multiple flags with a single dash in argparse as in this standard Linux argument style?

 tar -xvf some_filename.tar

回答1:


This will do the trick. Most likely, you didn't include the short form for each argument.

import argparse

parser = argparse.ArgumentParser(description='... saves many files together...')
parser.add_argument('--extract', '-x',
                    action='store_true',
                    help='extract files from an archive')
parser.add_argument('--verbose', '-v',
                    action='store_true',
                    help='verbosely list files processed')
parser.add_argument('--file', '-f',
                    # dest='file', -- only needed if the long form isn't first
                    help='use archive file or device ARCHIVE')

args = parser.parse_args()


来源:https://stackoverflow.com/questions/24919011/argparse-with-multiple-optional-flags-in-one-dash

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!