Error with argparse - python

I'm having some issues with getting argparse to run correctly. Previously, my script would be run as follows:
script.py <input_file(s)> <output_filename>
With code that looked like this:
cell_list_input = sys.argv[1:]
cell_list_output = sys.argv[len(cell_list_input)]
cell_list_input = cell_list_input[:len(cell_list_input)-1]
However, I'd like to add some argument parsing just to make it more readable and usable. Ideally, the format would be similar with the following options:
script.py -i <input_file(s)> -o <output_filename>
script.py --input_list <input_file(s)> --output <output_filename>
The argparse equivalent I'm trying to implement looks like this right now:
parser = argparse.ArgumentParser()
parser.add_argument('-i', '--input_list', action='append', dest='input_list', default=[], help='Input list of cell names')
parser.add_argument('-o', '--output', action='store', dest='output', help='Output file destination/name')
cli = parser.parse_args()
I know I'm doing something wrong, but can't seem to figure out what. Any help is appreciated. Thanks!

You need to specify nargs for your input file list. Try this:
parser.add_argument('-i', '--input_list', nargs="+", action='append', dest='input_list', default=[], help='Input list of cell names')
Complete example:
import argparse
parser = argparse.ArgumentParser()
parser.add_argument('-i', '--input_list', nargs="+", default=[], help='Input list of cell names')
parser.add_argument('-o', '--output', help='Output file destination/name')
cli = parser.parse_args()
print cli
Result:
$ python i.py -i a
Namespace(input_list=['a'], output=None)
$ python i.py -i a b c -o d
Namespace(input_list=['a', 'b', 'c'], output='d')

Related

Arguments in top level parser come before subparsers if it has subparsers

I have this MRE:
import argparse
parser = argparse.ArgumentParser(description='Bundle a Python application')
parser.add_argument(
'-o', '--output', metavar='OUTPUT FILE', dest='file_name', type=str,
default=None)
parser.add_argument(
'--extensions', '--ext', action='store_const', metavar='EXTENSIONS',
dest='extensions', const=True, default=False,
help='Whether to allow the importing of C extensions (not needed if C extensions are optional')
if 0:
actions_parser = parser.add_subparsers(
dest='action', metavar='ACTION', help='Action mod should take')
actions_parser.required = True
build_parser = actions_parser.add_parser("build")
build_parser.add_argument(
dest='root', metavar='PROJECT', type=str, help='Project path',
nargs='?', default='.')
get_parser = actions_parser.add_parser("get")
get_parser.add_argument(
dest='module', metavar='MODULE', type=str, help='Module to download')
args = parser.parse_args()
If you run this with python test.py --ext, this works as expected.
However, if you change the 0 to a 1, then python test.py foo --ext fails, even though it should work. Why?
As it turns out, all the arguments attached to the main parser must be before any subaction, e.g. python test.py --ext foo.

deferring exiting on --help when using parse_known_args()

I have a code that needs to build its command line arguments dynamically, based on configuration file. Schematically, what I end up doing is
parser = argparse.ArgumentParser()
parser.add_argument("-c", "--config", type=str, default="",
help="Path to config file.")
args, unknown = parser.parse_known_args()
#here do stuff with args.config to extend parser list of arguments, and then:
parser.parse_args()
The argument management seems to work perfectly well but the trouble I have is that --help will exit at the first call parse_known_args, instead of the second parser.parse_args() which would have shown all the dynamically added arguments.... Is there a way to solve this?
I had the same problem some time ago. It can be solved by using two parsers.
A first pre-parser, with add_help=False to determine the configuration file;
The second full parser with help.
# test.py
import argparse
# First pre-parser to determine the configuration file
parser = argparse.ArgumentParser(add_help=False)
parser.add_argument("-c", "--config", default="", help="Path to config file.")
args = parser.parse_known_args()
# Do something with args.config
# Full and real parser
parser = argparse.ArgumentParser()
parser.add_argument("-c", "--config", default="", help="Path to config file.")
parser.add_argument("-o", "--option", help="Option with default value from config file")
parser.parse_args()
This result in:
$ python3 test.py --help
usage: test.py [-h] [-c CONFIG] [-o OPTION]
optional arguments:
-h, --help show this help message and exit
-c CONFIG, --config CONFIG
Path to config file.
-o OPTION, --option OPTION
Option with default value from config file

Overwrite a default argument with another one using the command line

