I use argparse
to parse arguments for my scripts. Now I would like to write a program which runs a specific tool if the first argument (after the program name) is the name of the tool (which is closed in a class). For example if i want to run the "counter" tool I have to type:
python myscript.py counter filename
whereas if I want to run "fasta2bed" I have to type:
python myscript.py fasta2bed filename
I wrote this code but it seems like you can't skip positional arguments with Argparse:
import argparse
parser=argparse.ArgumentParser(
usage="""python myscript.py {toolname} filename [-option]""",
description='''Description.''',
epilog="""Epilog.""")
parser.add_argument('counter', nargs='?', choices=['counter'], help='count how many features are in the input file [-l][-s]')
parser.add_argument('fasta2bed', nargs='?', choices=['fasta2bed'], help='read sequences in FASTA format and print out BED format')
parser.add_argument('filename', help='the input file name')
parser.add_argument('-l', '--long', action='store_true', help='retrive a long summary file (default)')
parser.add_argument('-s', '--short', action='store_true', help='retrive a short summary file')
args=parser.parse_args()
The problem is that when I try to run python myscript.py fasta2bed filename
it doesn't work because it needs counter
.
So I try to insert all in a single argument like this:
parser.add_argument('tool', nargs='?', choices=['counter', 'fasta2bed'], help='help')
with this aim:
data = open("inputfile", "r")
if args.tool == "counter":
counter(data).summarize() #summarize is a function present in the counter class
elif args.tool == "fasta2bed":
fasta2bed(data) #fasta2bed is just a function
but it doesn't work because it runs counter
instead of fasta2bed
...
How can I make this work?
To my knowledge, you can't skip an argument the way you seem to want to. That said, it seems you don't really want to skip one. The way to do this is change the way you're looking at the problem. What would work is to have a single argument that accepts either 'counter' or 'fasta2bed'. Changing the relevant lines in your code might produce something like:
import argparse
parser=argparse.ArgumentParser(
usage="""python myscript.py {toolname} filename [-option]""",
description='''Description.''',
epilog="""Epilog.""")
parser.add_argument('toolname', choices=['counter', 'fasta2bed'], help='the name of the tool to be used')
parser.add_argument('filename', help='the input file name')
parser.add_argument('-l', '--long', action='store_true', help='retrive a long summary file (default)')
parser.add_argument('-s', '--short', action='store_true', help='retrive a short summary file')
args=parser.parse_args()
Hopefully this will help get you started.
Edit for additional material:
You can check the choices for toolname by checking args.toolname:
if args.toolname == 'counter':
print 'Running counter'
else:
print 'Running fasta2bed'
Note that you'll need to use elif if you have more than 2 choices.