argparse

argparse arguments nesting

≡放荡痞女 提交于 2019-12-04 23:56:27
I have a following code in python: parser = argparse.ArgumentParser(description='Deployment tool') group = parser.add_mutually_exclusive_group() group.add_argument('-a', '--add', dest='name_to_add', help='Add a new group or a role to existing group') group.add_argument('-u', '--upgrade', dest='name_to_upgrade', help='Upgrade a group with the new version') parser.add_argument('--web_port', help='Port of the WEB instance that is being added to the group') My problem is with "--web_port" option. I want to be able to add this option only with "-a" option but not with "-u". I want to be able to run

Using argparse in conjunction with sys.argv in Python

不问归期 提交于 2019-12-04 23:54:21
问题 I currently have a script, which uses file globbing via the sys.argv variable like this: if len(sys.argv) > 1: for filename in sys.argv[1:]: This works great for processing a bunch of files; however, I would like to use this with the argparse module as well. So, I would like my program to be able to handle something like the following: foo@bar:~$ myScript.py --filter=xyz *.avi Has anyone tried to do this, or have some pointers on how to proceed? 回答1: If I got you correctly, your question is

Is it bad form to raise ArgumentError by hand?

家住魔仙堡 提交于 2019-12-04 23:22:32
If you want to add an extra check not provided by argparse , such as: if variable a == b then c should be not None ...is it permissible to raise ArgumentError yourself? Or, should you raise Exception instead? Also what is common practice for this kind of situation? Say that you add a piece of code that's almost like a local extension of the library. Should you use the same exception type(s) as those provided by the library you are extending? There's nothing inherently wrong with raising an ArgumentError. You can use it anytime the arguments you receive are not what you expected them to be,

python using argparse.ArgumentParser method

孤街醉人 提交于 2019-12-04 22:50:45
I've tried to learn how argparse.ArgumentParser works and I've write a couple lines for that : global firstProduct global secondProduct myparser=argparse.ArgumentParser(description='parser test') myparser.add_argument("product1",help="enter product1",dest='product_1') myparser.add_argument("product2",help="enter product2",dest='product_2') args=myparser.parse_args() firstProduct=args.product_1 secondProduct=args.product_2 I just want to that when User run this script with 2 parameters my code assign them to firstProduct and secondProduct respectively. However it doesn’t work. Is there anyone

What is a namespace object?

我的梦境 提交于 2019-12-04 22:42:58
import argparse parser = argparse.ArgumentParser(description='sort given numbers') parser.add_argument('-s', nargs = '+', type = int) args = parser.parse_args() print(args) On command line when I run the command python3 file_name.py -s 9 8 76 It prints Namespace(s=[9, 8, 76]) . How can I access the list [9, 8, 76]? What is the namespace object. Where can I learn more about it? The documentation for argparse.Namespace can be found here . You can access the s attribute by doing args.s . If you'd like to access this as a dictionary, you can do vars(args) , which means you can also do vars(args)[

Python argparse value range help message appearance

醉酒当歌 提交于 2019-12-04 21:25:34
问题 I have an argument for a program that is an integer from 1-100 and I just don't like the way that it shows up in the -h help message when using argparse (it literally lists 0, 1, 2, 3, 4, 5,... etc) Is there any way to change this or have it represented in another way? Thanks EDIT: Here is the code for those who asked: norse = parser.add_argument_group('Norse') norse.add_argument('-n', '--norse', required=False, help='Run the Norse IPViking scan.', action='store_true') norse.add_argument('-

With argparse are subparsers inherently mutually exclusive?

断了今生、忘了曾经 提交于 2019-12-04 20:14:54
I have a script with two primary functions, upgrade and provision. I'm using subparsers as a way to dictate the action being performed by the script but I want to avoid them being used together. This is a snippet of the code: import argparse def main(): parser = argparse.ArgumentParser() subparser = parser.add_subparsers(help='sub-command help') parser.add_argument('--user', '-u', help='User', default=None, required=True) parser.add_argument('--password', '-p', help='Password', default=None, required=True) parser.add_argument('--server', '-s', help='server with Admin functionality', default

argparse — requiring either 2 values or none for an optional argument

无人久伴 提交于 2019-12-04 19:28:52
问题 I'm trying to make an optional argument for a script that can either take no values or 2 values, nothing else. Can you accomplish this using argparse? # desired output: # ./script.py -a --> works # ./script.py -a val1 --> error # ./script.py -a val1 val2 --> works version 1 -- accepts 0 or 1 values: parser = argparse.ArgumentParser() parser.add_argument("-a", "--action", nargs="?", const=True, action="store", help="do some action") args = parser.parse_args() # output: # ./script.py -a -->

parsing argument in python

僤鯓⒐⒋嵵緔 提交于 2019-12-04 18:24:11
I have a problem I am trying to find a solution. I am not sure if I can do it with argparse. I want to be able to specify an option myprog -a 1 myprog -a 2 Now when I have a = 1 , I want to be able to specify b and c . But when a = 2 , I can only specify d . myprog -a 1 -b 3 -c 0 myprog -a 2 -d 3 Also a must always be specified You can't do this with switched values as a single parse_args call, but you can do one of: Use sub-commands/sub-parsers Do partial parsing before fully configuring the parser and running on the complete command line, at first only checking for a , and then selecting the

Python multiprocessing throws error with argparse and pyinstaller

ε祈祈猫儿з 提交于 2019-12-04 18:06:26
In my project, I'm using argprse to pass arguments and somewhere in script I'm using multiprocessing to do rest of the calculations. Script is working fine if I call it from command prompt for ex. " python complete_script.py --arg1=xy --arg2=yz " . But after converting it to exe using Pyinstaller using command "pyinstaller --onefile complete_script.py" it throws error " error: unrecognized arguments: --multiprocessing-fork 1448" Any suggestions how could I make this work. Or any other alternative. My goal is to create an exe application which I can call in other system where Python is not