can anyone help me to overwrite a default argument with another one passing through the command line ?
this is my code:
parser = argparse.ArgumentParser()
parser.add_argument('log', action='store', help='path to the log file')
parser.add_argument('-f', '--filter', default =['filter.json'], help='path to a filter file')
parser.add_argument('-w', '--waiver', type =list, default = [], action='append', help='path to a waiver file')
args = parser.parse_args()
this is the command line to be used:
python3 script.py -f filter_file -w waiver_file log_file
so, I need to overwrite this default =['filter.json'] with filter_file
But don't know what should I do, can you support please ?
thanks
Your script is working:
My output:
PS C:\Users\xf01145\Documents\Python> & "C:/Program Files/Python37/python.exe"
c:/Users/xf01145/Documents/Python/cli.py log.log -f bla.bla blubb.bla test.test hugo.txt
Namespace(filter=['bla.bla', 'blubb.bla', 'test.test', 'hugo.txt'], log='log.log', waiver=[])
0 bla.bla
1 blubb.bla
2 test.test
3 hugo.txt
PS C:\Users\xf01145\Documents\Python>
from this:
import argparse
parser = argparse.ArgumentParser()
parser.add_argument('log', action='store', help='path to the log file')
parser.add_argument('-f', '--filter', default =['filter.json'], help='path to a filter file', nargs='+')
parser.add_argument('-w', '--waiver', type =list, default = [], action='append', help='path to a waiver file')
args = parser.parse_args()
print(args)
for i,item in enumerate(args.filter):
print(i, item)

python, argparse get argument from input- raw_input

I need help with this issue, I want to take an argument from user input using argparse save it to a variable and print out the result
Example:
Message to send:
-t Greeting -m hello guys how are you
prints out:
greeting hello guys how are you
this is my code:
import argparse, shlex
parser = argparse.ArgumentParser()
parser.add_argument('-t', action='store', dest='title')
parser.add_argument('-m', action='store', dest='msg')
command = raw_input('message to send')
args = parser.parse_args(shlex.split(command))
title = args.title
msg = args.msg
print title, msg
when you enter, -t hi -m hello, it works fine but when you enter more then one word, it doesn't work. Why?
Usually, when you declare a command line switch via the add_argument method, python considers that only the next world/value should be stored inside the resulting variable. That is why the following works fine:
-t hello -m world
While the following:
-t greetings -m hello world
returns something like:
greetings hello
Using nargs, you can tell python how many values should be stored in the final variable.
For example, in your code if you declare your command line switches as:
parser.add_argument('-t', action='store', dest='title', nargs='*', type=str, required=True)
parser.add_argument('-m', action='store', dest='msg', nargs='*', type=str, required=True)
Python knows that all values following the -t switch should be stored in a list called title. The same goes for the -m switch and the msg variable.
Here I also added the type and required arguments to indicate what type of value is expected and that both switches have to be present in the command line.
Fixing your entire script so far:
import argparse
parser = argparse.ArgumentParser()
parser.add_argument('-t', action='store', dest='title', nargs='*', type=str, required=True)
parser.add_argument('-m', action='store', dest='msg', nargs='*', type=str, required=True)
args = parser.parse_args()
print(" ".join(args.title), " ".join(args.msg))
All you have to do is call your script as follow:
python3 your_script.py -t greetings -m hello world
This should print:
greetings hello world
as a result.
You have 2 options here. Either quote the argument with spaces like so:
-t Hi -m "Hello word1 word2"
or
use davinellulinvega's answer. To get back the entire string with nargs='*', you'd have to:
parser.add_argument('-t', action='store', dest='title', nargs='*')
parser.add_argument('-m', action='store', dest='msg', nargs='*')
....
title = ' '.join(args.title)
msg = ' '.join(args.msg)

Unable to assign default values to arguments in Python argparse

I am using Python argparse to take in parameters through the CLI. I have tried using the following but when I don't give any one of the arguments, it gives the output as None. I want then default ones to be the ones provided in const=. Please take a look.
parser = argparse.ArgumentParser()
parser.add_argument('--input', nargs='?', const='testInput')
parser.add_argument('--target', nargs='?', const='testTarget')
parser.add_argument('--msg', nargs='?', const='helloFromTheOtherSide')
args = parser.parse_args()
print args.input
If I don't give input, it prints it as None as I said. I want it to print TestInput instead..
Use the default argument:
parser = argparse.ArgumentParser()
parser.add_argument('--input', nargs='?', default='testInput')
parser.add_argument('--target', nargs='?', default='testTarget')
parser.add_argument('--msg', nargs='?', default='helloFromTheOtherSide')
args = parser.parse_args()
print args.input
With
parser.add_argument('--input', nargs='?', default='testInput', const='aConst')
you have a 3 way choice
prog # input='testInput'
prog --input # input='aConst'
prog --input myfile # input='myfile'
If you don't need that aConst option, omit the nargs='?'. Since it is a flagged argument it is already optional. It doesn't need the `?'.
parser.add_argument('--input', default='testInput')

Categories

Resources