argparse

argparse subparser monolithic help output

老子叫甜甜 提交于 2019-11-27 08:15:37
My argparse has only 3 flags (store_true) on the top level, everything else is handled through subparsers. When I run myprog.py --help , the output shows a list of all subcommands like normal, {sub1, sub2, sub3, sub4, ...} . So, the default is working great... I usually can't remember the exact subcommand name I need, and all of its options. So I end up doing 2 help lookups: myprog.py --help myprog.py sub1 --help I do this so often, I decided to cram this into one step. I would rather have my toplevel help output a huge summary, and then I scroll through the list manually. I find it is much

Python argparse - Add argument to multiple subparsers

不问归期 提交于 2019-11-27 06:57:18
My script defines one main parser and multiple subparsers. I want to apply the -p argument to some subparsers. So far the code looks like this: parser = argparse.ArgumentParser(prog="myProg") subparsers = parser.add_subparsers(title="actions") parser.add_argument("-v", "--verbose", action="store_true", dest="VERBOSE", help="run in verbose mode") parser_create = subparsers.add_parser ("create", help = "create the orbix environment") parser_create.add_argument ("-p", type = int, required = True, help = "set db parameter") # Update parser_update = subparsers.add_parser ("update", help = "update

How to handle CLI subcommands with argparse

和自甴很熟 提交于 2019-11-27 06:14:42
问题 I need to implement a command line interface in which the program accepts subcommands. For example, if the program is called “foo”, the CLI would look like foo cmd1 <cmd1-options> foo cmd2 foo cmd3 <cmd3-options> cmd1 and cmd3 must be used with at least one of their options and the three cmd* arguments are always exclusive. I am trying to use subparsers in argparse, but with no success for the moment. The problem is with cmd2 , that has no arguments: if I try to add the subparser entry with

Display help message with python argparse when script is called without any arguments

会有一股神秘感。 提交于 2019-11-27 05:57:58
This might be a simple one. Assume I have a program that uses argparse to process command line arguments/options. The following will print the 'help' message: ./myprogram -h or: ./myprogram --help But, if I run the script without any arguments whatsoever, it doesn't do anything. What I want it to do is to display the usage message when it is called with no arguments. How is that done? This answer comes from Steven Bethard on Google groups . I'm reposting it here to make it easier for people without a Google account to access. You can override the default behavior of the error method: import

How do I create a Python namespace (argparse.parse_args value)?

一曲冷凌霜 提交于 2019-11-27 05:31:47
问题 To interactively test my python script, I would like to create a Namespace object, similar to what would be returned by argparse.parse_args() . The obvious way, >>> import argparse >>> parser = argparse.ArgumentParser() >>> parser.parse_args() Namespace() >>> parser.parse_args("-a") usage: [-h] : error: unrecognized arguments: - a Process Python exited abnormally with code 2 may result in Python repl exiting (as above) on a silly error. So, what is the easiest way to create a Python namespace

Get selected subcommand with argparse

你离开我真会死。 提交于 2019-11-27 05:09:59
问题 When I use subcommands with python argparse, I can get the selected arguments. parser = argparse.ArgumentParser() parser.add_argument('-g', '--global') subparsers = parser.add_subparsers() foo_parser = subparsers.add_parser('foo') foo_parser.add_argument('-c', '--count') bar_parser = subparsers.add_parser('bar') args = parser.parse_args(['-g, 'xyz', 'foo', '--count', '42']) # args => Namespace(global='xyz', count='42') So args doesn't contain 'foo' . Simply writing sys.argv[1] doesn't work

Python 命令行之旅 —— 初探 argparse

 ̄綄美尐妖づ 提交于 2019-11-27 04:43:00
『讲解开源项目系列』启动——让对开源项目感兴趣的人不再畏惧、让开源项目的发起者不再孤单。跟着我们的文章,你会发现编程的乐趣、使用和发现参与开源项目如此简单。欢迎联系我们给我们投稿,让更多人爱上开源、贡献开源~ 前言 你是否好奇过在命令行中敲入一段命令后,它是如何被解析执行的?是否考虑过由自己实现一个命令行工具,帮你执行和处理任务?是否了解过陪伴在你身边的 Python 有着丰富的库,来帮你轻松打造命令行工具? 别着急,本文作为 Python 命令行之旅的第一篇将带你逐步揭开命令行解析的面纱,介绍如何使用 Python 内置的 argparse 标准库解析命令行,并在后续的系列文章中介绍各具特色的第三方命令行库,讲讲它们的异同,进而全面地体验这次探索的旅程。 本系列文章默认使用 Python 3 作为解释器进行讲解。 若你仍在使用 Python 2,请注意两者之间语法和库的使用差异哦~ 介绍 argparse 作为 Python 内置的标准库,提供了较为简单的方式来编写命令行接口。当你在程序中定义需要哪些参数, argparse 便会从 sys.argv 中获取命令行输入进行解析,对正确或非法输入做出响应,也可以自动生成帮助信息和使用说明。 快速开始 设置解析器 第一步要做的就是设置解析器,后续对命令行的解析就依赖于这个解析器,它能够将命令行字符串转换为 Python 对象。

Python Argparse conditionally required arguments

。_饼干妹妹 提交于 2019-11-27 04:24:44
问题 I have done as much research as possible but I haven't found the best way to make certain cmdline arguments necessary only under certain conditions, in this case only if other arguments have been given. Here's what I want to do at a very basic level: p = argparse.ArgumentParser(description='...') p.add_argument('--argument', required=False) p.add_argument('-a', required=False) # only required if --argument is given p.add_argument('-b', required=False) # only required if --argument is given

How can I get argparse in Python 2.6?

北战南征 提交于 2019-11-27 03:43:09
问题 I have some Python 2.7 code written that uses the argparse module. Now I need to run it on a Python 2.6 machine and it won't work since argparse was added in 2.7. Is there anyway I can get argparse in 2.6? I would like to avoid rewriting the code, since I will be transferring this kind of code between the machines often. Upgrading python is not an option. I should have clarified that the ideal solution would be something that does not require module installation. 回答1: You can install it via

Python argparse ignore unrecognised arguments

耗尽温柔 提交于 2019-11-27 03:36:39
Optparse, the old version just ignores all unrecognised arguments and carries on. In most situations, this isn't ideal and was changed in argparse. But there are a few situations where you want to ignore any unrecognised arguments and parse the ones you've specified. For example: parser = argparse.ArgumentParser() parser.add_argument('--foo', dest="foo") parser.parse_args() $python myscript.py --foo 1 --bar 2 error: unrecognized arguments: --bar Is there anyway to overwrite this? Replace args = parser.parse_args() with args, unknown = parser.parse_known_args() For example, import argparse