I want to pass arguments to a Python script in two ways,
python main.py --source=aws
and
python main.py source aws
This is my current code,
parser = argparse.ArgumentParser()
parser.add_argument("--s", "--source", help='Flag to choose source')
This makes the first option possible. How do I make the second option possible?
There is not a way to do that with Argparse. The only way to do that is by filtering stdin using sys.argv.
import argparse
import sys
mangle_my_args = ['s', 'source']
arguments=['--'+arg if arg in mangle_my_args else arg for arg in sys.argv[1:]]
parser = argparse.ArgumentParser()
parser.add_argument("--s", "--source", help='Flag to choose source')
print(parser.parse_args(arguments))
Related
Tools like dnsgen works with this both type of options:
Without any argument:
cat domains.txt | dnsgen - | someotherprogram
With argument:
dnsgen -w wordlist filename.txt
Can I make my program use - without argument i.e. dnsgen - style. Lets say my code is this and it can receive data from both stdin and wordlist
import argparse
parser = argparse.ArgumentParser()
parser.add_argument('-w', '--wordlist', help="Wordlist")
argv = parser.parse_args()
my_awesome_function(argv.wordlist)
So, my code reads only from wordlist. How can I make this both by wordlist and by stdin using - argument style?
May be not an elegant solution but, You can assign a dummy variable and make it store_true as below.
import argparse
parser = argparse.ArgumentParser()
parser.add_argument('-', "--_", action='store_true')
parser.add_argument('-w', '--wordlist', help="Wordlist")
argv = parser.parse_args()
my_awesome_function(argv.wordlist)
I want to create a command parser mycommand, using argparse, with two subcommands read and write: read should have just one argument which is some path, and write should have two arguments one of which is a path and the other a value. It should be possible to execute the command in the following way:
mycommand read <path>
mycommand write <path> <value>
without using labels for the <path>, <value> arguments, i.e. without requiring --path. How can I do this?
This is pretty straight forward following the docs:
import argparse
parser = argparse.ArgumentParser()
subparsers = parser.add_subparsers()
read = subparsers.add_parser('read')
read.add_argument('path')
write = subparsers.add_parser('write')
write.add_argument('path')
write.add_argument('value')
print(parser.parse_args(['read', 'foo']))
print(parser.parse_args(['write', 'foo', 'bar']))
Note that this doesn't tell you what parser parsed the arguments. If you want that, you can simply add a dest to the add_subparsers command:
subparsers = parser.add_subparsers(dest='subparser')
Finally, you can add a default attribute for each subparser that you can use to perform the actions specific to that subparser. This is spelled out in the docs too, but for completeness, in our example, it might look something like this1:
import argparse
parser = argparse.ArgumentParser()
subparsers = parser.add_subparsers(dest='subparser')
def handle_read(args):
print('Handling read')
print(args)
read = subparsers.add_parser('read')
read.add_argument('path')
read.set_defaults(handler=handle_read)
def handle_write(args):
print('Handling write')
print(args)
write = subparsers.add_parser('write')
write.add_argument('path')
write.add_argument('value')
write.set_defaults(handler=handle_write)
args = parser.parse_args()
args.handler(args)
1I added the dest to subparsers in this example too for illustrative purposes -- Using argparse with handler functions probably makes that attribute on args obsolete.
I used the OptParse module a few years back, and it seemed so easy, but seeing the argparse module, it doesn't seem intuitive to use. Thus, I'd like some help.
I currently have (which isnt much yet):
import argparse
parser = argparse.ArgumentParser()
parser.parse_args()
I'd like to be able to issue a command like python myscript.py --char 20 . This value for char flag will always be an int.
If someone can please help, I'd greatly appreciate it! Thank you
This is how you add an argument, and retrieve it:
import argparse
parser = argparse.ArgumentParser()
parser.add_argument('--char', type=int,
help='number of characters')
args = parser.parse_args()
char = args.char
You should check out the docs:
https://docs.python.org/3/library/argparse.html
And here's a tutorial:
https://docs.python.org/2/howto/argparse.html
you need to add an argument to the parser object, and optionally specify the parameter type to be int
# testargs.py
import argparse
parser = argparse.ArgumentParser()
parser.add_argument('--char', type=int)
args = parser.parse_args()
print(args.char)
if you execute this file with
python testargs.py --char 20
it should print 20 to the console
I have a program which can be used in the following way:
program install -a arg -b arg
program list
program update
There can only ever be one of the positional arguments specified (install, list or update). And there can only be other arguments in the install scenario.
The argparse documentation is a little dense and I'm having a hard time figuring out how to do this correctly. What should my add_arguments look like?
This seems like you want to use subparsers.
from argparse import ArgumentParser
parser = ArgumentParser()
subparsers = parser.add_subparsers()
install = subparsers.add_parser('install')
install.add_argument('-b')
install.add_argument('-a')
install.set_defaults(subparser='install')
lst = subparsers.add_parser('list')
lst.set_defaults(subparser='list')
update = subparsers.add_parser('update')
update.set_defaults(subparser='update')
print parser.parse_args()
As stated in the docs, I have combined with set_defaults so that you can know which subparser was invoked.
I am creating a program that requires conditional arguments using argparse. I would like to generate new arguments in my code depending on if a previous argument has been entered or not. Here is a basic example of how I would like my code to look
import argparse
parser = argparse.ArgumentParser()
parser.add_argument("-bowtie",action = "store_true",help="use to run bowtie")
args = parser.parse_args()
if args.bowtie:
parser.add_argument( add some new argument here )
args = parser.parse_args()
I've been doing a lot of stuff recently that's really similar to this. Look into subparsers:
parser = argparser.ArgumentParser
subparsers = parser.add_subparsers('-bowtie')
subparser = subparsers.add_parser()
subparser.add_argument('new argument')