argparse

argparse doesn't check for positional arguments

白昼怎懂夜的黑 提交于 2019-12-06 06:04:38
I'm creating a script that takes both positional and optional arguments with argparse. I have gone through Doug's tutorial and the python Docs but can't find an answer. parser = argparse.ArgumentParser(description='script to run') parser.add_argument('inputFile', nargs='?', type=argparse.FileType('rt'), parser.add_argument('inputString', action='store', nargs='?') parser.add_argument('-option1', metavar='percent', type=float, action='store') parser.add_argument('-option2', metavar='outFile1', type=argparse.FileType('w'), parser.add_argument('-option3', action='store', default='<10', args =

How to add positional options for existing arguments in argparse

拥有回忆 提交于 2019-12-06 06:01:29
I'm dealing with a (Python 3.x) script (written by someone else) where the input and output are currently specified with flagged optional arguments like so: parser.add_argument('-i', '--input', nargs='?', type = argparse.FileType('r'), default=sys.stdin, dest='inputfile') parser.add_argument('-o', '--output-file', nargs='?', type=argparse.FileType('w'), default=sys.stdout, dest='outputfile') I'd like to upgrade this script so that the input and output file can be specified as positional arguments while maintaining the existing flag arguments for backwards compatibility. I'd also like to

max_help_position is not works in python argparse library

二次信任 提交于 2019-12-06 03:52:18
问题 Hi colleagues I have the code (max_help_position is 2000): formatter_class=lambda prog: argparse.HelpFormatter(prog, max_help_position=2000) parser = argparse.ArgumentParser(formatter_class=formatter_class) subparsers = parser.add_subparsers(title="Commands", metavar="<command>") cmd_parser = subparsers.add_parser('long_long_long_long_long_long_long', help='- jksljdalkjda', formatter_class=formatter_class) args = parser.parse_args(['-h']) print args we have optional arguments: -h, --help show

Parser in python3 does not take delimiter values from commandline via argparse

萝らか妹 提交于 2019-12-06 03:04:07
I have written a simple script as an advanced tool for my awk / sed requirements. In the script I compare two files on basis of values from one column of the query file and then extract whole entries from the master file. The script allows you to enter the values for columns and delimiters for each file. The problem is that the 'delimiter' options are not recognized by script when given from command line. Here is my code (partial): ##- - - - - - - -- - - - - - Arguments - - - - - - - - - - - - - -## parser = argparse.ArgumentParser() ## Command line options parser.add_argument("-m", "--master"

optional python arguments without dashes but with additional parameters?

▼魔方 西西 提交于 2019-12-06 00:42:38
问题 what I'd like to do in Python is accept arguments of the following format: script.py START | STOP | STATUS | MOVEABS <x> <y> | MOVEREL <x> <y> So in other words, I don't want to deal with hyphens; I have multiple possibilities, ONE of which is required; Each is mutually exclusive; Some of the commands (E.G. moveabs and moverel) have additional required arguments, but these args and should not be present with any other argument. Can this be done in python and would I use argparse or something

Neatly pass positional arguments as args and optional arguments as kwargs from argparse to a function

落花浮王杯 提交于 2019-12-06 00:10:12
I would like to write a Python script that takes some necessary positional and some optional command-line arguments via argparse : Let's call the positional args a , b , c , and the optional arguments x , y , z . In my Python script, I would like to pass these args on to a function; specifically, I want a , b , c to be passed as *args , and x , y , z to be passed as **kwargs , the latter retaining their names. I would like to do this many times with different functions and different numbers of positional and optional arguments. Is there a neat, flexible, and/or pythonic way to do this? Here is

Python argparse: How to change `--add` into `add` while still being an optional argument?

允我心安 提交于 2019-12-05 20:32:01
I want this functionality: $ python program.py add Peter 'Peter' was added to the list of names. I can achieve this with --add instead of add like this: import argparse parser = argparse.ArgumentParser() parser.add_argument("--add", help="Add a new name to the list of names", action="store") args = parser.parse_args() if args.add: print "'%s' was added to the list of names." % args.add else: print "Just executing the program baby." Such that: $ python program.py --add Peter 'Peter' was added to the list of names. But when I change --add to add it is no longer optional, how can I still let it

Conditional argparse with choice option

空扰寡人 提交于 2019-12-05 19:35:30
Below are three arguments I am writing in a module. parser.add_argument('--Type',type=str,choices=['a','b','c'],help='Options include: a,b,c.',required=True) parser.add_argument('--Input',default=False,help='Generate input files',required=False) parser.add_argument('--Directory',default=False,help='Secondary directory',required='--Input' in sys.argv) The --Type is possible with three options: a,b,c. Currently, I have it set up so that, if --Directory is true, it requires --Input to be true. However, I would like add an additional condition to --Directory to require --Type to be == 'c'. How do

How to pass command line arguments in IPython jupyter notebook

无人久伴 提交于 2019-12-05 17:58:54
I a new to Ipython. Currently i have installed Ipython using Anaconda and writing a code to plot chart using jupyter notebook UI. I want to pass few arguments to my working script with the help of argparse module. below is the code.. import argparse parser = argparse.ArgumentParser(description = 'Process display arguments') parser.add_argument('-t', "--test_name", help="Mandatory test name directory path", type=str) parser.add_argument('-s', "--symbolSet", nargs = '?', help="Optional symbolset", const = 'baz', default = 'deafultOne') args = parser.parse_args() if args.test_name is None: print(

argparse Python modules in cli

醉酒当歌 提交于 2019-12-05 14:51:39
问题 I am trying to run a python script from the Linux SSH Secure Shell command line environment, and I am trying to import the argparse library, but it gives the error: "ImportError: No module named argparse". I think that this is because the Python environment that the Linux shell is using does not have the argparse library in it, and I think I can fix it fix it if I can find the directories for the libraries being used by the Python environment, and copy the argparse library into it, but I can