argparse

argparse default option based on another option

你离开我真会死。 提交于 2019-11-28 07:02:30
问题 Suppose I have an argparse python script: import argparse parser = argparse.ArgumentParser() parser.add_argument("--foo", required=True) Now I want to add another option --bar, which would default to appending "_BAR" to whatever was specified by --foo argument. My goal: >>> parser.parse_args(['--foo', 'FOO']) >>> Namespace(foo='FOO', bar="FOO_BAR") AND >>> parser.parse_args(['--foo', 'FOO', '--bar', 'BAR']) >>> Namespace(foo='FOO', bar="BAR") I need something like this: parser.add_argument("-

How to use argparse subparsers correctly?

孤街浪徒 提交于 2019-11-28 06:43:02
I've been searching through allot of the subparser examples on here and in general but can't seem to figure this seemingly simple thing out. I have two var types of which one has constraints so thought subparser was the way to go. e.g. -t allows for either "A" or "B". If the user passes "A" then they are further required to also specify if it is either "a1" or "a2". If they pass just "B" then nothing. Can I do this and have argparse return me what type of "A" was passed or if it was just "B"? The below seems to work but for some reason breaks when passing anything after the subparse. e.g. from

How to stop Python program compiled in py2exe from displaying ImportError: No Module names 'ctypes'

人走茶凉 提交于 2019-11-28 06:00:23
问题 I was wondering if this might be a compilation error or if there is something I can do to stop it from displaying. I have made an argparse program for cmd. I compiled it with py2exe and when I run it, it exacutes the program properly but always gives this error before running the code: Traceback (most recent call last): File "boot_common.py", line 46, in <module> ImportError: No module named 'ctypes' If it is something in my code, here is my script: import argparse import zipfile import os

Overwriting Google Drive API v3 Argparse in Python

主宰稳场 提交于 2019-11-28 05:18:57
问题 I'm trying to use the Google Drive API (v3) with Python to obtain and upload files to my Google Drive account. I used this guide to setup my authentication: https://developers.google.com/drive/v3/web/quickstart/python But for my program, I would like to take commandline input for username, filename, and output_filename. I modified the google doc code and did the following: from __future__ import print_function import httplib2 import os from sys import argv from apiclient import discovery from

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

末鹿安然 提交于 2019-11-28 05:16:06
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 with a given set of attributes? E.g., I can create a dict on the fly ( dict([("a",1),("b","c")]) ) but

Argparse unit tests: Suppress the help message

僤鯓⒐⒋嵵緔 提交于 2019-11-28 04:47:42
问题 I'm writing test cases for argparse implementation. I intend to test '-h' feature. The following code does it. But it also outputs the usage for the script. Is there a way to suppress that? self.assertRaises(SystemExit, arg_parse_obj.parse_known_args, ['-h']) Also, can we check for the exception number thrown? For example '-h' throws SystemExit: 0 , while invalid or insufficient args throw SystemExit: 2 . Is there a way to check the numeric code? 回答1: When testing for exception codes, use

Passing arguments into os.system

泪湿孤枕 提交于 2019-11-28 04:31:18
问题 I need to execute the following command through python. rtl2gds is a tool which reads in 2 parameters: Path to a file and a module name rtl2gds -rtl=/home/users/name/file.v -rtl_top=module_name -syn I am reading in the path to the file and module name from the user through argparse as shown below: parser = argparse.ArgumentParser(description='Read in a file..') parser.add_argument('fileread', type=argparse.FileType('r'), help='Enter the file path') parser.add_argument('-e', help='Enter the

Create variable key/value pairs with argparse (python)

*爱你&永不变心* 提交于 2019-11-28 03:54:43
问题 I'm using argparse module to set my command line options. I'm also using a dict as a config in my application. Simple key/value store. What I'm looking for is a possibility to override JSON options using command line arguments, without defining all possible arguments in advance. Something like --conf-key-1 value1 --conf-key-2 value2 , which would create a dict {'key_1': 'value1','key_2': 'value2'} ('-' in the argument is replaced by '_' in the dict). Then I can combine this dict with my JSON

Get selected subcommand with argparse

◇◆丶佛笑我妖孽 提交于 2019-11-28 03:31:11
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 because of the possible global args. How can I get the subcommand itself? robert The very bottom of the

Python argparse and bash completion

筅森魡賤 提交于 2019-11-28 03:06:06
I would like to get auto-completion on my python scripts also in the arguments. I had never really understood how the bash_completion worked (for arguments), but after I digged in I understood that: it uses "complete" to bind a completing function to a command every completing function basically is a copy of the argument parser The second point in particular is not great, because I would like to have it automatically generated. The best thing would be that the shell asks to my program at every TAB about what to complete, but I have the impression that this can't really work, is that correct?