argparse

Don't show long options twice in print_help() from argparse

╄→尐↘猪︶ㄣ 提交于 2019-11-30 11:41:29
I have the following code: parser = argparse.ArgumentParser(description='Postfix Queue Administration Tool', prog='pqa', usage='%(prog)s [-h] [-v,--version]') parser.add_argument('-l', '--list', action='store_true', help='Shows full overview of all queues') parser.add_argument('-q', '--queue', action='store', metavar='<queue>', dest='queue', help='Show information for <queue>') parser.add_argument('-d', '--domain', action='store', metavar='<domain>', dest='domain', help='Show information about a specific <domain>') parser.add_argument('-v', '--version', action='version', version='%(prog)s 0.1'

Sort argparse help alphabetically

吃可爱长大的小学妹 提交于 2019-11-30 11:06:20
I am using Python's (2.7) argparse facility and would like to automatically sort the help it produces alphabetically by option. By default help entries are sorted in the order they are added*, as in: p = argparse.ArgumentParser(description='Load duration curves and other plots') p.add_argument('--first', '-f', type=int, default=1, help='First Hour') p.add_argument('--dur', '-d', type=int, default=-1, help='Duration in Hours. Use -1 for all') p.add_argument('--title', '-t', help='Plot Title (for all plots), default=file name') p.add_argument('--interp', '-i', action="store_true", default=True,

How to make python's argparse generate Non-English text?

*爱你&永不变心* 提交于 2019-11-30 09:44:49
The argparse module "automatically generates help and usage messages". I can give Non-English names to the arguments and provide Non-English help texts; but the help output then becomes a mixture of at least two languages, because terms like usage , positional arguments , optional arguments and show this help message and exit are automatically generated in English. How can I replace this English output with translations? Preferably, I would like to provide the translations within the script, so that the script generates the same output wherever it is started. Edit: Based on the answer by Jon

会用python把linux命令写一遍的人,进大厂有多容易?

二次信任 提交于 2019-11-30 09:24:37
看过这篇《 2000字谏言,给那些想学Python的人,建议收藏后细看! 》的读者应该都对一个命令有点印象吧?没错,就是 linux 中经常会用到的 ls 命令。 文章中我就提到如何提升自己的 python 能力呢?直接找项目写,但是作为零基础 / 小白 / 入门 的你来说做一个博客还要学 web 框架、html、css、js,又成为了阻碍你写实际项目的阻碍。 所以我就推荐了这个命令:ls。写一个 ls 非常简单,你只需要会一点 linux 的基础知识,知道 ls 能做什么就好了。 那今天就给大家码了一个哪哪能用的 ls.py ,没错,windows 也可以哦~ 演示环境 操作系统:windows10 python版本:python 3.7 idea:pycharm 2018.2 使用模块:argparse, os 了解argparse模块 argparse是python的标准库,他可以使我们很友好的编写命令行界面,并且可以自动生成帮助文档和使用消息,还能在参数无效的时候发出错误。 argparse.ArgumentParse类参数理解 prog:改变应用的名字,我们可以使用 %(prog)s 引用应用的名字,默认的应用名字为文件名。 usage: 显示这个命令用法, 一般用来显示参数的用法 description:显示这个命令的帮助信息 epilog: 显示命令的帮助信息

Disable abbreviation in argparse

限于喜欢 提交于 2019-11-30 08:55:42
问题 argparse uses per default abbreviation in unambiguous cases. I don't want abbreviation and I'd like to disable it. But didn't find it in the documentation. Is it possible? Example: import argparse parser = argparse.ArgumentParser() parser.add_argument('--send', action='store_true') parser.parse_args(['--se']) # returns Namespace(send=True) But I want it only to be true when the full parameter is supplied. To prevent user errors. UPDATE: I created a ticket at python bugtracker after Vikas

Python argparse type and choice restrictions with nargs > 1

孤街醉人 提交于 2019-11-30 08:41:55
The title pretty much says it all. If I have nargs greater than 1, is there any way I can set restrictions (such as choice/type) on the individual args parsed? This is some example code: parser = argparse.ArgumentParser() parser.add_argument('-c', '--credits', nargs=2, help='number of credits required for a subject') For the -c argument I need to specify a subject and how many credits are required. The subject should be limited to a predefined list of subjects, and the number of credits required should be a float. I could probably do this with a subparser, but as it is this is already part of

Using argparse to parse arguments of form “arg= val”

耗尽温柔 提交于 2019-11-30 06:54:37
问题 I want to use argparse to parse command lines of form "arg=val" For example, the usage would be: script.py conf_dir=/tmp/good_conf To achieve it, I am doing this: desc = "details" parser = argparse.ArgumentParser(description=desc, add_help=False) args = parser.add_argument("conf_dir") args = parser.parse_args("conf_dir=FOO".split()) args = parser.parse_args() print args.conf_dir But, the problem is that, on invocation of the script with: python script.py conf_dir=/tmp/good_conf I get: conf

Arguments that are dependent on other arguments with Argparse

£可爱£侵袭症+ 提交于 2019-11-30 06:44:13
I want to accomplish something like this: -LoadFiles -SourceFile "" -DestPath "" -SourceFolder "" -DestPath "" -GenericOperation -SpecificOperation -Arga "" -Argb "" -OtherOperation -Argc "" -Argb "" -Argc "" A user should be able to run things like: -LoadFiles -SourceFile "somePath" -DestPath "somePath" or -LoadFiles -SourceFolder "somePath" -DestPath "somePath" Basically, if you have -LoadFiles, you are required to have either -SourceFile or -SourceFolder after. If you have -SourceFile, you are required to have -DestPath, etc. Is this chain of required arguments for other arguments possible?

会用python把linux命令写一遍的人,进大厂有多容易?

心不动则不痛 提交于 2019-11-30 06:07:29
看过这篇《 2000字谏言,给那些想学Python的人,建议收藏后细看! 》的读者应该都对一个命令有点印象吧?没错,就是 linux 中经常会用到的 ls 命令。 文章中我就提到如何提升自己的 python 能力呢?直接找项目写,但是作为零基础 / 小白 / 入门 的你来说做一个博客还要学 web 框架、html、css、js,又成为了阻碍你写实际项目的阻碍。 所以我就推荐了这个命令:ls。写一个 ls 非常简单,你只需要会一点 linux 的基础知识,知道 ls 能做什么就好了。 那今天就给大家码了一个哪哪能用的 ls.py ,没错,windows 也可以哦~ 演示环境 操作系统:windows10 python版本:python 3.7 idea:pycharm 2018.2 使用模块:argparse, os 了解argparse模块 argparse是python的标准库,他可以使我们很友好的编写命令行界面,并且可以自动生成帮助文档和使用消息,还能在参数无效的时候发出错误。 argparse.ArgumentParse类参数理解 prog:改变应用的名字,我们可以使用 %(prog)s 引用应用的名字,默认的应用名字为文件名。 usage: 显示这个命令用法, 一般用来显示参数的用法 description:显示这个命令的帮助信息 epilog: 显示命令的帮助信息

Python argparser repeat subparse

一笑奈何 提交于 2019-11-30 06:01:19
问题 I'm using pythons(2.7.2) argparse (1.1) to parse command line and what I want is to create subparser and make it possible to enter subparser commands multiple times. Like this: ./script.py version 1 --file 1 2 3 version 3 --file 4 5 6 Is it possible to create such thing? Because now when I try to run script with such arguments in result namespase a get: Namespace(file=['4', '5', '6'], n=[1]) n it is a version number. So I get first version number and second list of files instead both file