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?
halfer
20.2k20 gold badges111 silver badges208 bronze badges
asked Aug 20, 2019 at 20:05
Melissa Stewart
3,63515 gold badges54 silver badges91 bronze badges
1 Answer 1
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))
Sign up to request clarification or add additional context in comments.
Comments
lang-py
python main.py --source aws.