argparse

Python using diferent options multiple times with argparse

巧了我就是萌 提交于 2021-01-28 04:00:32
问题 I am working on a custom Nagios script in which I would like to implement parsing of command line arguments in the same way that is used on existing Nagios Plugin check_disk . In that plugin you have an option parameter -C to Clear current config and start over with new parameters. Extracted from check_disk help: check_disk -w 100 -c 50 -C -w 1000 -c 500 -p /foo -C -w 5% -c 3% -p /bar ----(3)----- ---------(1)---------- --------(2)-------- Checks warning/critical thresholds: for volume /foo

Python using diferent options multiple times with argparse

流过昼夜 提交于 2021-01-28 03:50:59
问题 I am working on a custom Nagios script in which I would like to implement parsing of command line arguments in the same way that is used on existing Nagios Plugin check_disk . In that plugin you have an option parameter -C to Clear current config and start over with new parameters. Extracted from check_disk help: check_disk -w 100 -c 50 -C -w 1000 -c 500 -p /foo -C -w 5% -c 3% -p /bar ----(3)----- ---------(1)---------- --------(2)-------- Checks warning/critical thresholds: for volume /foo

Python Argparse - conditionally required arguments based on the value of another argument

大城市里の小女人 提交于 2021-01-28 03:35:41
问题 I am trying to write a daemon service, which can be controlled in the command line. For example, to start the service python3 test.py -c start -d /mydownloadfolder/ -j /myconfig.json to stop the service, python3 test.py -c stop The -d -j parameters are required only when I start the service. Therefore, I need to implement conditionally required arguments based on the value of another argument. I did some search and found this useful post Python Argparse conditionally required arguments The

Argparse: How to disallow some options in the presence of others - Python

倾然丶 夕夏残阳落幕 提交于 2021-01-27 17:54:39
问题 I have the following utility: import argparse parser = argparse.ArgumentParser(description='Do some action.') parser.add_argument('--foo', '--fo', type=int, default=-1, help='do something foo') parser.add_argument('--bar', '--br', type=int, default=-1, help='do something bar') parser.add_argument('--baz', '--bz', type=int, default=-1, help='do something baz') parser.add_argument('--bat', '--bt', type=int, default=-1, help='do something bat') However, if the --foo option is used, the --bat

python argparse stops parsing after it encounters '$'

对着背影说爱祢 提交于 2021-01-27 17:42:10
问题 I am trying to parse a command line using argparse from argparse import ArgumentParser argparser = ArgumentParser(prog="parse", description="desc") create.add_argument("--name",dest="name",required=True,help="Name for element") args = argparser.parse_args() print(args) When I execute this with below command python argparser.py --name "input$output$" The output is: ('args:', Namespace(name='input$')) Expected Output: ('args:', Namespace(name='input$output$')) Can you please help figure out

How can I list all registered arguments from an ArgumentParser instance?

与世无争的帅哥 提交于 2021-01-27 15:09:04
问题 Couldn't find any existing method for it so I wonder if there is a hack? 回答1: There is a hack; you can list all registered actions using the undocumented attribute parser._actions : >>> import argparse >>> parser = argparse.ArgumentParser() >>> parser.add_argument('--foo') _StoreAction(option_strings=['--foo'], dest='foo', nargs=None, const=None, default=None, type=None, choices=None, help=None, metavar=None) >>> parser.add_argument('--bar') _StoreAction(option_strings=['--bar'], dest='bar',

how to pass command line argument from pytest to code

折月煮酒 提交于 2021-01-22 04:19:06
问题 I am trying to pass arguments from a pytest testcase to a module being tested. For example, using the main.py from Python boilerplate, I can run it from the command line as: $ python3 main.py usage: main.py [-h] [-f] [-n NAME] [-v] [--version] arg main.py: error: the following arguments are required: arg $ python3 main.py xx hello world Namespace(arg='xx', flag=False, name=None, verbose=0) Now I am trying to do the same with pytest, with the following test_sample.py ( NOTE: the main.py

python argparse ignore other options when a specific option is used

与世无争的帅哥 提交于 2021-01-20 08:37:17
问题 I am writing a python program that I want to have a command line interface that behaves in a particular way The command line interface should accept the following invocations: my_prog test.svg foo my_prog --font=Sans test.svg foo (it will generate an svg with the word foo written in the specified or default font) Now I want to be able to also have this command accept the following invocation... my_prog --list-fonts which will list all of the valid options to --font as determined by the fonts

python argparse ignore other options when a specific option is used

我与影子孤独终老i 提交于 2021-01-20 08:36:36
问题 I am writing a python program that I want to have a command line interface that behaves in a particular way The command line interface should accept the following invocations: my_prog test.svg foo my_prog --font=Sans test.svg foo (it will generate an svg with the word foo written in the specified or default font) Now I want to be able to also have this command accept the following invocation... my_prog --list-fonts which will list all of the valid options to --font as determined by the fonts

is it possible to change display text of argparse argument name

余生颓废 提交于 2021-01-19 06:31:39
问题 using argparse i have made a command line tool, which takes below arguments, usage: ipush [-h] [-v] [-c] [-to [TO]] [-V] [-p PATCHES] [-d DIFF] from the below code.. parser = argparse.ArgumentParser(prog='ipush', description='Utility to push the last commit and email the color diff') parser.add_argument('-v', '--verbose', action='store_true', help='if enabled will spit every command and its resulting data.') parser.add_argument('-c', '--compose', action='store_true', help='compose message in