argparse

Require either of two arguments using argparse

梦想的初衷 提交于 2020-01-22 04:08:59
问题 Given: import argparse pa = argparse.ArgumentParser() pa.add_argument('--foo') pa.add_argument('--bar') print pa.parse_args('--foo 1'.split()) how do I make at least one of "foo, bar" mandatory: --foo x , --bar y and --foo x --bar y are fine make at most one of "foo, bar" mandatory: --foo x or --bar y are fine, --foo x --bar y is not 回答1: I think you are searching for something like mutual exclusion (at least for the second part of your question). This way, only foo or bar will be accepted,

plugins pattern + sub command

时光总嘲笑我的痴心妄想 提交于 2020-01-22 01:57:29
问题 I will do a command line application with plugin capability, each new plugin will be invoked by a sub command from a __main__.py script. I used to use argparse , I wonder if it's possible with argparse to implement sub command + plugin looking like (I found some tool but using deprecated packages) ? myfantasticCLI ├── __main__.py └── plugins ├── create.py ├── notify.py └── test.py I know that I could use argparse for sub command, but don't know how to use it in a dynamic loading way. :/ 回答1:

plugins pattern + sub command

橙三吉。 提交于 2020-01-22 01:56:48
问题 I will do a command line application with plugin capability, each new plugin will be invoked by a sub command from a __main__.py script. I used to use argparse , I wonder if it's possible with argparse to implement sub command + plugin looking like (I found some tool but using deprecated packages) ? myfantasticCLI ├── __main__.py └── plugins ├── create.py ├── notify.py └── test.py I know that I could use argparse for sub command, but don't know how to use it in a dynamic loading way. :/ 回答1:

Explanation for argparse python modul behaviour: Where do the capital placeholders come from?

ぐ巨炮叔叔 提交于 2020-01-21 11:52:13
问题 I am trying to write a command line interface (for the first time) and after reading up about argparse , optparse and getopt I chose argparse because of several recommendations here on SO and elswhere in the net. Adapting a little of the advice of Mr. van Rossum I hooked up my first command line interface like this: def main(argv=None): if argv is None: argv = sys.argv desc = u'some description' parser = argparse.ArgumentParser(description=desc) parser.add_argument('-s', '--search', help=

Explanation for argparse python modul behaviour: Where do the capital placeholders come from?

时光总嘲笑我的痴心妄想 提交于 2020-01-21 11:52:11
问题 I am trying to write a command line interface (for the first time) and after reading up about argparse , optparse and getopt I chose argparse because of several recommendations here on SO and elswhere in the net. Adapting a little of the advice of Mr. van Rossum I hooked up my first command line interface like this: def main(argv=None): if argv is None: argv = sys.argv desc = u'some description' parser = argparse.ArgumentParser(description=desc) parser.add_argument('-s', '--search', help=

How to make at least one argument required and have the possibility to take all arguments with argparse in Python?

吃可爱长大的小学妹 提交于 2020-01-16 18:42:07
问题 The program has 2 arguments to deal with: state and key. I need to have a possibility to give as input the following options: prog -state state_value prog -key key_value prog -state state_value -key key_value The closest thing is using mutually excluding groups, but it unables the possibility to give both arguments as input at once. 回答1: I think this is beyond the abilities of argparse. You could perform a simple validation on the returned namespace afterwards though. You should include in

argparse简要用法总结

你说的曾经没有我的故事 提交于 2020-01-16 17:50:15
argparse 是python自带的命令行参数解析包,可以用来方便地读取命令行参数,当你的代码需要频繁地修改参数的时候,使用这个工具可以将参数和代码分离开来,让你的代码更简洁,适用范围更广。 argparse使用比较简单,常用的功能可能较快地实现出来,下面我分几个步骤, 以Python3为例 ,逐渐递增地讲述argparse的用法。 1. 基本框架 下面是使用argparse从命令行获取用户名,然后打印’Hello ‘+ 用户名,假设python文件名为 print_name.py : # file-name:print_name.py import argparse def get_parser(): parser = argparse.ArgumentParser(description="Demo of argparse") parser.add_argument('--name', default='Great') return parser if __name__ == '__main__': parser = get_parser() args = parser.parse_args() name = args.name print('Hello {}'.format(name)) 在命令行执行如下命令: $ python print_name.py --name

Translate argparse's internal strings

天大地大妈咪最大 提交于 2020-01-16 03:56:27
问题 How can I translate Python's argparse module strings? For example, when you display the help, it says "usage: " , string that is translatable but I don't know how to do it in my program. This is the source code part of argparse.py : def _format_usage(self, usage, actions, groups, prefix): if prefix is None: prefix = _('usage: ') I couldn't trace the way to set this prefix, I think this is internal. I don't want to have to append a .mo file to somewhere in the system. Ideally the translation

Python argparse errors to file

时光总嘲笑我的痴心妄想 提交于 2020-01-15 12:08:34
问题 I am trying to make a script that takes all errors and logs them to a log file. I want to include any argparse errors to this file. I already use the logging package and a sys.excepthook to drive unexpected exceptions to the log file. Here's an example code: import argparse import logging import sys import traceback def log_uncaught_exceptions(ex_cls, ex, tb): logging.critical(''.join(traceback.format_tb(tb))) logging.critical('{0}: {1}'.format(ex_cls, ex)) logging.basicConfig( level=logging

Python multi-command CLI with common options

吃可爱长大的小学妹 提交于 2020-01-15 08:52:06
问题 I am adding CLI for my Python application. The CLI should allow to run multiple commands in a time. The commands should have common options and personal options. Example : $ python mycliapp.py --common-option1 value1 --common-option2 value2 cmd1 --cmd1-option cmd2 --cmd2-option somevalue cmd3 The example has two common options used by all commands and each command can have or not the option used by the command only. I have considered Python Click. It has rich functionality, but it does not