I am using argparse for command line arguments, where one argument is one or more .csv files:
parser = argparse.ArgumentParser(description='...')
parser.add_argument('csv', type=str, nargs='+', help='.csv file(s)'))
parser.add_argument('days', type=int, help='Number of days'))
args = parser.parse_args()
print(args.csv)
When running $ python Filename.py csv Filename.csv days 30, args.csv is ['csv', 'Filename.csv', 'days'], but I don't want to be capturing the csv and days arguments which surround the input csv files.
You can use
import argparse
parser = argparse.ArgumentParser(description='...')
parser.add_argument("csv", type=str, nargs='+', help='.csv file(s)')
parser.add_argument("days", type=int, help='Number of days')
args = parser.parse_args()
print(args.csv)
Invoked with e.g. python your_script.py test.csv 100 this yields
['test.csv']
Note that you do not need to encapsulate parser.add_argument(...) twice (as you had in your code).
Related
I am developing an application taking input from the user (path) and using it later however some problems when the path had special characters like % for example (C:\path%25 -> C C:\path5)
Is there any way to stop the parser from applying the skipping characters?
Am using the following code:
parser = argparse.ArgumentParser(description='test', formatter_class=argparse.ArgumentDefaultsHelpFormatter)
parser.add_argument('-p', '--path', help='Specifies the input path', nargs="*", dest='path', required=True, type=str)
args = parser.parse_args()
print(args.path)
I would like to allow a user to enter in multiple ids as an argument. For example, something like:
import argparse
parser = argparse.ArgumentParser(description='Dedupe assets based on group_id.')
parser.add_argument('--ids', nargs='?', default=None, type=int, help='Enter your ids')
parser.parse_args()
Yet when I enter in something like:
$ python test.py --ids 1 2 3 4
I get the following error:
test.py: error: unrecognized arguments: 2 3 4
What would be the proper way to allow/enter in multiple arguments for a single option?
you can use '+' rather than '?'.
import argparse
parser = argparse.ArgumentParser(description='Dedupe assets based on group_id.')
parser.add_argument('--ids', nargs='+', default=None, type=int, help='Enter your ids')
args = parser.parse_args()
print(args.ids)
Here's my code:
def parse_args():
parser = argparse.ArgumentParser(description='Simple training script for object detection from a CSV file.')
parser.add_argument('csv_path', help='Path to CSV file')
parser.add_argument('--weights', help='Weights to use for initialization (defaults to ImageNet).', default='imagenet')
parser.add_argument('--batch-size', help='Size of the batches.', default=1, type=int)
return parser.parse_args()
when I run my code, I get an error:
usage: Train.py [-h] [--weights WEIGHTS] [--batch-size BATCH_SIZE] csv_path
Train.py: error: too few arguments
Any idea where I'm going wrong?
This is because you did not specify the number of arguments expected after each flag with nargs as such:
import argparse
def parse_args():
parser = argparse.ArgumentParser(description='Simple training script for object detection from a CSV file.')
parser.add_argument('csv_path', nargs="?", type=str, help='Path to CSV file')
parser.add_argument('--weights', nargs="?", help='Weights to use for initialization (defaults to ImageNet).', default='imagenet')
parser.add_argument('--batch-size', nargs="?", help='Size of the batches.', default=1, type=int)
return parser.parse_args()
parse_args()
According to the doc:
If the nargs keyword argument is not provided, the number of arguments consumed is determined by the action. Generally this means a single command-line argument will be consumed and a single item (not a list) will be produced.
'?'. One argument will be consumed from the command line if possible, and produced as a single item. If no command-line argument is present, the value from default will be produced. Note that for optional arguments, there is an additional case - the option string is present but not followed by a command-line argument. In this case the value from const will be produced. Some examples to illustrate this:
Details here
The first arg csv_path is required (you did not provide some default value), so you need to pass that to your command line like below:
python Train.py some_file.csv # or the path to your file if it's not in the same directory
Try this:
import argparse
import sys
import csv
parser = argparse.ArgumentParser()
parser.add_argument('--file', default='fileName.csv')
args = parser.parse_args()
csvdata = open(args.file, 'rb')
Subcommands like git commit and git status can be easily parsed with argparse using add_subparsers. How does one get nested settings input for choices selected from command line?
Let's say I want to play music with all custom settings:
play.py --path /path/to.file --duration 100 --filter equalizer --effect echo equalizer_settings 1 2 echo_settings 5
Here --filter equalizer and --effect echo are first level choices but I need to get settings for those as secondary arguments. Ex: echo_settings 5 and equalizer_settings 1 2.
Secondary settings could be more than one and preferably with named arguments.
Listed below is what I have so far...
import argparse
parser = argparse.ArgumentParser(description='Play music the way I want it.')
parser.add_argument('-p', '--path', type=str, required=True, help='File path')
parser.add_argument('-d', '--duration', type=int, default=50, help='Play duration')
parser.add_argument('-f', '--filter', choices=['none', 'equalizer'], default='none', help='Filter selection')
parser.add_argument('-e', '--effect', choices=['none', 'echo', 'surround'], default='none', help='Effect selection')
subparsers = parser.add_subparsers(help='Settings of optional parameters')
equalizer_parser = subparsers.add_parser("equalizer_settings")
equalizer_parser.add_argument('equalizer_min_range', type=int)
equalizer_parser.add_argument('equalizer_max_range', type=int)
echo_parser = subparsers.add_parser("echo_settings")
echo_parser.add_argument('echo_strength', type=int)
surround_parser = subparsers.add_parser("surround_settings")
surround_parser.add_argument('surround_strength', type=int)
args = parser.parse_args()
print(args)
Currently this errors in error: unrecognized arguments
You can only use one subparser at a time, i.e. one of those 3 settings. There are some advanced ways of using several subparsers in sequence, but I don't think we want to go there (see previous SO argparse questions).
But do you really need to use subparsers? Why not just another set of optionals (flagged) arguments, something like:
parser.add_argument("--equalizer_settings", nargs=2, type=int)
parser.add_argument("--echo_settings", type=int)
parser.add_argument("--surround_strength", type=int
You are just adding some parameters, not invoking some sort of action command.
I want to have some options in argparse module such as --pm-export however when I try to use it like args.pm-export I get the error that there is not attribute pm. How can I get around this issue? Is it possible to have - in command line options?
As indicated in the argparse docs:
For optional argument actions, the value of dest is normally inferred from the option strings. ArgumentParser generates the value of dest by taking the first long option string and stripping away the initial -- string. Any internal - characters will be converted to _ characters to make sure the string is a valid attribute name
So you should be using args.pm_export.
Unfortunately, dash-to-underscore replacement doesn't work for positional arguments (not prefixed by --).
E.g:
parser = argparse.ArgumentParser(description='Process some integers.')
parser.add_argument('logs-dir',
help='Directory with .log and .log.gz files')
parser.add_argument('results-csv', type=argparse.FileType('w'),
default=sys.stdout,
help='Output .csv filename')
args = parser.parse_args()
print args
# gives
# Namespace(logs-dir='./', results-csv=<open file 'lool.csv', mode 'w' at 0x9020650>)
So, you should use 1'st argument to add_argument() as attribute name and metavar kwarg to set how it should look in help:
parser = argparse.ArgumentParser(description='Process some integers.')
parser.add_argument('logs_dir', metavar='logs-dir',
nargs=1,
help='Directory with .log and .log.gz files')
parser.add_argument('results_csv', metavar='results-csv',
nargs=1,
type=argparse.FileType('w'),
default=sys.stdout,
help='Output .csv filename')
args = parser.parse_args()
print args
# gives
# Namespace(logs_dir=['./'], results_csv=[<open file 'lool.csv', mode 'w' at 0xb71385f8>])
Dashes are converted to underscores:
import argparse
pa = argparse.ArgumentParser()
pa.add_argument('--foo-bar')
args = pa.parse_args(['--foo-bar', '24'])
print args # Namespace(foo_bar='24')
Concise and explicit but probably not always acceptable way would be to use vars():
#!/usr/bin/env python3
import argparse
parser = argparse.ArgumentParser()
parser.add_argument('a-b')
args = vars(parser.parse_args())
print(args['a-b'])
getattr(args, 'positional-arg')
This is another OK workaround for positional arguments:
#!/usr/bin/env python3
import argparse
parser = argparse.ArgumentParser()
parser.add_argument('a-b')
args = parser.parse_args(['123'])
assert getattr(args, 'a-b') == '123'
Tested on Python 3.8.2.
I guess the last option is to change shorten option -a to --a
import argparse
parser = argparse.ArgumentParser(description="Help")
parser.add_argument("--a", "--argument-option", metavar="", help="") # change here
args = parser.parse_args()
option = args.a # And here
print(option)