一个破折号中带有多个可选标志的 argparse [英] argparse with multiple optional flags in one dash

查看:34
本文介绍了一个破折号中带有多个可选标志的 argparse的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

是否可以像在这种标准的 Linux 参数样式中那样将 argparse 中的多个标志与单个破折号相关联?

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

推荐答案

这可以解决问题.最有可能的是,您没有为每个参数都包含简短的形式.

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

这篇关于一个破折号中带有多个可选标志的 argparse的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