argparse

How do I make an argparse argument optional when using subparsers?

寵の児 提交于 2019-12-12 03:28:20
问题 I'm working on a simple Git/Redmine glue script but I'm having some difficulty using optional arguments with the Python argparse module. With the following code: import argparse class MyClass: def StartWork(self, issueNumber, **kwargs): if issueNumber is None: issueNumber = input("please enter an issue number: ") else: print("issue number detected") print(issueNumber) parser = argparse.ArgumentParser() subparsers = parser.add_subparsers(dest='MyClass-command', help='Command to perform')

Python argparse AssertionError from metavar `[[USER@]HOST:]FILE`

橙三吉。 提交于 2019-12-12 03:05:12
问题 Argparse 1.1 or 1.4 fails with AssertionError - the weird regex that reads the metavar value seems to blow up argparse - Possibly related to Python argparse AssertionError when number of arguments exceeds threshold? Is there any alternative way to create or use the metavar [[USER@]HOST:]FILE ? Test setup: $ virtualenv-3.5 --always-copy test2 $ ./test2/bin/pip install argparse Python code test.py: #!/Users/[username]/Development/test2/bin/python3.5 import os import sys print('sys.prefix', sys

Argparse: Required argument 'y' if 'x' is present

ε祈祈猫儿з 提交于 2019-12-12 03:04:57
问题 I have a requirement as follows: ./xyifier --prox --lport lport --rport rport for the argument prox , I use action='store_true' to check if it is present or not. I do not require any of the arguments. But, if --prox is set I require rport and lport as well. Is there an easy way of doing this with argparse without writing custom conditional coding. More Code: non_int.add_argument('--prox', action='store_true', help='Flag to turn on proxy') non_int.add_argument('--lport', type=int, help='Listen

Custom parsing function for any number of arguments in Python argparse

醉酒当歌 提交于 2019-12-12 00:18:22
问题 I have a script that gets named parameters via command-line. One of arguments may be supplied multiple times. For example I want to run a script: ./script.py --add-net=user1:10.0.0.0/24 --add-net=user2:10.0.1.0/24 --add-net=user3:10.0.2.0/24 Now I want to have an argparse action that will parse every parameter and store results in a dict like: { 'user1': '10.0.0.0/24', 'user2': '10.0.1.0/24', 'user3': '10.0.2.0/24' } Also there should be a default value that will be supplied if there's no

Mandatory sub-arguments for particular value of conditional arguments -Python 2.7

半世苍凉 提交于 2019-12-12 00:03:38
问题 I need to parse additional arguments depending on the value of a conditional argument ('--name'). I don't want to handle it with 'if statements'. import argparse parser = argparse.ArgumentParser() parser.add_argument("--name", required = True, choices = ['a','b','c']) args = parser.parse_args() if args.name== 'a': parser.add_argument( add some mandatory arguments here ) if args.name== 'b': parser.add_argument( add some mandatory arguments here ) if args.name== 'c': parser.add_argument( add

Google Prediction API, Hello Prediction: error - Too few arguments

跟風遠走 提交于 2019-12-11 23:17:37
问题 Today is my first day trying out Google Prediction API on Anaconda Python (Ubuntu Linux). I wanted to try out the Hello Prediction starter code prediction.py using the following: $ python prediction.py --object_name="mymodelid/mybucket" --id="myidentifier" exactly how it is advised in the code documentation. However, I am getting the following error: usage: pred.py [-h] [--auth_host_name AUTH_HOST_NAME] [--noauth_local_webserver] [--auth_host_port [AUTH_HOST_PORT [AUTH_HOST_PORT ...]]] [-

Favoring a pip-installed module over the standard library copy

蓝咒 提交于 2019-12-11 20:36:50
问题 I've been digging into a Module was already imported warning I get when I run ipython and various other programs in Python 2.7: $ ipython [path to python]/lib/python2.7/site-packages/path.py:122: UserWarning: Module argparse was already imported from [path to python]/lib/python2.7/argparse.pyc, but [path to python]/lib/python2.7/site-packages is being added to sys.path import pkg_resources This stems from argparse being included both in the standard library and as a module from PyPI. In my

is there a way to clear python argparse?

徘徊边缘 提交于 2019-12-11 20:24:50
问题 Consider the following script: import argparse parser1 = argparse.ArgumentParser() parser1.add_argument('-a') args1 = parser1.parse_args() parser2 = argparse.ArgumentParser() parser2.add_argument('-b') args2 = parser2.parse_args() I have several questions: Is parse_args a one-time method or is there a way to clear the arguments before adding new ones? (e.g. something like args1.clear() or parser1.clear() ) The result of this script is unusable. Although this script accepts the -a argument, it

Python argparse seems not to support too long arguments?

我的未来我决定 提交于 2019-12-11 16:47:41
问题 i create an script with the following args: def arguments(): ''' gets arguments ''' parser = argparse.ArgumentParser( description="This scripts start the setup for the project, this setup is defined by the Core \ Module of each Setup, so first it download the scripts from that module and \ proceed to run them from a temporal directory installing the desired \ components and applying the configuration." ) parser.add_argument( "--project_name", help="The name for the project", type=str,

importing custom python modules.. why do only some elements carry over?

╄→гoц情女王★ 提交于 2019-12-11 15:33:25
问题 I need to create a python module that many scripts need to ultimately import from: Custom arg parsing to set format and constant args (i.e. my verbose and help args, and everything visually pleasing/consistent across scripts) My custom module is currently working independently and I'm satisfied enough to move forward for now, but I can't seem to import it into another script properly. THAT module imports another module (ver.py) defining a simple variable constant with no issues (a version